主要内容

Create a Hierarchy to Manage System Complexity

Add structure to your model one subcomponent at a time by creating ahierarchyof nested states. You can then control multiple levels of complexity in your Stateflow®chart. For more information, seeModel Finite State Machines.

State Hierarchy

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.

Stateflow chart with a hierarchy of states. The outer state is called Parent. It contains two inner states called Child1 and Child2.

父状态的内容表现得更小的图表。当父状态变为活动状态时,其中一个子状态也变为活动状态。当父状态变为非活动状态时,所有子状态都变为非活动状态。

Example of Hierarchy

此示例模拟了由AM收音机,FM收音机和CD播放器组成的立体声系统。在仿真过程中,您可以通过单击媒体播放器辅助用户界面上的按钮来控制立体声系统。

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.

使用状态层次结构实现行为

最初,此立体声系统的完整实现显示相当复杂。但是,通过一次关注单一的活动,您可以逐步设计整个系统设计。例如,CD播放器进入快进播放模式所需的这些条件是必要的:

  1. You turn on the stereo.

  2. 你打开CD播放器。

  3. You playing a disc.

  4. 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.

实现立体声系统的行为,sf_cdplayeruses the hierarchy of nested states listed by the Model Explorer under the Media Player Mode Manager chart. To open the Model Explorer, in theModelingtab, selectModel Explorer.

嵌套状态的层次结构,如模型资源管理器所列。

This table lists the role of each state in the hierarchy.

层次结构级别 State 描述
Top level (Media Player Mode Manager chart) ModeManager 立体声系统的正常操作模式
喷射 Disc ejection mode (interrupts all other stereo functions)

立体声系统活动(儿童国家ModeManager)

Standby 立体声系统处于待机模式(关闭)
Stereo system is active (ON)

Stereo subcomponents (child states of)

AMMode AM radio subcomponent is active
FMMode FM收音机子组件处于活动状态
cdmode CD player subcomponent is active

CD播放器活动(儿童国家cdmode)

停止 CD播放器停止了
CD播放器正在播放光盘

光盘播放模式(儿童状态)

Normal Normal play mode
Rew Reverse play mode
快速 快进播放模式

This figure shows the complete layout of the states in the chart.

状态在媒体播放器模式管理器图表中的排列。

使用子图文简化图表外观

You can simplify the overall appearance of a chart with a complex hierarchy by hiding low-level details inside subcharts, which appear as opaque boxes. The use of subcharts does not change the behavior of the chart. For instance, insf_cdplayer, the stereo subcomponentsAMMode,FMMode,和cdmode实施为子图文。打开媒体播放器模式管理器图表时,您只能看到状态层次结构的三个级别。要查看其中一个子剖面内的详细信息,请双击子片。

Subcharts appear as opaque boxes to hide the low-level details of the chart.

Explore the Example

这个例子sf_cdplayer包含两个其他州流图:

  • 用户请求管理与UI的接口,并将输入传递给媒体播放器模式管理器和CD播放器行为模型图表。

  • CD Player Behavior Model receives the output from the User Request and Media Player Mode Manager charts and mimics the mechanical behavior of the CD player.

Simulink model that simulates a media player. The model contains the chart Media Player Mode Manager and two additional charts, User Request and CD Player Behavior Model.

During simulation, you can investigate how each chart responds to interactions with the Media Player Helper. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.

Related Examples

More About