Check State Activity by Using the in
Operator
In a Stateflow® chart with parallel state decomposition, substates can be active at the same time. To coordinate the behavior of different parallel states, one state can check the substate activity of another state and react accordingly. For example, one state can keep its substates synchronized with the substates of the other state.
The in
Operator
To check if a state is active in a given time step, call the in
operator in state and
transition actions. The in
operator takes a qualified state name
state_name
and returns a Boolean output. If state
state_name
is active, in
returns a value
of 1 (true
). Otherwise, in
returns a value of
0 (false
).
in(state_name)
For example, in this chart, Fan
and Heater
are parallel (AND) states. Each state has a pair of substates, On
and Off
. Every second, the active substate of the state
Fan
alternates between Fan.Off
and
Fan.On
. In the state Heater
, the
conditions on the transitions check the substate activity in Fan
and keep the states synchronized. A change of active substate in
Fan
causes a corresponding change of active substate in
Heater
.
Resolution of State Activity
Checking state activity is a two-part process. First, the Stateflow chart resolves the qualified state name by performing a localized search of the chart hierarchy for a matching state. Then, the chart determines if the matching state is active.
The search begins at the hierarchy level where the in
operator
is called with the qualified state name:
For a state action, the starting point is the state containing the action.
For a transition label, the starting point is the parent of the transition source.
The resolution process searches each level of the chart hierarchy for a path to the state. If a state matches the path, the process adds that state to the list of possible matches. Then, the process continues the search one level higher in the hierarchy. The resolution process stops after it searches the chart level of the hierarchy. If a unique match exists, the chart checks if the matching state is active. Otherwise, the resolution process fails. Simulation stops with an error.
This flow chart illustrates the different stages in the process for checking state activity.
Best Practices for Checking State Activity
To resolve the state activity, a Stateflow chart does not perform an exhaustive search for all states and does not stop after finding the first match. To improve the chances of finding a unique search result:
Use specific paths in qualified data names.
Give states unique names.
Use states and boxes as enclosures to limit the scope of the path resolution search.
Examples of State Activity Resolution
Search Finds Local Copy of Substate
This chart contains two parallel states, A
and
B
. Each state has a pair of substates,
A1
and A2
. A1
has
substates X
and Y
, while
A2
has substates P
and
Q
. In A.A2
and in
B.A2
, the condition in(A1.Y)
guards
the transition from P
to Q
.
The chart resolves each qualified state name as the local copy of the substate
Y
:
In the state
A
, the conditionin(A1.Y)
checks the activity of stateA.A1.Y
.In the state
B
, the conditionin(A1.Y)
checks the activity of stateB.A1.Y
.
For example, this table lists the different stages in the
resolution process for the transition condition in state
A
.
Stage | Description | Result |
---|---|---|
1 | Starting in state A.A2 , the chart
searches for the state A.A2.A1.Y . | No match found. |
2 | Moving up to the next level of the hierarchy (state
A ), the chart searches for the state
A.A1.Y | Match found. |
3 | Moving up to the next level of the hierarchy (the chart
level), the chart searches for the state
A1.Y | No match found. |
The search ends with a single match found. Because the
resolution algorithm localizes the scope of the search, the
in
operator guarding the transition in
A.A2
detects only the state A.A1.Y
.
The in
operator guarding the transition in
B.A2
detects only the state
B.A1.Y
.
To check the state activity of the other copy of Y
, use
more specific qualified state names:
In state
A
, use the expressionin(B.A1.Y)
.In state
B
, use the expressionin(A.A1.Y)
.
Search Produces No Matches
In this chart, the during
action in state
A.B
contains the expression in(Q.R)
.
Stateflow cannot resolve the qualified state name Q.R
.
This table lists the different stages in the resolution process.
Stage | Description | Result |
---|---|---|
1 | Starting in state A.B , the chart
searches for the state A.B.Q.R . | No match found. |
2 | Moving up to the next level of the hierarchy (state
A ), the chart searches for the state
A.Q.R . | No match found. |
3 | Moving up to the next level of the hierarchy (the chart
level), the chart searches for the state
Q.R . | No match found. |
The search ends at the chart level with no match found for
Q.R
, resulting in an error.
To avoid this error, use a more specific qualified state name. For instance,
check state activity by using the expression
in(P.Q.R)
.
Search Finds the Wrong State
In this chart, the during
action in state
A.B
contains the expression in(Q.R)
.
When resolving the qualified state name Q.R
, Stateflow cannot detect the substate A.B.P.Q.R
.
This table lists the different stages in the resolution process.
Stage | Description | Result |
---|---|---|
1 | Starting in state A.B , the chart
searches for the state A.B.Q.R . | No match found |
2 | Moving up to the next level of the hierarchy (state
A ), the chart searches for the state
A.Q.R . | No match found. |
3 | Moving up to the next level of the hierarchy (the chart
level), the chart searches for the state
Q.R . | Match found. |
The search ends with a single match found. The
in
operator detects only the substate
R
of the top-level state Q
.
To check the state activity of A.B.P.Q.R
, use a more
specific qualified state name. For instance, use the expression
in(P.Q.R)
.
Search Produces Multiple Matches
In this chart, the during
action in state
A.B
contains the expression in(P.Q.R)
.
Stateflow cannot resolve the qualified state name P.Q.R
.
This table lists the different stages in the resolution process.
Stage | Description | Result |
---|---|---|
1 | Starting in state A.B , search for the
state A.B.P.Q.R . | Match found |
2 | Moving up to the next level of the hierarchy (state
A ), the chart searches for the state
A.P.Q.R . | No match found. |
3 | Moving up to the next level of the hierarchy (the chart
level), the chart searches for the state
P.Q.R . | Match found. |
The search ends at the chart level with two matches found for
P.Q.R
, resulting in an error.
To avoid this error:
Use a more specific qualified state name. For example, to check the substate activity inside
B
, use the expressionin(B.P.Q.R)
.Rename one of the matching states.
Enclose the top-level state
P
in a box or another state. Adding an enclosure prevents the search process from detecting substates in the top-level state.