Main Content

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 $u$, the chart computes the hailstone sequence $n_0 = u,$ $n_1,$ $n_2,$ $n_3,$ … by iterating this rule:

  • If $n_i$ is even, then $n_{i+1} = n_i / 2$.

  • If $n_i$ is odd, then $n_{i+1} = 3n_i+1$.

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 input u.

  • Local data n2 to the remainder when n is divided by two.

  • Output data y to false.

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, the entry action determines the parity of n and sets y to false. When the chart transitions out of Init after one time step, the exit action determines whether n is equal to one.

  • Even — When this state becomes active, and on every subsequent time step that the state is active, the combined entry, 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 combined entry, during action checks whether n 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 action n = u assigns the input value u to the local data n.

  • Transition from Init to Even — The condition n2 == 0 determines that the transition occurs when n is even. The number 1 at the source of this transition indicates that this transition is evaluated before the transition Init to Odd.

  • Transition from Odd to Even — The condition n2 == 0 determines that the transition occurs when n is even.

  • Transition from Even to Odd — The condition n2 ~= 0 determines that the transition occurs when n is odd. In this case, the condition action y = isequal(n,1) determines whether n 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 $t = 0$, the default transition to Init occurs. The transition action sets the value of n to 9. The state Init becomes active. The entry actions in Init set n2 to 1 and y to false.

  • At time $t = 1$, the condition n2 == 0 is false so the chart prepares to transition to Odd. The exit action in Init sets y to false. The state Init becomes inactive and the state Odd becomes active. The entry action in Odd sets n to 28 and n2 to 0.

  • At time $t = 2$, the condition n2 == 0 is true so the chart prepares to transition to Even. The state Odd becomes inactive and the state Even becomes active. The entry action in Even sets n to 14 and n2 to 0.

  • At time $t = 3$, the condition n2 ~= 0 is false so the chart does not take a transition. The state Even remains active. The during action in Even sets n to 7 and n2 to 1.

  • At time $t = 4$, the condition n2 ~= 0 is true so the chart prepares to transition to Odd. The transition action sets y to false. The state Even becomes inactive and the state Odd becomes active. The entry actions in Odd set n to 22 and n2 to 0.

  • The chart continues to compute the hailstone sequence until it arrives at a value of n $= 1$ at time $t = 19$.

  • At time $t = 20$, the chart prepares to transition from Even to Odd. The transition action sets y to true. The state Even becomes inactive and the state Odd becomes active. The entry actions in Odd do not modify n or n2. The Stop Simulation block connected to the output signal y 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.

Related Topics