A state describes an operating mode of a reactive system. In a Stateflow® chart, states are used for sequential design to create state transition diagrams.
States can be active or inactive. The activity or inactivity of a state can change depending on events and conditions. The occurrence of an event drives the execution of the state transition diagram by making states become active or inactive. At any point during execution, active and inactive states exist.
To manage multilevel state complexity, use hierarchy in your Stateflow chart. With hierarchy, you can represent multiple levels of subcomponents in a system.
In the following example, three levels of hierarchy appear in the chart. Drawing one state within the boundaries of another state indicates that the inner state is a substate (or child) of the outer state (or superstate). The outer state is the parent of the inner state.
In this example, the chart is the parent of the state Car_done
. The
state Car_done
is the parent state of the Car_made
and Car_shipped
states. The state Car_made
is also
the parent of the Parts_assembled
and Painted
states. You can also say that the states Parts_assembled
and
Painted
are children of the Car_made
state.
To represent the Stateflow hierarchy textually, use a slash character (/)
to
represent the chart and use a period (.)
to separate each level in the
hierarchy of states. The following list is a textual representation of the hierarchy of
objects in the preceding example:
/Car_done
/Car_done
.Car_made
/Car_done
.Car_shipped
/Car_done
.Car_made
.Parts_assembled
/Car_done
.Car_made
.Painted
States can contain all other Stateflow objects. Stateflow chart notation supports the representation of graphical object hierarchy in Stateflow charts with containment. A state is a superstate if it contains other states. A state is a substate if it is contained by another state. A state that is neither a superstate nor a substate of another state is a state whose parent is the Stateflow chart itself.
States can also contain nongraphical data, event, and message objects. The hierarchy of this containment appears in the Model Explorer. You define data, event, and message containment by specifying the parent object.
Every state (or chart) has a decomposition that dictates what type of substates the state (or chart) can contain. All substates of a superstate must be of the same type as the superstate decomposition. State decomposition can be exclusive (OR) or parallel (AND).
Substates with solid borders indicate exclusive (OR) state decomposition. Use this decomposition to describe operating modes that are mutually exclusive. When a state has exclusive (OR) decomposition, only one substate can be active at a time.
In the following example, either state A
or state
B
can be active. If state A
is active, either
state A1
or state A2
can be active at a given
time.
Substates with dashed borders indicate parallel (AND) decomposition. Use this decomposition to describe concurrent operating modes. When a state has parallel (AND) decomposition, all substates are active at the same time.
In the following example, when state A
is active,
A1
and A2
are both active at the same time.
The activity within parallel states is essentially independent, as demonstrated in the following example.
In the following example, when state A
becomes active, both states
B
and C
become active at the same time. When state
C
becomes active, either state C1
or state
C2
can be active.
The label for a state appears on the top left corner of the state rectangle with the following general format:
name/ entry:entry actions during:during actions exit:exit actions on event_name:on event_name actions on message_name:on message_name actions bind:events
The following example demonstrates the components of a state label.
Each action in the state label appears in the subtopics that follow. For more information on state actions, see Execution of a Stateflow Chart.
A state label starts with the name of the state followed by an optional
/
character. In the preceding example, the state names are
On
and Off
. Valid state names consist of
alphanumeric characters and can include the underscore (_
) character. For
more information, see Guidelines for Naming Stateflow Objects.
Hierarchy provides some flexibility in naming states. The name that you enter on the state label must be unique when preceded by ancestor states. The name in the Stateflow hierarchy is the text you enter as the label on the state, preceded by the names of parent states separated by periods. Each state can have the same name appear in the label, as long as their full names within the hierarchy are unique.
The following example shows how unique naming of states works.
Each of these states has a unique name because of its location in the chart. The full
names for the states in FAN1
and FAN2
are:
PowerOn.FAN1.On
PowerOn.FAN1.Off
PowerOn.FAN2.On
PowerOn.FAN2.Off
After the name, you enter optional action statements for the state with a keyword label that identifies the type of action. You can specify none, some, or all of them. The colon after each keyword is required. The slash following the state name is optional as long as it is followed by a carriage return.
For each type of action, you can enter more than one action by separating each action
with a carriage return, semicolon, or a comma. You can specify actions for more than one
event or message by adding additional on
event_name
or on
message_name
lines.
If you enter the name and slash followed directly by actions, the actions are
interpreted as entry
action(s). This shorthand is useful if you are
specifying only entry
actions.
This table summarizes the different state action types.
State Action | Abbreviation | Description |
---|---|---|
entry | en | Executes when the state becomes active. |
exit | ex | Executes when the state is active and a transition out of the state occurs. |
during | du | Executes when the state is active and a specific event occurs. |
bind | none | Binds an event or data object so that only that state and its children can broadcast the event or change the data value. |
| none | Executes when the state is active and it receives a broadcast of
event_name . |
| none | Executes when a message message_name is
available. |
| none | Executes when:
For more information, see |
| none | Executes when:
For more information, see |
| none | Executes when:
For more information, see |
| none | Executes:
For more information, see |
entry
ActionsEntry actions are executed when a state becomes active. Entry actions consist
of the prefix entry
(or the abbreviation en
)
followed by a colon (:
) and one or more actions. To separate multiple
entry actions, use semicolons or commas. You can also enter the actions on separate
lines.
In the preceding example, the entry action id = x+y
executes when
the chart takes the default transition and state A becomes active. See Enter a Chart or State.
exit
ActionsExit actions are executed when a state is active and a transition out of the state
occurs. Exit actions consist of the prefix exit
(or the abbreviation
ex
) followed by a colon (:
) and one or more
actions. To separate multiple exit actions, use semicolons or commas. You can also enter
the actions on separate lines.
In the preceding example, the exit action time_out
executes when
the chart takes one of the transitions from state A to state B or C. See Exit a State.
during
ActionsDuring actions are executed when a state is active, an event occurs, and no valid
transition to another state or the current state is available. During actions consist of
the prefix during
(or the abbreviation du
) followed
by a colon (:
) and one or more actions. To separate multiple during
actions, use semicolons or commas. You can also enter the actions on separate
lines.
In the preceding example, the during action switch_on()
executes
whenever the state C is active because there are no valid transitions to another state.
See Execution of a Stateflow Chart.
bind
ActionsYou can bind the data and events to a state by using a bind
action.
A bind
action consists of the prefix bind
followed
by a colon (:
) and one or more events or data. To separate multiple
events and data, use semicolons or commas. You can also enter the events and data on
separate lines.
Only a state and its children can change data or broadcast events bound to that state. Other states can read the bound data or listen for the bound event, but they cannot change the bound data or send the bound events.
Bind actions apply to a chart whether the binding state is active or not. In the
preceding example, the bind action bind: id, time_out
for state
A
binds the data id
and the event
time_out
to state A
. This binding prevents any
other state (or its children) in the chart from changing id
or
broadcasting event time_out
.
If a state includes actions that change data or broadcast events that bind to another state, a compile-time error occurs. For example, this chart contains two state actions that produce errors.
State Action | Reason for Error |
---|---|
bind: id in state B | Only one state can change the data id , which is bound to
state A |
entry: time_out in state C | Only one state can broadcast the event time_out , which is
bound to state A |
Binding a function-call event to a state also binds the function-call subsystem that it calls. The function-call subsystem is enabled when the binding state is entered and disabled when the binding state is exited. For more information about this behavior, see Control Function-Call Subsystems by Using bind Actions.
Bind actions are supported only in Stateflow charts in Simulink® models.
on
ActionsOn actions are executed when the state is active and it receives an event or message.
On actions consist of the prefix on
followed by a unique event
event_name
or message
message_name
, a colon (:
), and one or more
actions. To separate multiple on actions, use semicolons or commas. You can also enter the
actions on separate lines.
You can specify actions for more than one event or message. For example, if you want
different events to trigger different actions, enter multiple on
action
statements in the state action
label:
on ev1: action1(); on ev2: action2();
ev1
and ev2
occur at the same time, then
action1()
executes first and action2()
executes
second. See Execution of a Stateflow Chart.