Define Chart Behavior by Using Actions
State and transition actionsare instructions that you write inside a state or next to a transition to define how a Stateflow®chart behaves during simulation. For more information, seeModel Finite State Machines.
Example of State and Transition Actions
The actions in this chart define a state machine that empirically verifies one instance of the Collatz conjecture. For a given numeric input, the chart computes the hailstone sequence… by iterating this rule:
Ifis even, then.
Ifis odd, then.
The Collatz conjecture states that every positive integer has a hailstone sequence that eventually reaches one.
The chart consists of three states. At the start of simulation, theInit
state initializes the chart data:
The local data
n
is set to the value of the inputu
.The local data
n2
is set to the remainder whenn
is divided by two.输出数据
y
is set tofalse
.
Depending on the parity of the input, the chart transitions to either theEven
orOdd
state. As the state activity shifts between theEven
andOdd
states, the chart computes the numbers in the hailstone sequence. When the sequence reaches a value of one, the output datay
becomestrue
and triggers aStop Simulation(Simulink)block in the Simulink® model.
State Action Types
State actions define what a Stateflow chart does while a state is active. The most common types of state actions areentry
,during
, andexit
actions.
Type of State Action | Abbreviation | Description |
---|---|---|
entry |
en |
Action occurs on a time step when the state becomes active. |
during |
du |
Action occurs on a time step when the state is already active and the chart does not transition out of the state. |
exit |
ex |
Action occurs on a time step when the chart transitions out of the state. |
You can specify the type of a state action by its complete keyword (entry
,during
,exit
) or by its abbreviation (en
,du
,ex
). You can also combine state action types by using commas. For instance, an action with the combined typeentry,during
occurs on the time step when the state becomes active and on every subsequent time step while the state remains active.
This table lists the result of each state action in the hailstone chart.
State | Action | Result |
---|---|---|
Init |
entry: n2 = rem(n,2); y = false; |
WhenInit becomes active at the start of the simulation, determines the parity ofn and setsy tofalse . |
exit: y = isequal(n,1); |
When transitioning out ofInit after one time step, determines whethern is equal to one. |
|
Even |
entry,during: n = n/2; n2 = rem(n,2); |
Computes the next number of the hailstone sequence (
|
Odd |
entry,during: n = 3*(n-y)+1; n2 = rem(n,2); |
Computes the next number of the hailstone sequence (3
Throughout most of the simulation, |
Transition Action Types
Transition actions define what a Stateflow chart does when a transition leads away from an active state. The most common types of transition actions are conditions and conditional actions. To specify transition actions, use a label with this syntax:
[condition]{conditional_action}
condition
is a Boolean expression that determines whether the transition occurs. If you do not specify a condition, an implied condition evaluating to true is assumed.
conditional_action
is an instruction that executes when the condition guarding the transition is true. The conditional action takes place after the condition but before anyexit
orentry
state actions.
This table lists the result of each transition action in the hailstone chart.
Transition | Action | Action Type | Result |
---|---|---|---|
Default transition intoInit |
n = u |
Conditional action | At the start of the simulation, assigns the input valueu to the local datan . |
Transition fromInit toEven |
n2 == 0 |
Condition | Whenn is even, transition occurs. The number 1 at the source of this transition indicates that it is evaluated before the transition toOdd . |
Transition fromInit toOdd |
None | Whenn is odd, transition occurs. The number 2 at the source of this transition indicates that it is evaluated after the transition toEven . |
|
Transition fromOdd toEven |
n2 == 0 |
Condition | Whenn is even, transition occurs. |
Transition fromEven toOdd |
n2 ~= 0 |
Condition | Whenn is odd, transition occurs. |
y = isequal(n,1) |
Conditional action | When transition occurs, determines whethern is equal to one. |
Examine Chart Behavior
Suppose that you want to compute the hailstone sequence starting with a value of nine.
In theConstant(Simulink)block, enter a value of
9
.In theSimulationtab, clickRun.
The chart responds with these actions:
At timet= 0, the default transition to
Init
occurs.The transition action sets the value of
n
to 9.The
Init
state becomes active.The
entry
行动Init
setn2
to 1 andy
tofalse
.
At timet= 1, the condition
n2 == 0
is false so the chart prepares to transition toOdd
.The
exit
action inInit
setsy
tofalse
.The
Init
state becomes inactive.The
Odd
state becomes active.The
entry
行动Odd
setn
to 28 andn2
to 0.
At timet= 2, the condition
n2 == 0
is true so the chart prepares to transition toEven
.The
Odd
state becomes inactive.The
Even
state becomes active.The
entry
行动Even
setn
to 14 andn2
to 0.
At timet= 3, the condition
n2 ~= 0
is false so the chart does not take a transition.The
Even
state remains active.The
during
行动Even
setn
to 7 andn2
to 1.
At timet= 4, the condition
n2 ~= 0
is true so the chart prepares to transition toOdd
.The transition action sets
y
tofalse
.The
Even
state becomes inactive.The
Odd
state becomes active.The
entry
行动Odd
setn
to 22 andn2
to 0.
The chart continues to compute the hailstone sequence until it arrives at a value of
n
= 1at timet= 19.At timet= 20, the chart prepares to transition from
Even
toOdd
.Before the
Even
state becomes inactive, the transition action setsy
totrue
.The
Odd
state becomes active.The
entry
行动Odd
do not modifyn
orn2
.TheStop Simulation(Simulink)block connected to the output signal
y
stops the simulation.
In theSimulationtab, underReview Results,点击Data Inspector.
To see the values of the hailstone sequence, in the Simulation Data Inspector, select the logged signal
n
.
The hailstone sequence reaches a value of one after 19 iterations.