Engee documentation

The logic of finite automata operation

Page in progress.

State Machines It is an abstract model of computation that sequentially transitions between different states based on input data or events. A finite state machine includes two approaches to model construction — a state machine and a transition graph.

Logic of the state machine

State Machines It is a representation of an event—driven system that switches from one mode of operation to another when the condition determining the change is true. State machines use all available block functionality Chart to build models of a finite automaton, namely:

An example of a finite state machine with a state machine:

stateflow all in one en

The execution algorithm consists of the following steps:

The general logic of the state machine

  1. The operation of the state machine begins before any state is activated.:

    1. 1. If the state machine has a default transition (or several such transitions), then actions related to these transitions are performed first, taking into account their priorities and conditions.;

    1. 2. If there are several transitions by default, the transitions are analyzed in the order of the set priorities until a transition with an incomplete or missing condition is detected, after which the corresponding state is activated.;

    1. 3. If there are no default transitions and there is a single state, then it is activated automatically.

      Go to point 2.


  1. At the next step of the machine, after activating the state, possible transitions from the active state are checked. There can be zero, one, or several such transitions.

    1. 1. If there are no transitions from the current active state, the machine remains in the same current state, and the during and on sections (if specified) of this state are executed. The 'exit` section of this state is never executed.

    1. 2. If there is only one transition from the current state, then its condition is checked. If the condition is not met, the machine remains in the same current state, and the during and on sections (if specified) of this state are executed. If the condition has the value "true", then, first, the transition action is performed (if specified). Secondly:

      1. 2.1. If the transition leads to another state, the exit section of the current state and the entry section of the other state are executed (if specified). Another state becomes active instead of the current one (hierarchical states are not considered here).

      1. 2. 2. If the transition leads to the same state (here we are considering only external transitions), the 'exit` and entry sections of the current state are executed, and it remains active.

      1. 2.3. If the transition leads to a dead end node, the current state remains active, and its during and on sections are executed (if specified).

    1. 3. If several transitions exit the current state, the machine tries to go through them in turn in the order of set priorities until it encounters a transition whose condition is fulfilled or absent (composite transitions are not considered here — see the next paragraph). If no such transition is found, the machine remains in the current state, and the during and on sections (if specified) of this state are executed. If the transition is found, then see paragraphs 2.2.1, 2.2.2 and 2.2.3.


  1. If any of the transitions (clauses 2.2 and 2.3) is composite (consists of two or more segments) and has forks, the backtrackingfootnote mechanism can be implemented:[Backtracking is a problem—solving method based on an attempt to sequentially iterate through all possible solutions in order to find the right one. When the program reaches a decision node (for example, in the transition graph, it may be an exit node), and all possible options for further actions turn out to be incorrect (for example, all transitions from the node have false conditions), it returns (backtracks) to the previous decision node and tries another (alternative) option.]: moving through the transitions according to their priorities and conditions, the automaton remembers the forks passed, and if the condition for the next transition is not fulfilled, the automaton returns to the nearest fork passed, where alternative branches have not been tested, and makes a new attempt, but along an alternative path. If there are no forks with alternative paths, then the current state remains active, and its during and on sections are executed (if specified).

Groups of operators

In the state machine operator groups entry, during, and exit initiate actions when states change and while in a state. The logic of executing these operators is closely related to transitions, states, and activation order. Let’s take a closer look at how each group of operators works and how they interact with each other.:

  1. Checking transition conditions — At each step, the state machine checks whether there is an active transition from the current state. The transition may be conditioned by conditions change indicators (hasChanged, etc.) or temporal logic (after, etc.), which indicate when the transition can be made;

  2. Making a transition if the condition is met:

    • If the transition conditions are met, the state machine deactivates the current state.;

    • exit — is executed before the end of the current state, if the group of exit statements is specified. It completes all actions related to this state.;

    • Then there is a transition to a new state, where a group of entry statements for this state is executed.

  3. If the transition does not occur:

    • If the transition conditions are not met, the state machine remains in the current state.;

    • during — is executed if the state is active and actions are specified for during. This continues at each step as long as the automaton remains in this state and the transition conditions are not met.;

    • 'on` — is executed when a certain condition occurs, if the current state is active and the group of on statements is associated with this condition.

The priority of executing groups of operators

  1. Transition condition — is checked first to determine whether the transition will be completed;

  2. Transition actions — are performed after checking the condition, if the transition is confirmed;

  3. `exit' — completes the execution of the current state before the transition;

  4. on — executed if the state is active and the specified event has occurred;

  5. `entry' — is performed when a new state is activated, preparing it for performing actions;

  6. during — is performed at each step of the machine, while the current state remains active.

Then the logic of the finite state machine with transitions and groups of operators is as follows:

  • At each step, the state machine checks the transition conditions. If a transition is possible, then the actions exit are performed for the current state, and then entry for the new one.;

  • If the transition does not occur, then the during actions are performed for the current state.;

  • on adds the ability to respond to events by activating actions only if these events occur in the active state.

The logic of transition graphs

Transition graphs — it is a graphical construct representing the flow of operations or logic in the system. Unlike state machines, transition graphs are constructed using nodes., transitions and default transitions, not using states. An example of a finite state machine with a transition graph:

sf loop 1 1

The logic of transition graphs consists of sequential analysis of nodes and transitions, starting with the default transition. The algorithm of operation can be divided into the following points:

  1. Unlike a state machine, the transition graph is fully checked at each step. The transition is performed if the condition is true, or, if there is no condition, it is performed by default. If a transition is possible (the condition is true or not), then the graph performs actions related to the transition. Go to point 2.

  2. When performing a transition, if the condition is true or missing, then the actions defined on this transition are performed. After that, the transition is performed to the next node specified in the graph. Completing the actions completes the second step and leads to step 3.

  3. If the machine has arrived at the node, then the following actions are performed, depending on three possible outcomes:

    1. 1. The node is a dead end. If backtracking is possible, then it is performed. Otherwise, the automaton step is completed.

    1. 2. One transition originates from the node. The machine is trying to make this transition. If the condition is met, the automaton performs a transition action and proceeds to the node to which this transition leads. If the condition is not met, the machine tries to perform backtracking. If backtracking is possible, then backtracking is performed. Otherwise, the automaton step is completed.

    1. 3. There are several transitions coming from the node. If the automaton is at this node for the first time (within the current step), then it tries to perform the transition with the first sequence number. At the same time, the number of the transition that the machine tried to perform is remembered. If the path starting from this transition leads to backtracking, and the machine returns to the current fork, it will try to perform the next transition in order (numbered "2", then "3", etc.) until it has exhausted all available transitions or completes one of them successfully.

  4. If the transition from the node is completed, the automaton moves to the next node and continues execution from point 1. If the transition from the node is not completed, and backtracking is possible, then backtracking is performed. If backtracking is not possible, then the automaton step is completed. The entire logic of execution is based solely on transitions between nodes, as well as memorizing and sequentially checking the sequence numbers of transitions in fork nodes.

Nodes

Knot They are key elements of transition graphs that serve as junction points for transitions. They define the points through which the transitions pass, organizing an ordered sequence of steps in the model.

Default transitions

Default transition is a special type of transition, it does not have an initial node or state and is performed before they are activated. The system automatically adds a default transition for the first state and adds a node for the transition. The default transition is not needed if there is only one state in the system.

Transitions

condition action stateflow en

Transition They are connections between nodes that define the logic of moving from one node to another. Each transition can have a condition that is checked to determine whether to make the transition. Transitions can be conditional (performed only if a certain condition is true) or unconditional (always performed if they are active). If several transitions originate from a state or node, the program assigns explicit transitions to them. priorities displayed numerically (1,2,3, etc.). During simulation, transitions are checked in this order, and the first available transition with a true condition is performed.

Typical cases of transition logic:

  • Simple transitions between nodes that occur when conditions related to variables, parameters, or calculation results are met.;

  • Transitions that depend on change indicators (hasChanged, hasChangedFrom, hasChangedTo), temporal logic operators, etc., which are executed when the values of certain parameters change.;

  • The logic of backtracking, where transitions can return execution to the previous node to check alternative paths if the condition of the current transition is not met.

Conditions on the crossings

Transition conditions [] specify logical expressions that determine when the transition should occur. These conditions may include comparing values, checking logical expressions, or using logical operators such as `hasChanged'. If the condition is true, then the transition is performed, and execution continues at the next node.

Examples of using conditions on traffic crossings:

  • The condition can check ranges of values, for example, the transition is carried out only when a certain parameter level is reached.;

  • Logical expressions can include several checks, for example, a set of conditions ('A > 5 || B < 10`);

  • Change indicators are used to determine if the state of the system has changed at a certain moment.

Actions on transitions

Actions on transitions {} determine which Julia statements will be executed on the transition. Actions on transitions allow you to control changes in the data state and ensure the implementation of the necessary logic depending on the selected route (priority of transitions).

Temporal logic operators

Temporal logic operators transitions allow you to take into account time aspects or conditions related to the frequency of events. The logical operators after, at, and every' (and others) specify conditions for transitions based on time, repetition, or frequency of events. They can be used in a state machine within states with a group of `on operators and on transitions.

Examples of the application of temporal logic:

  • Using the after operator to specify the transition that is performed after a specified period of time or the number of steps of the state machine after activating the state associated with this operator;

  • Setting the transition using the 'at` operator, which is performed only at a given step from the moment of activation of the state associated with this operator;

  • Implementation of repetitive actions using the every operator to set the frequency of execution.

Memory Nodes

Memory Node stateflow history junction allows the state machine to remember the last active child state and restore it when the parent state is reactivated. The memory node is used in situations where it is necessary to save the context of the machine’s operation and continue execution from the same place, even after exiting and re-entering the state. The logic of the memory node is based on storing the current child state and activating it when certain conditions are met, for example, after executing the during and on groups of the parent state.

Examples of using a memory node:

  • Restoring the last active child state when reactivating the parent state;

  • Implementation of complex scenarios where it is necessary to temporarily exit the state and return to it while maintaining the context;

  • Managing transitions between child states based on the stored state.