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:
Right-click the parallel state.
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:
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:
Change the priority of top-level state
b
to 3.Add a top-level state
g
.Remove substate
e
.
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 state | Using the undo command | The original priority is restored. |
Dragging outside the boundaries of the parent state or so its boundaries overlap the parent state and releasing the mouse button | Dragging it back into the parent state | The 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 button | Dragging it back into the parent state | The original priority is restored. |
Dragging so its boundaries overlap one or more sibling states | Dragging it to a location with no overlapping boundaries inside the same parent state | The original priority is restored. |
Cutting | Pasting | The 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.