Represent Multiple Paths by Using Connective Junctions
Label Format for Transition Segments
The label format for a transition segment entering a junction is the same as for transitions entering states, as shown in the following example. The chart uses implicit ordering of outgoing transitions (see Implicit Ordering).
Execution of a transition in this example occurs as follows:
When an event occurs, state
S1
is checked for an outgoing transition with a matching event specified.If a transition with a matching event is found, the transition condition for that transition (in brackets) is evaluated.
If
condition_1
evaluates to true, the condition actioncondition_action
(in braces) is executed.The outgoing transitions from the junction are checked for a valid transition. Since
condition_2
is true, a valid state-to-state transition fromS1
toS2
exists.State
S1
exit actions execute and complete.State
S1
is marked inactive.The transition action
transition_action
executes and completes.The completed state-to-state transition from
S1
toS2
occurs.State
S2
is marked active.State
S2
entry actions execute and complete.
If-Then-Else Decision Construct
This example shows the behavior of an if-then-else
decision
construct. The chart uses implicit ordering of outgoing transitions (see Implicit Ordering).
Initially, the chart is asleep. State A
is active. Condition
[C_two]
is true. Event E_one
occurs and
awakens the chart, which processes the event from the root down through the
hierarchy:
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. Because implicit ordering applies, the transition segments beginning from a 12 o'clock position on the connective junction are evaluated for validity. The first transition segment, labeled with condition [C_one
], is not valid. The next transition segment, labeled with the condition [C_two
], is valid. The complete transition from stateA
to stateC
is valid.State
A
exit actions (exitA()
) execute and complete.State
A
is marked inactive.State
C
is marked active.State
C
entry actions (entC()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow® chart associated with event E_one
.
Self-Loop Transition
This example shows the behavior of a self-loop transition using a connective junction. The chart uses implicit ordering of outgoing transitions (see Implicit Ordering).
Initially, the chart is asleep. State A
is active. Condition
[C_one]
is false. Event E_one
occurs and
awakens the chart, which processes the event from the root down through the
hierarchy:
The chart root checks to see if there is a valid transition as a result of
E_one
. A valid transition segment from stateA
to the connective junction exists. Because implicit ordering applies, the transition segment labeled with a condition is evaluated for validity. Because the condition [C_one
] is not valid, the complete transition from stateA
to stateB
is not valid. The transition segment from the connective junction back to stateA
is valid.State
A
exit actions (exitA()
) execute and complete.State
A
is marked inactive.The transition action
A_two
is executed and completed.State
A
is marked active.State
A
entry actions (entA()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow chart associated with event E_one
.
For-Loop Construct
This example shows the behavior of a for
loop using a
connective junction. The chart uses implicit ordering of outgoing transitions (see
Implicit Ordering).
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:
The chart root checks to see if there is a valid transition as a result of
E_one
. There is a valid transition segment from stateA
to the connective junction. The transition segment condition action,i = 0
, executes and completes. Of the two transition segments leaving the connective junction, the transition segment that is a self-loop back to the connective junction evaluates next for validity. That segment takes priority in evaluation because it has a condition, whereas the other segment is unlabeled. This evaluation behavior reflects implicit ordering of outgoing transitions in the chart.The condition
[i < 10]
evaluates as true. The condition actionsi++
and a call tofunc1
execute and complete until the condition becomes false. Because a connective junction is not a final destination, the transition destination is still unknown.The unconditional segment to state
B
is now valid. The complete transition from stateA
to stateB
is valid.State
A
exit actions (exitA()
) execute and complete.State
A
is marked inactive.State
B
is marked active.State
B
entry actions (entB()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this chart associated with event
E_one
.
Flow Chart Notation
This example shows the behavior of a Stateflow chart that uses flow chart notation. The chart uses implicit ordering of outgoing transitions (see Implicit Ordering).
Initially, the chart is asleep. State A.A1
is active. The
condition [C_one()]
is initially true. Event
E_one
occurs and awakens the chart, which processes the event
from the root down through the hierarchy:
The chart root checks to see if there is a valid transition as a result of
E_one
. There is no valid transition.State
A
checks itself for valid transitions and detects a valid inner transition to a connective junction.The next possible segments of the transition are evaluated. Only one outgoing transition exists, and it has a condition action defined. The condition action executes and completes.
The next possible segments are evaluated. Two outgoing transitions exist: a conditional self-loop transition and an unconditional transition segment. Because implicit ordering applies, the conditional transition segment takes precedence. Since the condition
[C_one()]
is true, the self-loop transition is taken. Since 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.The next possible transition segment (to the next connective junction) is evaluated. It is an unconditional transition segment with a condition action. The transition segment is taken and the condition action,
{d=my_func()}
, executes and completes. The returned value ofd
is 84.The next possible transition segment is evaluated. Three outgoing transition segments exist: two conditional and one unconditional. Because implicit ordering applies, the segment labeled with the condition
[d < 100]
evaluates first based on the geometry of the two outgoing conditional transition segments. Because the returned value ofd
is 84, the condition[d < 100]
is true and this transition to the destination stateA.A1
is valid.State
A.A1
exit actions (exitA1()
) execute and complete.State
A.A1
is marked inactive.State
A.A1
is marked active.State
A.A1
entry actions (entA1()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow chart associated with event E_one
.
Transition from a Common Source to Multiple Destinations
This example shows the behavior of transitions from a common source to multiple conditional destinations using a connective junction. The chart uses implicit ordering of outgoing transitions (see Implicit Ordering).
Initially, the chart is asleep. State A
is active. Event
E_two
occurs and awakens the chart, which processes the event
from the root down through the hierarchy:
The chart root checks to see if there is a valid transition as a result of
E_two
. A valid transition segment exists from stateA
to the connective junction. Because implicit ordering applies, evaluation of segments with equivalent label priority begins from a 12 o'clock position on the connective junction and progresses clockwise. The first transition segment, labeled with eventE_one
, is not valid. The next transition segment, labeled with eventE_two
, is valid. The complete transition from stateA
to stateC
is valid.State
A
exit actions (exitA()
) execute and complete.State
A
is marked inactive.State
C
is marked active.State
C
entry actions (entC()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow chart associated with event E_two
.
Resolve Equally Valid Transition Paths
What Are Conflicting Transitions?
Conflicting transitions are two equally valid paths from the same source in a Stateflow chart during simulation. In the case of a conflict, the chart evaluates equally valid transitions based on ordering mode in the chart: explicit or implicit.
For explicit ordering (the default mode), evaluation of conflicting transitions occurs based on the order you specify for each transition. For details, see Explicit Ordering.
For implicit ordering in C charts, evaluation of conflicting transitions occurs based on internal rules described in Implicit Ordering.
Example of Conflicting Transitions
The following chart has two equally valid transition paths:
Conflict Resolution for Implicit Ordering
For implicit ordering, the chart evaluates multiple outgoing transitions with
equal label priority in a clockwise progression starting from the twelve o'clock
position on the state. In this case, the transition from state
A
to state B
occurs.
Conflict Resolution for Explicit Ordering
For explicit ordering, the chart resolves the conflict by evaluating outgoing
transitions in the order that you specify explicitly. 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.
How the Transition Conflict Occurs
The default transition to state A
assigns data
a
equal to 1 and data b
equal 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 issue is a
transition conflict.
Transition from Multiple Sources to a Common Destination
This example shows the behavior of transitions from multiple sources to a single destination using a connective junction.
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:
The chart root checks to see if there is a valid transition as a result of
E_one
. A valid transition segment exists from stateA
to the connective junction and from the junction to stateC
.State
A
exit actions (exitA()
) execute and complete.State
A
is marked inactive.State
C
is marked active.State
C
entry actions (entC()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow chart associated with event E_one
.
Transition from a Source to a Destination Based on a Common Event
This example shows the behavior of transitions from multiple sources to a single destination based on the same event using a connective junction.
Initially, the chart is asleep. State B
is active. Event
E_one
occurs and awakens the chart, which processes the event
from the root down through the hierarchy:
The chart root checks to see if there is a valid transition as a result of
E_one
. A valid transition segment exists from stateB
to the connective junction and from the junction to state C.State
B
exit actions (exitB()
) execute and complete.State
B
is marked inactive.State
C
is marked active.State
C
entry actions (entC()
) execute and complete.The chart goes back to sleep.
This sequence completes the execution of this Stateflow chart associated with event E_one
.
Backtrack in Flow Charts
This example shows the behavior of transitions with junctions that force backtracking behavior in flow charts.
Initially, state A
is active, conditions c1
,
c2
, and c3
are true, and condition
c4
is false:
The chart root checks to see if there is a valid transition from state
A
.There is a valid transition segment marked with the condition
c1
from stateA
to a connective junction.Condition
c1
is true and actiona1
executes.Condition
c3
is true and actiona3
executes.Condition
c4
is not true and control flow backtracks to stateA
.The chart root checks to see if there is another valid transition from state
A
.There is a valid transition segment marked with the condition
c2
from stateA
to a connective junction.Condition
c2
is true and actiona2
executes.Condition
c3
is true and actiona3
executes.Condition
c4
is not true and control flow backtracks to stateA
.The chart goes to sleep.
The preceding example shows the expected behavior of executing both actions
a1
and a2
. Another unexpected behavior is
the execution of action a3
twice. To resolve this problem,
consider adding unconditional transitions to terminating junctions.
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.