Main Content

Represent Multiple Paths by Using Connective Junctions

The label format for a transition segment entering a junction is the same as for transitions entering states, as shown in this image:

Stateflow chart with states called S1, S2, and S3.

In this image, the execution order is:

  1. When an event occurs, the chart checks state S1 for an outgoing transition that matches the specified event.

  2. If the chart finds a transition with a matching event, the chart evaluates the transition.

  3. If condition_1 evaluates to true, the chart executes the condition action condition_action.

  4. The chart checks the outgoing transitions from the junction for a valid transition. Because condition_2 is true, a valid state-to-state transition exists from S1 to S2.

  5. The state S1 exit actions execute and complete.

  6. State S1 becomes inactive.

  7. The transition action transition_action executes and completes.

  8. The completed state-to-state transition from S1 to S2 occurs.

  9. State S2 becomes active.

  10. The state S2 entry action executes and completes.

If-Then-Else Decision Construct

This image shows the behavior of an if-then-else decision construct.

Stateflow chart with states called A, B, C, and D.

Initially, the chart is asleep. State A is active and condition [C_two] is true. Event E_one occurs and wakes the chart, which processes the event from the root through the hierarchy:

  1. The chart root checks to see if there is a valid transition as a result of E_one.

    A valid transition segment from state A to the connective junction exists. The chart evaluates the transition segments according to their labeled order. The first transition segment, condition [C_one], is not valid. The next transition segment, condition [C_two], is valid. The complete transition from state A to state C is valid.

  2. The state A exit action exitA() executes and completes.

  3. State A becomes inactive.

  4. State C becomes active.

  5. The state C entry action entC() executes and completes.

  6. The chart goes to sleep.

Self-Loop Transition

This image shows the behavior of a self-loop transition that uses a connective junction.

Stateflow chart with states called A and B.

Initially, the chart is asleep. State A is active and condition [C_one] is false. Event E_one occurs and wakes the chart, which processes the event from the root down through the hierarchy:

  1. The chart root checks if there is a valid transition as a result of E_one. A valid transition segment from state A to the connective junction exists. The chart evaluates the transition segments according to their labeled order. Because the condition [C_one] is not valid, the complete transition from state A to state B is not valid. The transition segment from the connective junction back to state A is valid.

  2. The state A exit actions exitA() executes and completes.

  3. State A becomes inactive.

  4. The transition action A_two executes and completes.

  5. State A becomes active.

  6. The state A entry actions entA() executes and completes.

  7. The chart goes to sleep.

For-Loop Construct

This example shows the behavior of a for-loop that uses a connective junction.

Stateflow chart with states called A and B.

Initially, the chart is asleep. State A is active. Event E_one occurs and awakens the chart, which processes the event from the root down through the hierarchy:

  1. The chart root checks if there is a valid transition as a result of E_one. There is a valid transition segment from state A to the connective junction. The transition segment condition action, i = 0, executes and completes. Of the two transition segments that leave the connective junction, the chart evaluates the transition segment that is a self-loop back to the connective junction next. That segment takes priority in evaluation because it has a condition, whereas the other segment is unlabeled. This evaluation behavior reflects the ordering of outgoing transitions in the chart.

  2. The condition [i < 10] evaluates as true. The condition action i++ and the call to func1 execute and complete until the condition becomes false. Because a connective junction is not a final destination, the transition destination is still unknown.

  3. The unconditional segment to state B is now valid. The complete transition from state A to state B is valid.

  4. The state A exit action exitA() executes and completes.

  5. State A becomes inactive.

  6. State B becomes active.

  7. The state B entry action entB() executes and completes.

  8. The chart goes back to sleep.

Flow Chart Notation

This image shows the behavior of a Stateflow® chart that uses flow chart notation.

Chart with an inner transition path to three substates.

Initially, the chart is asleep. State A.A1 is active and condition [C_one()] is true. Event E_one occurs and wakes the chart, which processes the event from the root through the hierarchy:

  1. The chart root checks if there is a valid transition as a result of E_one. There is no valid transition.

  2. State A checks itself for valid transitions and detects a valid inner transition to a connective junction.

  3. The chart evaluates next possible segments of the transition. Only one outgoing transition exists, and it has a condition action. The condition action executes and completes.

  4. The chart evaluates the next possible segments. Two outgoing transitions exist: a conditional self-loop transition and an unconditional transition segment. Because of the assigned evaluation order, the conditional transition segment takes precedence. Because the condition [C_one()] is true, the chart takes the self-loop transition. Because a final transition destination has not been reached, this self-loop continues until [C_one()] is false.

    Assume that after five iterations, [C_one()] is false.

  5. The chart evaluates the next possible transition segment. This transition is an unconditional transition segment with a condition action. The chart takes the transition segment and the condition action, {d=my_func()}, executes and completes. The returned value of d is 84.

  6. The chart evaluates the next possible transition segment. Three outgoing transition segments exist: two conditional and one unconditional. Because of the assigned evaluation order, the segment with the condition [d < 100] evaluates first based on the geometry of the two outgoing conditional transition segments. Because the returned value of d is 84, the condition [d < 100] is true and this transition to the destination state A.A1 is valid.

  7. The state A.A1 exit action exitA1() executes and completes.

  8. State A.A1 becomes inactive.

  9. State A.A1 becomes active.

  10. The state A.A1 entry action entA1() executes and completes.

  11. The chart goes to sleep.

Transition from a Common Source to Multiple Destinations

This image shows the behavior of transitions from a common source to multiple conditional destinations using a connective junction.

Stateflow chart with states called A, B and C.

Initially, the chart is asleep. State A is active. Event E_two occurs and wakes the chart, which processes the event from the root through the hierarchy:

  1. The chart root checks to see if there is a valid transition as a result of E_two. A valid transition segment exists from state A to the connective junction. The chart evalutes the segments according to their assigned order. The first transition segment, E_one, is not valid. The next transition segment, E_two, is valid. The complete transition from state A to state C is valid.

  2. The state A exit action exitA() executes and completes.

  3. State A is marked inactive.

  4. State C is marked active.

  5. The state C entry action entC() executes and completes.

  6. The chart goes to sleep.

Resolve Equally Valid Transition Paths

A chart contains conflicting transitions when it has two equally valid paths from the same source. In the case of a conflict, the chart evaluates the equally valid transitions based on the order you specify for each transition.

For example, this chart has two equally valid transition paths.

Stateflow chart with states called A, B and C.

The default transition sets the value of data a to 1 and the value of data b to 10. The during action of state A increments a and decrements b during each time step. The transition from state A to state B is valid if the condition [a > 4] is true. The transition from state A to state C is valid if the condition [b < 7] is true. During simulation, there is a time step where state A is active and both conditions are true. This causes a transition conflict.

By default, the chart executes conditions in the order you created them. However, you can also manually change the execution order. For example, if you right-click the transition from state A to state C and select Execution Order > 1 from the context menu, the chart evaluates that transition first. In this case, the transition from state A to state C occurs.

Transition from Multiple Sources to a Common Destination

This image shows the behavior of transitions from multiple sources to a single destination using a connective junction.

Stateflow chart with states called A, B and C.

Initially, the chart is asleep. State A is active. Event E_one occurs and wakes the chart, which processes the event from the root through the hierarchy:

  1. The chart root checks if there is a valid transition as a result of E_one. A valid transition segment exists from state A to the connective junction and from the junction to state C.

  2. The state A exit action exitA() executes and completes.

  3. State A becomes inactive.

  4. State C becomes active.

  5. The state C entry action entC() executes and completes.

  6. The chart goes to sleep.

Transition from a Source to a Destination Based on a Common Event

This image shows the behavior of transitions from multiple sources to a single destination using a connective junction. The transition segment located after the junction contains an event.

Stateflow chart with states called A, B and C.

Initially, the chart is asleep. State B is active. Event E_one occurs and wakes the chart, which processes the event from the root through the hierarchy:

  1. The chart root checks if there is a valid transition as a result of E_one. A valid transition segment exists from state B to the connective junction and from the junction to state C.

  2. The state B exit action exitB() executes and completes.

  3. State B becomes inactive.

  4. State C becomes active.

  5. The state C entry action entC() executes and completes.

  6. The chart goes to sleep.

Backtrack in Flow Charts

This image shows the behavior of transitions with junctions that force backtracking behavior in flow charts.

Stateflow chart with states called A and B.

Initially, state A is active, conditions c1, c2, and c3 are true, and condition c4 is false.

  1. The chart root checks if there is a valid transition from state A.

    Condition c1, which connects state A to a connective junction, is valid.

  2. Condition c1 is true and action a1 executes.

  3. Condition c3 is true and action a3 executes.

  4. Condition c4 is not true and the chart backtracks to state A.

  5. The chart root checks to see if there is another valid transition from state A.

    Condition c2, which connects state A to a connective junction, is valid.

  6. Condition c2 is true and action a2 executes.

  7. Condition c3 is true and action a3 executes.

  8. Condition c4 is not true and the chart backtracks to state A.

  9. The chart goes to sleep.

In the previous image, the chart executes both actions a1 and a2 and executes a3 twice. To resolve this problem, consider adding unconditional transitions to terminating junctions.

Stateflow chart with states called A and B.

The terminating junctions allow flow to end if either c3 or c4 is not true. This design leaves state A active without executing unnecessary actions.

Additional Examples of Unintended Backtrack

Open this model to see additional examples of unintended backtracking in flow charts.

Related Topics