Main Content

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.

Stateflow chart with two superstates called Fan and Heater. Each superstate has two substates called On and Off.

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.

Flow chart that 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.

Chart with two parallel states called A and B. Each state has two parallel substates called A1 and A2. A1 has two exclusive states called X and Y. A2 has two exclusive states called P and Q.

The chart resolves each qualified state name as the local copy of the substate Y:

  • In the state A, the condition in(A1.Y) checks the activity of state A.A1.Y.

  • In the state B, the condition in(A1.Y) checks the activity of state B.A1.Y.

For example, this table lists the different stages in the resolution process for the transition condition in state A.

Stage DescriptionResult
1Starting in state A.A2, the chart searches for the state A.A2.A1.Y.No match found.
2Moving up to the next level of the hierarchy (state A), the chart searches for the state A.A1.YMatch found.
3Moving up to the next level of the hierarchy (the chart level), the chart searches for the state A1.YNo 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 expression in(B.A1.Y).

  • In state B, use the expression in(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.

Chart with five nested states called A, B, P, Q, and R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, the chart searches for the state A.B.Q.R.No match found.
2Moving up to the next level of the hierarchy (state A), the chart searches for the state A.Q.R.No match found.
3Moving 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.

Chart with two superstates called A and Q. A has four nested substates called B, P, Q, and R. Q has a substate called R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, the chart searches for the state A.B.Q.R.No match found
2Moving up to the next level of the hierarchy (state A), the chart searches for the state A.Q.R.No match found.
3Moving 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.

Chart with two superstates called A and P. A has four nested substates called B, P, Q, and R. P has two nested substates called Q and R.

This table lists the different stages in the resolution process.

StageDescriptionResult
1Starting in state A.B, search for the state A.B.P.Q.R.Match found
2Moving up to the next level of the hierarchy (state A), the chart searches for the state A.P.Q.R.No match found.
3Moving 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 expression in(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.

    Chart with a superstate called A and a box called Enclosure. A has four nested substates called B, P, Q, and R. Enclosure has three nested substates called P, Q, and R.

See Also

|

Related Topics