Main Content

Execution Order for Parallel States

Although parallel (AND) states execute concurrently, the Stateflow® chart must determine when to activate each during simulation. This ordering determines when each parallel state performs the actions that take it through all stages of execution.

Ordering of Parallel States

Charts assign a priority number to each parallel state. The lower the number, the sooner the state executes. The priority number appears in the upper-right corner of each state.

When you open a new chart or a chart that does not contain any parallel states, the chart automatically assigns priority numbers to parallel states in the order you create them. The numbering starts with the next available number in the parent container.

To change the execution order of a parallel state:

  1. Right-click the parallel state.

  2. Click Execution Order and select the new priority number.

When you change the priority number for a parallel state, the chart automatically renumbers the other parallel states to preserve their relative priority.

In code generated from charts that contain parallel states, each state executes based on its priority.

Order Maintenance for Parallel States

When you remove, renumber, or add parallel states, the chart reprioritizes the parallel states to:

  • Fill in gaps in the sequence so that ordering is continuous.

  • Ensure that no two states have the same priority.

  • Preserve the intended relative priority.

Consider this example:

Stateflow chart with three top-level parallel states called a, b, and c. State a has three parallel substates called d, e, and f.

By default, the state and substates are numbered in the order in which you created them. For example, if you make these changes to the chart:

  1. Change the priority of top-level state b to 3.

  2. Add a top-level state g.

  3. Remove substate e.

Stateflow chart with four top-level parallel states called a, b, c, and g. State a has two parallel substates called d and f.

The chart preserves the priority that you set explicitly for the top-level state b, but renumbers all other parallel states to preserve their prior relative order.

Execution Priorities in Restored States

There are situations in which you need to restore a parallel state after you remove it from a Stateflow chart. However, a chart cannot always reinstate the original execution priority to a restored state. It depends on how you restore the state.

If you remove a state by...And restore the state by...What is the priority?
Deleting, cutting, dragging outside the boundaries of the parent state, or dragging so its boundaries overlap the parent stateUsing the undo commandThe original priority is restored.
Dragging outside the boundaries of the parent state or so its boundaries overlap the parent state and releasing the mouse buttonDragging it back into the parent stateThe original priority is lost. The Stateflow chart treats the restored state as the last created and assigns it the lowest execution priority.
Dragging outside the boundaries of the parent state or so its boundaries overlap the parent state without releasing the mouse buttonDragging it back into the parent stateThe original priority is restored.
Dragging so its boundaries overlap one or more sibling statesDragging it to a location with no overlapping boundaries inside the same parent stateThe original priority is restored.
CuttingPastingThe original priority is lost. The Stateflow chart treats the restored state as the last created and assigns it the lowest execution priority.

Execution Order of Parallel States in Boxes and Subcharts

When you group parallel states inside a box, the states retain their relative execution order. In addition, the Stateflow chart assigns the box its own priority based on the default ordering rules. This priority determines when the chart activates the parallel states inside the box.

When you convert a state with parallel decomposition into a subchart, its substates retain their relative execution order.

Related Topics