Stateflow Semantics
In Stateflow®, semantics describe the execution behavior of your Stateflow chart. Various factors can affect how your chart executes, including:
Execution order of states
Transition order between states
Events sent by parallel or superstates
As you build your chart, you expect it to behave in a certain way. By knowing how these factors affect your chart, you can create a chart that behaves with intentional interaction of the graphical and nongraphical objects. Graphical and nongraphical objects are the building blocks for all Stateflow charts.
Stateflow Objects
Stateflow objects are the building blocks of Stateflow charts. These objects can be categorized as either graphical or nongraphical. Graphical objects consist of objects that appear graphically in a chart. Nongraphical objects appear textually in a chart and often refer to data, events, and messages. This chart shows a variety of both graphical and nongraphical objects.
For more information on this example, see How Stateflow Objects Interact During Execution.
Graphical Objects
To build graphical objects, use the object palette in the Stateflow Editor (see Stateflow Editor Operations).
Nongraphical Objects
You create nongraphical objects textually in your chart. See Add Stateflow Data, Define Events in a Chart, and Define Messages in a Chart for details. Examples of nongraphical objects include:
Nongraphical Object | Description | Reference |
---|---|---|
Condition | Boolean expression that specifies that a transition path is valid if the expression is true; part of a transition label | |
Condition action | Action that executes as soon as the condition evaluates to true; part of a transition label | |
State actions | Expressions that specify actions to take when a state is active, such as initializing or updating data; part of a state label | |
Function calls | Expression used to activate a specific function within a chart. | Reuse MATLAB Code by Defining MATLAB Functions and Reuse Simulink Functions in Stateflow Charts |
Temporal logic statements | Operators that are used to control chart actions. | Control Chart Execution by Using Temporal Logic |
Enter a Chart
The set of default flow paths execute. If this action does not cause a state entry and the chart has parallel decomposition, then each parallel state becomes active.
If executing the default flow paths does not cause state entry, a state inconsistency error occurs.
Execute an Active Chart
If the chart has no states, each execution is equivalent to initializing a chart. Otherwise, the active children execute. Parallel states execute in the same order that they become active.
Enter a State
If the parent of the state is not active, perform steps 1 through 4 for the parent.
If this state is a parallel state, check that all siblings with a higher (that is, earlier) entry order are active. If not, perform steps 1 through 5 for these states first.
Mark the state active.
Perform any entry actions.
Enter children, if needed:
If the state contains a history junction and there was an active child of this state at some point after the most recent chart initialization, perform the entry actions for that child. Otherwise, execute the default flow paths for the state.
If this state has children that are parallel states (parallel decomposition), perform entry steps 1 through 5 for each state according to its entry order.
If this state has only one child substate, the substate becomes active when the parent becomes active, regardless of whether a default transition is present. Entering the parent state automatically makes the substate active. The presence of any inner transition has no effect on determining the active substate.
If this state is a parallel state, perform all entry steps for the sibling state next in entry order if one exists.
If the transition path parent is not the same as the parent of the current state, perform entry steps 6 and 7 for the immediate parent of this state.
Execute an Active State
The set of outer flow charts execute. If this action causes a state transition, execution stops. This step never occurs for parallel states.
During actions and valid on-event actions are performed.
The set of inner flow charts execute. If this action does not cause a state transition, the active children execute, starting at step 1. Parallel states execute in the same order that they become active.
Exit an Active State
If this is a parallel state, make sure that all sibling states that became active after this state have already become inactive. Otherwise, perform all exiting steps on those sibling states.
If there are any active children, perform the exit steps on these states in the reverse order that they became active.
Perform any exit actions.
Mark the state as inactive.
Execute a Set of Flow Charts
Flow charts execute by starting at step 1 below with a set of starting transitions. The starting transitions for inner flow charts are all transition segments that originate on the respective state and reside entirely within that state. The starting transitions for outer flow charts are all transition segments that originate on the respective state but reside at least partially outside that state. The starting transitions for default flow charts are all default transition segments that have starting points with the same parent:
Ordering of a set of transition segments occurs.
While there are remaining segments to test, testing a segment for validity occurs. If the segment is invalid, testing of the next segment occurs. If the segment is valid, execution depends on the destination:
States
Testing of transition segments stops and a transition path forms by backing up and including the transition segment from each preceding junction until the respective starting transition.
The states that are the immediate children of the parent of the transition path exit.
The transition action from the final transition path executes.
The destination state becomes active.
Junctions with no outgoing transition segments
Testing stops without any state exits or entries.
Junctions with outgoing transition segments
Step 1 is repeated with the set of outgoing segments from the junction.
After testing all outgoing transition segments at a junction, backtrack the incoming transition segment that brought you to the junction and continue at step 2, starting with the next transition segment after the backtrack segment. The set of flow charts finishes execution when testing of all starting transitions is complete.
Execute an Event Broadcast
Output edge-trigger event execution is equivalent to changing the value of an output data value. All other events have the following execution:
If the receiver of the event is active, then it executes. The event receiver is the parent of the event unless a direct event broadcast occurs using the
send()
function.If the receiver of the event is not active, nothing happens.
After broadcasting the event, the broadcaster performs early return logic based on the type of action statement that caused the event.
Action Type
Early Return Logic
State Entry
If the state is no longer active at the end of the event broadcast, any remaining steps in entering a state do not occur.
State Exit
If the state is no longer active at the end of the event broadcast, any remaining exit actions and steps in state transitioning do not occur.
State During
If the state is no longer active at the end of the event broadcast, any remaining steps in executing an active state do not occur.
Condition
If the origin state of the inner or outer flow chart or parent state of the default flow chart is no longer active at the end of the event broadcast, the remaining steps in the execution of the set of flow charts do not occur.
Transition
If the parent of the transition path is not active or if that parent has an active child, the remaining transition actions and state entry do not occur.