Create a Hierarchy to Manage System Complexity
Add structure to your model one subcomponent at a time by creating a hierarchy of nested states. You can then control multiple levels of complexity in your Stateflow® chart. For more information, see Model Finite State Machines.
To create a hierarchy of states, place one or more states within the boundaries of another state. The inner states are child states (or substates) of the outer state. The outer state is the parent (or superstate) of the inner states.
The contents of a parent state behave like a smaller chart. When a parent state becomes active, one of its child states also becomes active. When the parent state becomes inactive, all of its child states become inactive.
Example of Hierarchy
This example models a stereo system consisting of an AM radio, an FM radio, and a CD player. During simulation, you control the stereo system by clicking buttons on the Media Player Helper user interface.
The stereo is initially in standby mode (OFF). When you select one of the Radio Request buttons, the stereo turns on the corresponding subcomponent. If you select the CD player, you can click one of the CD Request buttons to choose Play, Rewind, Fast-Forward, or Stop. You can insert or eject a disc at any point during the simulation.
Implement Behavior by Using State Hierarchy
Initially, the complete implementation of this stereo system appears rather complicated. However, by focusing on a single level of activity at a time, you can design the overall system design incrementally. For example, these conditions are necessary for the CD player to enter Fast-Forward play mode:
You turn on the stereo.
You turn on the CD player.
You playing a disc.
You click the FF button in the UI.
You can construct a hierarchical model that considers each of these conditions one at a time. For instance, the outermost level can define the transitions between the stereo turning on and off. The middle levels define the transition between the different stereo subcomponents, and between the stop and play modes of the CD player. The bottommost level defines the response to the CD Request buttons when you meet all the other conditions for playing a disc.
To implement the behavior of the stereo system,
the hierarchy of nested states listed by the Model Explorer under the
Manager chart. To open the Model Explorer, in the
Modeling tab, select Model Explorer.
This table lists the role of each state in the hierarchy.
|Top level (||Disc ejection mode (interrupts all other stereo functions)|
|Normal operating mode for stereo system|
Stereo system activity (child states of
|Stereo system is in standby mode (OFF)|
|Stereo system is active (ON)|
Stereo subcomponents (child states of
|CD player subcomponent is active|
|AM radio subcomponent is active|
|FM radio subcomponent is active|
CD player activity (child states of
|CD player is stopped|
|CD player is playing disc|
Disc play modes (child states of
|Normal play mode|
|Reverse play mode|
|Fast-Forward play mode|
This figure shows the complete layout of the states in the chart.
The history junctions inside the
ON states record the operating mode (Standby or ON) and the
active subcomponent of the media player (CD player, AM radio, or FM radio) before you
eject a disc or turn off the system.
Explore the Example
sf_mediaplayer contains two other Stateflow charts:
App Interfacemanages the interface with the UI and passes inputs to the
CD Playerreceives the output from the
Mode Managercharts and mimics the mechanical behavior of the CD player.
During simulation, you can investigate how each chart responds to interactions with the Media Player Helper app. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.