Avoid Unwanted Recursion in a Chart

Recursion can be useful for controlling substate transitions among parallel states at the same level of the chart hierarchy. For example, you can send a directed event broadcast from one parallel state to a sibling parallel state to specify a substate transition. This type of recursive behavior is desirable and efficient. For details, see Broadcast Local Events to Synchronize Parallel States.

However, unwanted recursion can also occur during chart execution. To avoid unwanted recursion, do not use recursive function calls or undirected local event broadcasts.

Recursive Function Calls

Suppose that you have functions named f, g, and h in a chart. These functions can be any combination of graphical functions, truth table functions, MATLAB® functions, or Simulink® functions.

To avoid recursive behavior, do not:

  • Have f calling g calling h calling f.

  • Have f, g, or h calling itself.

Undirected Local Event Broadcasts

Examples of state actions with undirected local event broadcasts include:

  • entry: send(E1), where E1 is a local event in the chart.

  • exit: E2, where E2 is a local event in the chart.

To avoid recursive behavior, replace undirected event broadcasts with directed event broadcasts by using the syntax

where event is a local event in the chart and state is a destination state.

  • If the local event broadcast occurs in a state action, ensure that the destination state is not an ancestor of the source state in the chart hierarchy.

  • If the local event broadcast occurs in a transition, ensure that:

    • The destination state is not an ancestor of the transition in the chart hierarchy.

    • The transition does not connect to the destination state.

During simulation, Stateflow® charts can 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 none, warning, or error. The default setting is warning. For more information, see Undirected event broadcasts (Simulink).

See Also

Related Topics