Documentation Center

  • Trial Software
  • Product Updates

Execution Order for Parallel States

Ordering for Parallel States

Although multiple parallel (AND) states in the same chart execute concurrently, the Stateflow® chart must determine when to activate each one during simulation. This ordering determines when each parallel state performs the actions that take it through all stages of execution, as described in Process for Entering, Executing, and Exiting States.

Unlike exclusive (OR) states, parallel states do not typically use transitions. Instead, order of execution depends on:

Parallel states are assigned priority numbers based on order of execution. The lower the number, the higher the priority. The priority number of each state appears in the upper right corner.

Because execution order is a chart property, all parallel states in the chart inherit the property setting. You cannot mix explicit and implicit ordering in the same Stateflow chart. However, you can mix charts with different ordering modes in the same Simulink® model.

Explicit Ordering of Parallel States

By default, a Stateflow chart orders parallel states explicitly based on execution priorities you set.

How Explicit Ordering Works

When you open a new Stateflow chart — or one that does not yet contain any parallel states — the chart automatically assigns priority numbers to parallel states in the order you create them. Numbering starts with the next available number within the parent container.

When you enable explicit ordering in a chart that contains implicitly ordered parallel states, the implicit priorities are preserved for the existing parallel states. When you add new parallel states, execution order is assigned in the same way as for new Stateflow charts — in order of creation.

You can reset execution order assignments at any time on a state-by-state basis, as described in Set Execution Order for Parallel States Individually. When you change execution order for a parallel state, the Stateflow chart automatically renumbers the other parallel states to preserve their relative execution order. For details, see Order Maintenance for Parallel States.

Order Parallel States Explicitly

To use explicit ordering for parallel states, perform these tasks:

  1. Enable Explicit Ordering at the Chart Level

  2. Set Execution Order for Parallel States Individually

Enable Explicit Ordering at the Chart Level.  To enable explicit ordering for parallel states, follow these steps:

  1. Right-click inside the top level of the chart and select Properties from the context menu.

    The Chart properties dialog box appears.

  2. Select the User specified state/transition execution order check box.

  3. Click OK.

    If your chart already contains parallel states that have been ordered implicitly, the existing priorities are preserved until you explicitly change them. When you add new parallel states in explicit mode, your chart automatically assigns priorities based on order of creation (see How Explicit Ordering Works). However you can now explicitly change execution order on a state-by-state basis, as described in Set Execution Order for Parallel States Individually.

Set Execution Order for Parallel States Individually.  In explicit ordering mode, you can change the execution order of individual parallel states. Right-click the parallel state of interest and select a new priority from the Execution Order menu.

Implicit Ordering of Parallel States

Rules of Implicit Ordering for Parallel States

In implicit ordering mode, a Stateflow chart orders parallel states implicitly based on location. Priority goes from top to bottom and then left to right, based on these rules:

  • The higher the vertical position of a parallel state in the chart, the higher the execution priority for that state.

  • Among parallel states with the same vertical position, the leftmost state receives highest priority.

The following example shows how these rules apply to top-level parallel states and parallel substates.

    Note:   Implicit ordering creates a dependency between design layout and execution priority. When you rearrange parallel states in your chart, you can accidentally change order of execution and affect simulation results. For more control over your designs, use the default explicit ordering mode to set execution priorities.

Order Parallel States Implicitly

To use implicit ordering for parallel states, follow these steps:

  1. Right-click inside the top level of the chart and select Properties from the context menu.

  2. In the Chart properties dialog box, clear the User specified state/transition execution order check box.

  3. Click OK.

Order Maintenance for Parallel States

Whether you use explicit or implicit ordering, a chart tries to reconcile execution priorities when you remove, renumber, or add parallel states. In these cases, a chart reprioritizes the parallel states to:

  • Fill in gaps in the sequence so that ordering is contiguous

  • Ensure that no two states have the same priority

  • Preserve the intended relative priority of execution

How a Chart Preserves Relative Priorities in Explicit Mode

For explicit ordering, a chart preserves the user-specified priorities. Consider this example of explicit ordering:

Because of explicit ordering, the priority of each state and substate matches the order of creation in the chart. The chart reprioritizes the parallel states and substates when you perform these actions:

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

  2. Add a top-level state g.

  3. Remove substate e.

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

How a Chart Preserves Relative Priorities in Implicit Mode

For implicit ordering, a chart preserves the intended relative priority based on geometry. Consider this example of implicit ordering:

If you remove top-level state b and substate e, the chart automatically reprioritizes the remaining parallel states and substates to preserve implicit geometric 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. In implicit ordering mode, a chart reassigns the execution priority based on where you restore the state. If you return the state to its original location in the chart, you restore its original priority.

However, in explicit ordering mode, 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.

Switching Between Explicit and Implicit Ordering

If you switch to implicit mode after explicitly ordering parallel states, the Stateflow chart resets execution order to follow implicit rules of geometry. However, if you switch from implicit to explicit mode, the chart does not restore the original explicit execution order.

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 explicit or implicit ordering rules that apply. 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 based on the prevailing explicit or implicit rules.

Was this topic helpful?