Detect Common Modeling Errors During Chart Simulation

To avoid common design errors, you can run diagnostic checks that test the completeness of your Stateflow® chart during compilation and simulation. Stateflow diagnostics detect state inconsistencies, violations in data ranges, and cyclic behavior in Stateflow charts in Simulink® models.

When you simulate a model, Stateflow automatically parses the Stateflow machine. The parser evaluates the graphical and nongraphical objects and data in each Stateflow machine against the supported chart notation and the action language syntax. You can also check the syntax of your chart by selecting Update Chart in the Modeling tab of the Stateflow Toolstrip.

If syntax errors exist in your chart, the chart automatically appears with the highlighted object that causes the first error. You can select the error in the diagnostic window to bring its source chart to the front with the source object highlighted. Any unresolved data or events in the chart are flagged in the Symbol Wizard.

State Inconsistencies

In a Stateflow chart, states are inconsistent if they violate one of these rules:

  • An active state with exclusive (OR) decomposition and at least one substate has exactly one active substate.

  • All substates of an active state with parallel (AND) decomposition are active.

  • All substates of an inactive state are inactive regardless of the state decomposition.

While you edit your chart, the Stateflow Editor displays potential causes for state inconsistencies by highlighting objects in red or orange. For more information, see Detect Modeling Errors During Edit Time.

Causes of State Inconsistency

One type of state inconsistency occurs when all of these conditions are true:

  • A state with multiple substates has exclusive (OR) decomposition.

  • The state does not contain a history junction.

  • Every default path that leads to a substate is guarded by a condition or triggered by an event.

  • Not all transitions into the state are supertransitions leading directly to a substate.

For example, this chart has a state inconsistency because there is no default transition to indicate which substate becomes active first.

Adding an unconditional default transition to one of the states resolves the state inconsistency.

Detect State Inconsistencies

At compile time, Stateflow charts detect state inconsistencies caused by the omission of an unconditional default transition. To control the level of diagnostic action, open the Model Configuration Parameters dialog box. In the Diagnostics > Stateflow pane, for the diagnostic No unconditional default transitions, you can select error, warning, or none. The default setting is error. For more information, see No unconditional default transitions.

Data Range Violations

During simulation, a data range violation occurs when:

For example, this chart contains local data a that has a Minimum value of 0 and a Maximum value of 2. The entry action in state A initializes a to 1. The during action increments the value of a by 1. After two time steps, the value of a exceeds its specified range, resulting in a data range violation.

Detect Data Range Violations

At run time, Stateflow charts detect data range violations. To control the level of diagnostic action, open the Model Configuration Parameters dialog box. In the Diagnostics > Data Validity pane, you can select error, warning, or none for these diagnostics:

  • Simulation range checking detects violations based on minimum-and-maximum range checks. The default setting is none.

  • Wrap on overflow and Saturate on overflow detect violations that occur when integer or fixed-point operations exceed the numeric capacity of their result type. The default setting is warning.

For more information, see Simulation range checking, Wrap on overflow, and Saturate on overflow.

Cyclic Behavior

Cyclic behavior occurs when a step or sequence of steps is repeated indefinitely during chart simulation.

For example, the actions in this chart produce an infinite cycle of recursive event broadcasts.

  • The during action in state C broadcasts the event E1.

  • The event E1 triggers the transition from state A.A1 to state A.A2. The condition action for this transition broadcasts the event E2.

  • The event E2 triggers the transition from state B.B1 to state B.B2. The condition action for this transition broadcasts the event E1.

The event broadcasts in states A and B occur in condition actions, so the transitions do not take place until the chart processes the resulting events. The substates A.A1 and B.B1 remain active, so new event broadcasts continue to trigger the transitions and the process repeats indefinitely.

Because undirected local event broadcasts can cause unwanted recursive behavior, use of the send operator to broadcast directed local events is recommended. For more information, see Broadcast Local Events to Synchronize Parallel States.

Detect Cyclic Behavior During Simulation

During chart simulation, Stateflow charts use cycle detection algorithms to detect a class of infinite recursions caused by event broadcasts. To enable cycle detection, open your Stateflow chart. In the Debug tab, select Diagnostics > Detect Cyclical Behavior. Cyclic behavior checking is selected by default.

Stateflow charts also detect undirected local event broadcasts. To control the level of diagnostic action, open the Model Configuration Parameters dialog box. In the Diagnostics > Stateflow pane, for the Undirected event broadcasts diagnostic, you can select error, warning, or none. The default setting is warning. For more information, see Undirected event broadcasts.

Limitations of Cyclic Behavior Detection

Stateflow cycle detection is limited to cases of recursion due to event broadcasts and does not extend to other types of cyclic behavior.

For instance, Stateflow cannot detect the infinite cycle in this flow chart. In this example, the default transition initializes the local data i to 0. The next transition segment increments i. The transition to the terminating junction is valid only when the condition [i < 0] is true. Because this condition is never true, an infinite cycle results.

The model sf_cycle_error_fix provides suggestions for fixing cyclic behavior in flow charts. At the MATLAB® command prompt, enter:

sfhelp('cycle_error');

Related Topics