.. include:: alias.rst .LF File Format =============== Introduction ------------ The ``.lf`` file format is a human-readable format for representing |rcm|'s semantics for state machines and all their properties. Its intended use case is the creation and manipulation of state machines using very small and light-weight tools using a text editor and a command line interface to read, validate and upload state machines to the |core|. .lf files --------- A ``.lf`` file holds a single top-level state, all its hierarchical 'children' states, as well as all ports, barriers, actions, parameters and results. The name of the file directly acts as the name of that state, e.g. a file called ``Wiggle.lf`` is interpreted as a hierarchical state machine called 'Wiggle'. State machines can also contain script code, e.g. for actions. Scripts are stored within ``.lf`` files as well, but they are enclosed in special tags(``@{`` as the opening tag and ``}@`` as the closing tag) to simplify parsing of ``.lf`` files as well as offer the possibility to use any script language within these tags. The text inside these special tags is parsed as a single string literal. This enables embedding other code, such as HTML-based context menus. An example of a state machine representing a simple timer module is given below: .. code-block:: lf Timer { port done service("timer").operation("wait").status == "success" parameterType { float duration; } entry @{ service("timer").operation("wait").call(parameter.duration) }@ clientData { type : "app"; } } Note that the script language (e.g. inside the entry or exit function) is Lua. In the following sections, we will go over the grammar of the individual elements. Elements of a Statemachine --------------------------- A statemachine consists of several elements. The following figure shows an overview of them. For a simple state only the name of the statemachine and the ports are mandatory, all the other elements can be used if necessary. If you want to build a DESK App, a few more elements are necessary which are explained later in the tutorial. .. figure:: _static/statemachine_structure.png :align: center The next code block displays these elements in lingua franka. The order of the elements inside the state does not matter. The structure of the child states is similar to the parent state. All the elements can be used, except of the clientData block. .. code-block:: lf App { port Success child("child_state_2").port("success") port Error child("child_state_1").port("error") or child("child_state_2").port("error") clientData { ... } parameterType { ... } resultType { ... } variableType { ... } entry @{ ... }@ action ..condition.. @{ ... }@ action ..condition.. @{ ... }@ exit @{ ... }@ --> child_state_1 { port success ..condition.. -> child_state_2 port error ..condition.. parameterType { ... } resultType { ... } variableType { ... } entry @{ ... }@ action ..condition.. @{ ... }@ action ..condition.. @{ ... }@ exit @{ ... }@ } where { ... } child_state_2 { port success ..condition.. port error ..condition.. parameterType { ... } resultType { ... } variableType { ... } entry @{ ... }@ action ..condition.. @{ ... }@ action ..condition.. @{ ... }@ exit @{ ... }@ } where { ... } } where { ... } Root States ----------- A single ``.lf`` file represents a single root state, whose name is given by the filename (without the ``.lf`` suffix). Top-level statements within the file define the various structural members of the root state, including a hierarchy of children states. The grammar for a root state (i.e. for a complete ``.lf`` file) is given as follows: .. code-block:: bnf ::= The rules for ```` and ```` will be defined in the next section. States ------ A state is a named entity in a hierarchical state machine that can contain other states or barriers. It can be connected to sibling states via ports, and contain actions that are executed upon certain triggers. In |RCM|, the state holds a reference to a single child state or barrier which it treats as its "first" child, i.e. upon executing the state, this "first" child element gets activated. In ``.lf`` files, this attribute is not expressed at the parent state, but the "first" child element is marked by a ``-->`` arrow, marking it as the entry state. A state can be a link state and reference a library which it links to. Such a link state cannot contain states, barriers, nor actions as these elements are inherited from the linked library. The only elements the link state can contain are ports without condition that must also be present within the library. Linking can occur at the top level or in child states. .. code-block:: bnf ::= ::= "<-" ::= epsilon | "-->" ::= epsilon | "where" ::= "{" "}" ::= * ::= | | | | | | | ::= "{" "}" ::= * ::= | ::= ::= ::= ::= * ::= A-Za-z ::= | 0-9 | "_" Valid state or library identifiers start with a letter and are followed by any number of letters, numbers or underscore. Identifiers are case-sensitive. The ``epsilon`` represents an empty symbol. The rules for ````, ````, ````, ````, ````, ````, ```` and ```` are given in the following sections. Barriers -------- Barriers are child elements of states and represent either: - a synchronization point where incoming port connections from multiple states can be merged onto a single destination state. Execution waits for all incoming ports to activate and transitions to the destination state. This is akin to a ``join`` operation in multi-threaded programs. - a branching point where a single incoming port is mapped onto a number of destination states. In this case, execution starts the execution of the destination states in parallel. This is similar to a ``fork`` operation in multi-threaded programs. Barriers can also be entry nodes of the containing state, i.e. they are the first element to activate when the parent activates. The grammar for barriers is defined as follows: .. code-block:: bnf ::= "barrier" ::= ::= "{" "}" ::= * ::= "->" Actions ------- Actions are functions written in Lua that are executed based on trigger conditions during the execution of the state machines. Special cases are the ``entry`` and ``exit`` actions, which are executed upon entering or leaving a state. More general ``actions`` have an additional condition expression which must evaluate to true to be executed. The grammar for actions is defined as follows: .. code-block:: bnf ::= | | ::= 'entry'