Enter a Chart or State

Chart and state entry occurs when:

  • A chart is activated for the first time. This is called chart initialization.

  • A valid transition into a state exists. See Evaluate Transitions.

Workflow for Entering a Chart or State

This flow chart shows the progression of events that Stateflow® takes for entry into a chart or a state. In this flow chart, the current state refers to the state in which a decision or a process is taking place.

Chart Entry

The first time that your Stateflow chart becomes active is called initialization. When initialization of your chart occurs, the chart is entered and Stateflow executes any default transitions for exclusive (OR) states. If the states at the top level of your chart are parallel (AND), they become active based on their order number.

If you want your chart to take any default transitions at time t = 0, in the Chart Properties dialog box, select the Execute (enter) Chart At Initialization check box. The default transition paths of the chart then execute during the model initialization phase.

State Entry

When a state is marked for entry, entry actions for a state execute. Once your chart is active and has gone through initialization, the top-level state becomes active. A state is marked for entry in one of these ways:

  • An incoming transition crosses state boundaries.

  • An incoming transition ends at the state boundary.

  • The state is a parallel state child of an active state.

Entry Actions

Entry actions are preceded by the prefix entry or en for short, followed by a required colon (:), and then followed by one or more actions. You separate multiple actions by using a carriage return, a semicolon (;), or a comma (,). If you do not specify the state action type explicitly for a statement, the chart treats that statement as an entry,during action.

Enter a Stateflow Chart

In this example, the first time the chart becomes active, chart initialization occurs.

By following the Workflow for Entering a Chart or State until the chart goes to sleep, the steps for chart initialization are in this order:

  1. The default transition actions are executed, and x = 0, y = 0, and z = 0.

  2. StateA is marked for entry.

  3. StateA is not a substate of an inactive parent. Perform the entry actions for StateA. Now x = 1.

  4. StateA is the state that was initially marked for entry.

  5. StateA does not contain any history junctions.

  6. There is a default transition to the substate, StateA1. Go to the Evaluate Transitions flow chart.

  7. By following the Evaluate Transitions flow chart, mark StateA1 for entry. Go to the Exit Actions flow chart.

  8. The current state, StateA, is a superstate of the destination state, StateA1. Return to the Entry Actions flow chart.

  9. StateA1 is not a substate of an inactive parent. Perform entry actions for StateA1. Now x = 3.

  10. StateA1 is the state that was initially marked for entry.

  11. StateA1 does not contain any history junctions.

  12. StateA1 does not contain any default transitions.

  13. StateA1 does not contain any single substates.

  14. The chart goes to sleep.

Steps 1 through 14 take place in the initial time step. This completes the chart initialization process.

Entering a State by Using History Junctions

If you want your Stateflow chart to remember and return to a substate that was previously active, regardless of a default transition, use a history junction. Placing a history junction within a state overrides the default transition leading to exclusive (OR) substates. After placing a history junction within a state, upon entry, your Stateflow chart remembers and enters the previously active substate. The history junction applies only to the level of the hierarchy in which it appears.

In this example, a light can be on or off. These options are indicated by the states Power_on and Power_off. The options are controlled by the input events switch_on and switch_off. When the light is on, it can be dim or bright. These options are indicated by the states Low and High and are controlled by the input events switch_low and switch_high.

Initially, the chart is asleep. The state Power_off is active. When the state Power_on was last active, High was the previously active substate. The event switch_on occurs and the state Power_on is marked for entry. At this time p = 0.

By following the Workflow for Entering a Chart or State until the chart goes to sleep, the execution steps for entering the state Power_on are in this order:

  1. Power_on is not the child of an inactive parent.

  2. There are no entry actions for Power_on.

  3. Power_on is the state that was initially marked for entry.

  4. There are history junctions in Power_on.

  5. High was the previously active substate. Now p = 2.

  6. High does not contain any history junctions.

  7. High does not contain any default transitions.

  8. High does not contain any single substates.

  9. The chart goes to sleep.

This completes the entry actions for Power_on and High.

Entering a State by Using Supertransitions

A supertransition is a transition between different levels in a chart. A supertransition can be between a state in a top-level chart and a state in one of its subcharts, or between states residing in different subcharts at the same or different levels in a chart. You can create supertransitions that span any number of levels in your chart.

When a state is entered through a supertransition, before the entry actions for the final destination are executed, its superstates must be marked active and their entry actions must be executed. In this example, StateB1 has been marked for entry from StateA2. At this point, x = 5, y = 5, and z = 1.

By following the Workflow for Entering a Chart or State until the chart goes to sleep, the execution steps for entering the state StateB1 are in this order:

  1. StateB1 is the substate of an inactive parent (StateB).

  2. StateB is marked as active.

  3. StateB is not the substate of an inactive parent.

  4. Perform the entry actions for StateB. Now x = 4.

  5. StateB is not the state that was initially marked for entry.

  6. Perform the entry actions for StateB1. Now x = 3.

  7. StateB1 is the state that was initially marked for entry.

  8. StateB1 has no history junctions.

  9. StateB1 does not contain any default transitions.

  10. StateB1 does not contain any single substates.

  11. The chart goes to sleep.

This completes the entry actions for StateB and StateB1.

Related Topics