Resolve Unintended Recursive Behavior - MATLAB & Simulink (original) (raw)
Issue
If your Stateflow® chart contains recursive event broadcasts or function calls, you may experience slow simulation speeds, or the Simulink Diagnostic window may display a warning.
To address these issues, modify your chart to remove:
- Undirected local event broadcasts
- Early returns caused by self-triggered event transitions
- Function calls
Possible Solutions
Remove or Modify Undirected Local Event Broadcasts
When you do not direct an event broadcast towards a state, the chart sends the event to all states in which the broadcast is visible, which can cause recursion. The format of an undirected event broadcast is
where_event_name_ is a local event.
In a future release, Stateflow will no longer support undirected local event broadcasts. If your chart contains an undirected local event broadcast, either remove the broadcast or direct the broadcast towards an existing or new state.
Remove the Event
If an undirected local event broadcast triggers a transition, you can replace the event with a temporal operator or condition.
For example, this chart transitions from state A
to state B
when the leave
event triggers. In state A
, an on
action broadcasts the leave
event once every five steps.
To resolve the event recursion, remove the leave
event and the on
action. Add an every(5,tick)
operator to the transition.
Direct the Event Broadcast to Existing States
Suppose a state broadcasts an undirected local event, and a transition or action in another state receives it. If the receiving state is not a descendant of the broadcasting state, you can direct the event to the receiving state.
For example, this chart has three parallel states: Motor
, Vent
, and Fan
. The Vent
and Fan
states use two events, cooling_on
and cooling_off
, to transition between operating modes. The Motor
state triggers the events by using undirected event broadcasts.
To resolve the event recursion, direct the event broadcasts to Vent
and Fan
.
Direct the Event Broadcast to a New Parent State
Suppose a state broadcasts an undirected local event, and transitions or actions in two or more states receive it. If the receiving states are not descendants of the broadcasting state, you can enclose the receiving states in a new parent state and then direct the event broadcast to the parent.
For example, this chart has three parallel states: Motor
, Vent
, and Fan
. The Vent
and Fan
states use two events, cooling_on
and cooling_off
, to transition between operating modes. The Motor
state triggers the events by using undirected event broadcasts.
To resolve the event recursion, enclose the Vent
and Fan
states inside a new parent state called Cooling
. Direct the event broadcasts to Cooling
.
Restructure States That Trigger Both Internal and Exit Transitions
If an undirected event triggers both a transition inside a state and a transition out of a state on the same time step, restructure the chart so that the transitions happen in different states.
For example, in this chart, state A
has both an inner transition and a transition out of the state. Both transitions contain an every
operator that executes when the tick
implicit event triggers.
When the inner transition activates the local event local
:
- The chart wakes up.
- The chart processes the local event.
- The
tick
implicit event triggers. - Both transitions trigger.
To resolve the recursion:
- Move the inner transition logic to a separate state.
- Enclose the original state in a parent state.
- Direct the event to the parent state.
Replace Self-Triggered Event Transitions with Exit Junctions
If a state broadcasts an event and uses the event to trigger a transition to either itself or a different state, it causes event recursion.
To resolve the event recursion, remove the event. Then, add an exit junction and connect the exit port to the original destination. For more information, see Create Entry and Exit Connections Across State Boundaries.
For example, this chart has parent state A
and child states B
and C
. When B
is active for at least one time step, it broadcasts the event returnToSelf
, which causes the parent state to exit and then reenter itself.
To resolve the event recursion, remove the returnToSelf
event. Replace C
with an exit junction. Transition from the exit port to A
.
Remove Recursive Function Calls
You can cause an infinite, recursive loop during execution by creating a function that directly or indirectly calls itself. For example, suppose a chart contains functionsf
and g
. Recursion occurs if either functions calls itself. However, recursion also occurs if f
callsg
and g
also calls f
. To fix recursive function calls, rewrite the function code so that no function directly or indirectly calls itself.