Stateflow    

Transitioning from State to State with Events Example

The following example shows the behavior of a simple transition focusing on the implications of whether states are active or inactive.

Processing of a First Event

Initially the Stateflow diagram is asleep. State On and state Off are OR states. State On is active. Event E_one occurs and awakens the Stateflow diagram. Event E_one is processed from the root of the Stateflow diagram down through the hierarchy of the Stateflow diagram:

  1. The Stateflow diagram root checks to see if there is a valid transition as a result of E_one. A valid transition from state On to state Off is detected.
  2. State On exit actions (ExitOn()) execute and complete.
  3. State On is marked inactive.
  4. The event E_one is broadcast as the transition action.
  1. This second event E_one is processed, but because neither state is active, it has no effect. Had a valid transition been possible as a result of the broadcast of E_one, the processing of the first broadcast of E_one would be preempted by the second broadcast of E_one. See Early Return Logic for Event Broadcasts.

  1. State Off is marked active.
  2. State Off entry actions (entOff()) execute and complete.
  3. The Stateflow diagram goes back to sleep, waiting to be awakened by another event.

This sequence completes the execution of the Stateflow diagram associated with event E_one when state On is initially active.

Processing of a Second Event

Using the same example, what happens when the next event, E_one, occurs while state Off is active?

Again, initially the Stateflow diagram is asleep. State Off is active. Event E_one occurs and awakens the Stateflow diagram. Event E_one is processed from the root of the Stateflow diagram down through the hierarchy of the Stateflow diagram with the following steps:

  1. The Stateflow diagram root checks to see if there is a valid transition as a result of E_one.
  1. A valid transition from state Off to state On is detected.

  1. State Off exit actions (exitOff()) execute and complete.
  2. State Off is marked inactive.
  3. State On is marked active.
  4. State On entry actions (entOn()) execute and complete.
  5. The Stateflow diagram goes back to sleep, waiting to be awakened by another event.

This sequence completes the execution of the Stateflow diagram associated with the second event E_one when state Off is initially active.

Processing of a Third Event

Using the same example, what happens when a third event, E_two, occurs?

Notice that the event E_two is not used explicitly in this example. However, its occurrence (or the occurrence of any event) does result in behavior. Initially, the Stateflow diagram is asleep and state On is active.

  1. Event E_two occurs and awakens the Stateflow diagram.
  1. Event E_two is processed from the root of the Stateflow diagram down through the hierarchy of the Stateflow diagram:

  1. The Stateflow diagram root checks to see if there is a valid transition as a result of E_two. There is none.
  2. State On during actions (durOn()) execute and complete.
  3. The Stateflow diagram goes back to sleep, waiting to be awakened by another event.

This sequence completes the execution of the Stateflow diagram associated with event E_two when state On is initially active.


  Label Format for a State-to-State Transition Example Transitioning from a Substate to a Substate with Events Example