Define Chart Behavior by Using State Actions and Transition Labels
State actions and transition actions are instructions that you write inside a state or on a transition, respectively, to define how a Stateflow® chart behaves during simulation. For example, 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:
If is even, then .
If is 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, the Init
state initializes the chart data by setting:
Local data
n
to the value of the inputu
.Local data
n2
to the remainder whenn
is divided by two.Output data
y
tofalse
.
Depending on the parity of the input, the chart transitions to either the Even
or Odd
state. As the state activity shifts between the Even
and Odd
states, the chart computes the numbers in the hailstone sequence. When the sequence reaches a value of one, the output data y
becomes true
and triggers a Stop 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 are entry
, during
, and exit
actions:
entry
actions occur when the state becomes active.during
actions occur on a time step when the state is already active and the chart does not transition out of the state.exit
actions occur when the chart transitions out of the state.
You can specify the type of a state action by using a complete keyword (entry
, during
, exit
) or an abbreviation (en
, du
, ex
). You can also combine state action types by using commas. For instance, an action with the combined type entry, during
occurs on the time step when the state becomes active and on every subsequent time step while the state remains active.
The hailstone chart contains actions in these states:
Init
— When this state becomes active at the start of the simulation, theentry
action determines the parity ofn
and setsy
tofalse
. When the chart transitions out ofInit
after one time step, theexit
action determines whethern
is equal to one.Even
— When this state becomes active, and on every subsequent time step that the state is active, the combinedentry, during
action computes the value and parity for next number of the hailstone sequence,n/2
.Odd
— When this state becomes active, and on every subsequent time step that the state is active, the combinedentry, during
action checks whethern
is greater than one and, if it is, computes the value and parity for next number of the hailstone sequence,3*n+1
.
Transition Label Types
Transition labels define what a Stateflow chart does when the active state changes. The most common types of transition labels are conditions and condition actions.
[Condition]{ConditionAction}
Condition
is a Boolean expression that determines whether the transition occurs. If you do not specify a condition, the transition occurs one time step after the source state becomes active.
ConditionAction
is an instruction that executes when the condition that guards the transition is true. The condition action takes place after the condition but before any exit
or entry
state actions.
The hailstone chart contains actions on these transitions:
Default transition into
Init
— At the start of the simulation, the condition actionn = u
assigns the input valueu
to the local datan
.Transition from
Init
toEven
— The conditionn2 == 0
determines that the transition occurs whenn
is even. The number 1 at the source of this transition indicates that this transition is evaluated before the transitionInit
toOdd
.Transition from
Odd
toEven
— The conditionn2 == 0
determines that the transition occurs whenn
is even.Transition from
Even
toOdd
— The conditionn2 ~= 0
determines that the transition occurs whenn
is odd. In this case, the condition actiony = isequal(n,1)
determines whethern
is equal to one.
Examine Chart Behavior
To compute the hailstone sequence starting with a value of nine:
1. In the Constant block, enter a value of 9
.
2. In the Simulation tab, click Run. The chart responds with these actions:
At time , the default transition to
Init
occurs. The transition action sets the value ofn
to 9. The stateInit
becomes active. Theentry
actions inInit
setn2
to 1 andy
tofalse
.At time , the condition
n2 == 0
is false so the chart prepares to transition toOdd
. Theexit
action inInit
setsy
tofalse
. The stateInit
becomes inactive and the stateOdd
becomes active. Theentry
action inOdd
setsn
to 28 andn2
to 0.At time , the condition
n2 == 0
is true so the chart prepares to transition toEven
. The stateOdd
becomes inactive and the stateEven
becomes active. The entry action inEven
setsn
to 14 andn2
to 0.At time , the condition
n2 ~= 0
is false so the chart does not take a transition. The stateEven
remains active. Theduring
action inEven
setsn
to 7 andn2
to 1.At time , the condition
n2 ~= 0
is true so the chart prepares to transition toOdd
. The transition action setsy
to false. The stateEven
becomes inactive and the stateOdd
becomes active. Theentry
actions inOdd
setn
to 22 andn2
to 0.The chart continues to compute the hailstone sequence until it arrives at a value of
n
at time .At time , the chart prepares to transition from
Even
toOdd
. The transition action setsy
totrue
. The stateEven
becomes inactive and the stateOdd
becomes active. Theentry
actions inOdd
do not modifyn
orn2
. The Stop Simulation block connected to the output signaly
stops the simulation.
3. In the Simulation tab, under Review Results, click Data Inspector.
4. 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.