Main Content

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$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, theInitstate initializes the chart data:

  • The local datanis set to the value of the inputu.

  • The local datan2is set to the remainder whennis divided by two.

  • 输出数据yis set tofalse.

Depending on the parity of the input, the chart transitions to either theEvenorOddstate. As the state activity shifts between theEvenandOddstates, the chart computes the numbers in the hailstone sequence. When the sequence reaches a value of one, the output dataybecomestrueand 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, andexitactions.

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,duringoccurs 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;

WhenInitbecomes active at the start of the simulation, determines the parity ofnand setsytofalse.

exit: y = isequal(n,1);

When transitioning out ofInitafter one time step, determines whethernis equal to one.
Even

entry,during: n = n/2; n2 = rem(n,2);

Computes the next number of the hailstone sequence (n/ 2) and updates its parity on:

  • The time step whenEvenfirst becomes active.

  • Every subsequent time step thatEvenis active.

Odd

entry,during: n = 3*(n-y)+1; n2 = rem(n,2);

Computes the next number of the hailstone sequence (3n+1) and updates its parity on:

  • The time step whenOddfirst becomes active.

  • Every subsequent time step thatOddis active.

Throughout most of the simulation,yevaluates to zero. On the last time step, whenn= 1,yevaluates to one so this action does not modifynorn2before the simulation stops.

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}

conditionis 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_actionis an instruction that executes when the condition guarding the transition is true. The conditional action takes place after the condition but before anyexitorentrystate 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 valueuto the local datan.
Transition fromInittoEven

n2 == 0

Condition Whennis even, transition occurs. The number 1 at the source of this transition indicates that it is evaluated before the transition toOdd.
Transition fromInittoOdd None Whennis odd, transition occurs. The number 2 at the source of this transition indicates that it is evaluated after the transition toEven.
Transition fromOddtoEven

n2 == 0

Condition Whennis even, transition occurs.
Transition fromEventoOdd

n2 ~= 0

Condition Whennis odd, transition occurs.

y = isequal(n,1)

Conditional action When transition occurs, determines whethernis equal to one.

Examine Chart Behavior

Suppose that you want to compute the hailstone sequence starting with a value of nine.

  1. In theConstant(Simulink)block, enter a value of9.

  2. In theSimulationtab, clickRun.

    The chart responds with these actions:

    • At timet= 0, the default transition toInitoccurs.

      • The transition action sets the value ofnto 9.

      • TheInitstate becomes active.

      • Theentry行动Initsetn2to 1 andytofalse.

    • At timet= 1, the conditionn2 == 0is false so the chart prepares to transition toOdd.

      • Theexitaction inInitsetsytofalse.

      • TheInitstate becomes inactive.

      • TheOddstate becomes active.

      • Theentry行动Oddsetnto 28 andn2to 0.

    • At timet= 2, the conditionn2 == 0is true so the chart prepares to transition toEven.

      • TheOddstate becomes inactive.

      • TheEvenstate becomes active.

      • Theentry行动Evensetnto 14 andn2to 0.

    • At timet= 3, the conditionn2 ~= 0is false so the chart does not take a transition.

      • TheEvenstate remains active.

      • Theduring行动Evensetnto 7 andn2to 1.

    • At timet= 4, the conditionn2 ~= 0is true so the chart prepares to transition toOdd.

      • The transition action setsytofalse.

      • TheEvenstate becomes inactive.

      • TheOddstate becomes active.

      • Theentry行动Oddsetnto 22 andn2to 0.

    • The chart continues to compute the hailstone sequence until it arrives at a value ofn= 1at timet= 19.

    • At timet= 20, the chart prepares to transition fromEventoOdd.

      • Before theEvenstate becomes inactive, the transition action setsytotrue.

      • TheOddstate becomes active.

      • Theentry行动Odddo not modifynorn2.

      • TheStop Simulation(Simulink)block connected to the output signalystops the simulation.

  3. In theSimulationtab, underReview Results,点击Data Inspector.

  4. To see the values of the hailstone sequence, in the Simulation Data Inspector, select the logged signaln.

Simulation Data Inspector showing the output of the chart.

The hailstone sequence reaches a value of one after 19 iterations.

Related Topics