Common Modeling Errors Stateflow Can Detect

State Inconsistencies in a Chart

Definition of State Inconsistency

States in a Stateflow® chart are inconsistent if they violate any of these rules:

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

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

  • All substates of an inactive state with either exclusive (OR) or parallel (AND) decomposition are inactive.

Causes of State Inconsistency

An error occurs at compile time when the following conditions are all true:

  • A transition leads to a state that has exclusive (OR) decomposition and multiple substates. There are no default paths that lead to the entry of any substate. This condition results in a state inconsistency error. (However, if all transitions into that state are supertransitions leading directly to the substates, there is no error.)

  • The state with multiple substates does not contain a history junction.

You can control the level of diagnostic action that occurs due to omission of a default transition in the Diagnostics > Stateflow pane of the Model Configuration Parameters dialog box. For more information, see the documentation for the No unconditional default transitions diagnostic.

State Inconsistency Example

The following chart has a state inconsistency.

In the absence of a default transition indicating which substate is to become active, the chart has a state inconsistency error.

Adding a default transition to one of the substates resolves the state inconsistency.

Conflicting Transitions in a Chart

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, Stateflow software evaluates equally valid transitions based on ordering mode in the chart: explicit or implicit.

Detect Conflicting Transitions for C Charts with Implicit Ordering

Transition conflict checking is enabled by default. To detect conflicting transitions in a C chart with implicit ordering during a simulation:

  1. Build the target with debugging enabled.

  2. Confirm Simulation > Debug > Stateflow Error Checking Options > Transition Conflict is checked.

  3. Start the simulation.

To turn off transition conflict checking, uncheck Simulation > Debug > Stateflow Error Checking Options > Transition Conflict.

Example of Conflicting Transitions

The following chart has two conflicting transitions:

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 where state A is active and both conditions are true. This issue is a transition conflict.

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.

Conflict Resolution for Implicit Ordering Checking Transition Conflicts Is Not Selected.  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.

Data Range Violations in a Chart

Types of Data Range Violations

Stateflow detects the following data range violations during simulation:

  • When a data object equals a value outside the range of the values set in the Initial value, Minimum, and Maximum fields specified in the Data properties dialog box

    See Set Data Properties for a description of the Initial value, Minimum, and Maximum fields in the Data properties dialog box.

  • When the result of a fixed-point operation overflows its bit size

    See Detect Overflow for Fixed-Point Types for a description of the overflow condition in fixed-point numbers.

When you select Saturate on integer overflow for your chart, Stateflow does not flag any cases of integer overflow during simulation. However, Stateflow continues to flag out-of-range data violations based on minimum-and-maximum range checks. For more information, see Impact of Saturation on Error Checks.

Detect Data Range Violations

Data range violation checking is enabled by default. To detect data range violations during a simulation:

  1. Build the target with debugging enabled.

  2. Confirm Simulation > Debug > Stateflow Error Checking Options > Data Range is checked.

  3. Start the simulation.

To turn off data range checking, uncheck Simulation > Debug > Stateflow Error Checking Options > Data Range.

Data Range Violation Example

The following chart has a data range violation.

Assume that the data a has an Initial value and Minimum value of 0 and a Maximum value of 2. Each time an event awakens this chart and state A is active, a increments. The value of a quickly becomes a data range violation.

Cyclic Behavior in a Chart

What Is Cyclic Behavior?

Cyclic behavior is a step or sequence of steps that is repeated indefinitely (recursive). Stateflow uses cycle detection algorithms to detect a class of infinite recursions caused by event broadcasts.

Detect Cyclic Behavior During Simulation

Cyclic behavior checking is enabled by default. To detect cyclic behavior during a simulation:

  1. Build the target with debugging enabled.

  2. Confirm Simulation > Debug > Stateflow Error Checking Options > Detect Cycles is checked.

  3. Start the simulation.

To turn off cyclic behavior checking, uncheck Simulation > Debug > Stateflow Error Checking Options > Detect Cycles.

Cyclic Behavior Example

This chart shows how an event broadcast can cause infinite recursive cycles.

When the state C during action executes, event E1 is broadcast. The transition from state A.A1 to state A.A2 becomes valid when event E1 is broadcast. Event E2 is broadcast as a condition action of that transition. The transition from state B.B1 to state B.B2 becomes valid when event E2 is broadcast. Event E1 is broadcast as a condition action of the transition from state B.B1 to state B.B2. Because these event broadcasts of E1 and E2 are in condition actions, a recursive event broadcast situation occurs. Neither transition can complete.

    Tip   Avoid using undirected local event broadcasts, which can cause unwanted recursive behavior in your chart. Use the send operator for directed local event broadcasts. For more information, see Broadcast Events to Synchronize States.

    You can set the diagnostic level for detecting undirected local event broadcasts. In the Model Configuration Parameters dialog box, go to the Diagnostics > Stateflow pane and set the Undirected event broadcasts diagnostic to none, warning, or error. The default setting is warning.

Flow Cyclic Behavior Not Detected Example

This chart shows an example of cyclic behavior in a flow chart that Stateflow cannot detect.

The data object i is set to 0 in the condition action of the default transition. i increments in the next transition segment condition action. The transition to the third connective junction is valid only when the condition [i < 0] is true. This condition is never true in this flow chart, resulting in a cycle.

Stateflow cannot detect this cycle because it does not involve recursion due to event broadcasts. Although Stateflow cannot detect cycles that depend on data values, a separate diagnostic error does appear during simulation, for example:

Junction is part of a cycle and does not have an 
unconditional path leading to termination.

For information on fixing cyclic behavior in flow charts, type the following at the MATLAB® command prompt:

sfhelp('cycle_error');

Noncyclic Behavior Flagged as a Cycle Example

This chart shows an example of noncyclic behavior that Stateflow flags as being cyclic.

State A becomes active and i is initialized to 0. When the transition is tested, the condition [i < 5] is true. The condition actions that increment i and broadcast the event E are executed. The broadcast of E when state A is active causes a repetitive testing (and incrementing of i) until the condition is no longer true. Stateflow flags this behavior as a cycle, but the so-called cycle breaks when i becomes greater than 5.

    Tip   Avoid using undirected local event broadcasts, which can cause unwanted recursive behavior in your chart. Use the send operator for directed local event broadcasts. For more information, see Broadcast Events to Synchronize States.

    You can set the diagnostic level for detecting undirected local event broadcasts. In the Model Configuration Parameters dialog box, go to the Diagnostics > Stateflow pane and set the Undirected event broadcasts diagnostic to none, warning, or error. The default setting is warning.

Was this topic helpful?