Main Content

Evaluate Transitions

Stateflow® uses transitions in charts to move from one exclusive (OR) state to another exclusive (OR) state. For the entry and execution workflows of chart execution, Stateflow evaluates transitions to determine if they are valid. A valid transition is a transition whose condition labels are true and whose path ends at a state. If a transition is valid, Stateflow exits from the source state and enters the destination state. To learn about when evaluation occurs during the execution and entry workflows, see Execution of a Stateflow Chart and Enter a Chart or State.

Workflow for Evaluating Transitions

Flow chart that shows the steps for evaluating a transition.

Transition Evaluation Order

When multiple transitions originate from a single source, such as a state or junction, Stateflow uses evaluation order to determine when to test each transition. Depending on which action language your chart uses, you can create the order of your transitions explicitly or implicitly. Whether explicitly or implicitly ordered, transitions show a number near the source of the transition that designates the transition order.

Note

Use explicit ordering to avoid your transitions from changing order while you are editing a chart.

Explicit Ordering

When you open a new Stateflow chart, all outgoing transitions from a source are automatically numbered in the order in which you create them. The order starts with 1 and continues to the next available number for the source.

To change the execution order of a transition, right-click the transition, place your cursor over Execution Order, and select the order in which you want your transition to execute. When you change a transition number, the Stateflow chart automatically renumbers the other outgoing transitions for the source by preserving their relative order.

Implicit Ordering

For C charts in implicit ordering mode, a Stateflow chart evaluates a group of outgoing transitions from a single source based on:

  • Hierarchy.

    A chart evaluates a group of outgoing transitions in an order based on the hierarchical level of the parent of each transition.

  • Label.

    A chart evaluates a group of outgoing transitions with equal hierarchical priority based on the labels, in the following order of precedence:

    1. Labels with events and conditions

    2. Labels with events

    3. Labels with conditions

    4. No label

    For more information about chart behavior when using events, see Control Chart Behavior by Using Implicit Events.

  • Angular surface position of transition source.

    A chart evaluates a group of outgoing transitions with equal hierarchical and label priority based on angular position on the surface of the source object. The transition with the smallest clock position has the highest priority. For example, a transition with a 2 o'clock source position has a higher priority than a transition with a 4 o'clock source position. A transition with a 12 o'clock source position has the lowest priority.

Transition to the Inner Edge of a Parent State

Transitions that end on the inside edge of a parent state are a shortcut back to the default transition path, and the default path is evaluated during the current time step. In this example, the transition from state B leads immediately to the default transition to state A.

Chart that shows a transition to the inner edge of a state.

If there are default transitions, then Stateflow immediately executes those paths. If not, and there are no children, then that is the end of the time step. In both cases, the parent remains active, and exit and entry actions of the parent are not executed.

Evaluate Outer Transition

In this example, the Stateflow chart is initialized and the entry actions are performed for StateA. A new time step occurs and the chart wakes up. By following the Workflow for Stateflow Chart Execution, Stateflow finds multiple outer transition paths from StateA. At this time step x = 1, y = 1, and z = 1.

Stateflow chart with states called StateA, StateC, StateD, and StateE.

By following the Workflow for Evaluating Transitions, the steps for evaluating the transitions of this chart are in this order:

  1. Transition 1 from StateA is marked for evaluation.

  2. Transition 1 from StateA has a condition.

  3. The condition is true.

  4. The destination of transition 1 from StateA is not a state.

  5. The junction does have outgoing transitions.

  6. Transition 1 from the junction is marked for evaluation.

  7. Transition 1 from the junction has a condition.

  8. The condition is false.

  9. Transition 2 from the junction is marked for evaluation.

  10. Transition 2 from the junction does not have a condition.

  11. The destination of transition 2 from the junction is a state (StateD).

  12. StateD is marked for entry, and StateA is marked for exit.

To complete the time step, follow the Workflow for Exiting a State for StateA and the Workflow for Entering a Chart or State for StateE.

Evaluate Outer Transition with Backtracking

When all outgoing transitions from a source are invalid or do not end with a terminating junction, but there are previously unevaluated transitions, Stateflow returns to the previous state or junction to evaluate all possible paths.

In this example, the Stateflow chart is initialized and the entry actions are performed for StateA. A new time step occurs, and the chart wakes up. By following the Workflow for Stateflow Chart Execution, Stateflow finds multiple outer transition paths from StateA. At this time step x = 1, y = 1, and z = 1.

Stateflow chart with states called StateA, StateC, StateD, and StateE.

By following the Workflow for Evaluating Transitions, the steps for evaluating the transitions of this chart are in this order:

  1. Transition 1 from StateA is marked for evaluation.

  2. Transition 1 from StateA has a condition.

  3. The condition is true.

  4. The destination of transition 1 from StateA is not a state.

  5. The junction does have outgoing transitions.

  6. Transition 1 from the junction is marked for evaluation.

  7. Transition 1 from the junction has a condition.

  8. The condition is false.

  9. Transition 2 from the junction is marked for evaluation.

  10. Transition 2 from the junction has a condition.

  11. The condition is false.

  12. Transition 2 from StateA is marked for evaluation.

  13. Transition 2 from StateA does not have a condition.

  14. The destination of transition 2 from StateA is a state (StateE).

  15. StateE is marked for entry, and StateA is marked for exit.

To complete the time step, follow the Workflow for Exiting a State for StateA and the Workflow for Entering a Chart or State for StateE.

Prevent Backtracking

In this example, a terminating junction prevents backtracking. The Stateflow chart is initialized and the entry actions are performed for StateA. A new time step occurs and the chart wakes up. By following the Workflow for Stateflow Chart Execution, Stateflow finds multiple outer transition paths from StateA. At this time step x = 1, y = 1, and z = 1.

Stateflow chart with states called StateA, StateC, StateD, and StateE.

By following the Workflow for Evaluating Transitions, the steps for evaluating the transitions of this chart are in this order:

  1. Transition 1 from StateA is marked for evaluation.

  2. Transition 1 from StateA has a condition.

  3. The condition is true.

  4. The destination of transition 1 from StateA is not a state.

  5. The junction does have outgoing transitions.

  6. Transition 1 from the junction is marked for evaluation.

  7. Transition 1 from the junction has a condition.

  8. The condition is false.

  9. Transition 2 from the junction is marked for evaluation.

  10. Transition 2 from the junction has a condition.

  11. The condition is false.

  12. Transition 3 from the junction is marked for evaluation.

  13. Transition 3 from the junction does not have a condition.

  14. The destination is not a state and does not have any outgoing transitions.

  15. Return to Workflow for Stateflow Chart Execution.

To complete the time step, follow the Workflow for Stateflow Chart Execution for StateA, starting where you left off.

Evaluate Outer Transitions with Condition and Transition Actions

This example contains both condition actions and transition actions:

  • In transition label syntax, condition actions follow the transition condition and are enclosed in curly braces ({}). Condition actions are executed when the condition is evaluated as true but before the transition path has been determined to be valid.

  • In transition label syntax, transition actions are preceded with a forward slash (/) and are enclosed in curly braces ({}). Transition actions execute only after the transition path is determined to be valid.

The Stateflow chart is initialized and the entry actions are performed for StateA. A new time step occurs and the chart wakes up. There are multiple outer transition paths from StateA. At this time step x = 1, y = 1, and z = 1.

Stateflow chart with states called StateA, StateC, StateD, and StateE.

By following the Workflow for Evaluating Transitions, the steps for evaluating the transitions of this chart are in this order:

  1. Transition 1 from StateA is marked for evaluation.

  2. Transition 1 from StateA has a condition ([y >= 1]).

  3. The condition is true.

  4. There are no condition actions.

  5. The destination of transition 1 from StateA is not a state.

  6. The junction does have outgoing transitions.

  7. Transition 1 from the junction is marked for evaluation.

  8. Transition 1 from the junction has a condition ([x > 2]).

  9. The condition is false.

  10. Transition 2 from the junction is marked for evaluation.

  11. Transition 2 from the junction has a condition ([x >= 1]).

  12. The condition is true.

  13. There is a condition action ({y = 0;}). Now y = 0.

  14. The junction does have outgoing transitions.

  15. The transition from the junction is marked for evaluation.

  16. Transition 1 from the junction has a condition ([z >= 5]).

  17. The condition is false.

  18. Transition 2 from StateA is marked for evaluation.

  19. Transition 2 from StateA does not have a condition.

  20. The destination of transition 2 from StateA is a state (StateD).

  21. StateD is marked for entry, and StateA is marked for exit. Execute the transition action for this valid path (/{z = 5}). Now z = 5.

To complete the time step, follow the Workflow for Exiting a State for StateA and the Workflow for Entering a Chart or State for StateE.

Related Topics