Main Content

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.

Stateflow objects in a chart.

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).

Graphical ObjectsTypesReferences

Flow charts

Decision logic patternsCreate Flow Charts in Stateflow
Loop logic patterns

Functions

Graphical functionsReuse Logic Patterns by Defining Graphical Functions
MATLAB® functionsReuse MATLAB Code by Defining MATLAB Functions
Truth table functionsUse Truth Tables to Model Combinatorial Logic
Simulink® functionsReuse Simulink Functions in Stateflow Charts

Junctions

Connective junctionsCombine Transitions and Junctions to Create Branching Paths
History junctionsResume Prior Substate Activity by Using History Junctions

States

States with exclusive (OR) decompositionExclusive (OR) State Decomposition
States with parallel (AND) decompositionParallel (AND) State Decomposition
Substates and superstatesCreate Substates and Superstates

Transitions

Object-to-object transitionsTransition Between Operating Modes
Default transitionsUse Default Transitions to Specify Initial Substate Activity
Inner transitionsControl Chart Execution by Using Inner Transitions
Self-loop transitionsSelf-Loop Transition

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 ObjectDescriptionReference
ConditionBoolean expression that specifies that a transition path is valid if the expression is true; part of a transition label

Define Actions in a Transition and Conditions

Condition actionAction that executes as soon as the condition evaluates to true; part of a transition label

Define Actions in a Transition and Condition Actions

State actionsExpressions that specify actions to take when a state is active, such as initializing or updating data; part of a state label

Define Actions in a State

Function callsExpression 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 statementsOperators 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

  1. If the parent of the state is not active, perform steps 1 through 4 for the parent.

  2. 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.

  3. Mark the state active.

  4. Perform any entry actions.

  5. Enter children, if needed:

    1. 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.

    2. 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.

    3. 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.

  6. If this state is a parallel state, perform all entry steps for the sibling state next in entry order if one exists.

  7. 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

  1. The set of outer flow charts execute. If this action causes a state transition, execution stops. This step never occurs for parallel states.

  2. During actions and valid on-event actions are performed.

  3. 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

  1. 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.

  2. If there are any active children, perform the exit steps on these states in the reverse order that they became active.

  3. Perform any exit actions.

  4. 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:

  1. Ordering of a set of transition segments occurs.

  2. 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

    1. 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.

    2. The states that are the immediate children of the parent of the transition path exit.

    3. The transition action from the final transition path executes.

    4. 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.

  3. 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:

  1. 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.

  2. 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.

Related Topics