During execution, Stateflow® objects interact with each other to simulate real-world behavior. In the following model, different Stateflow objects are shown and their interactions are explained.
The example model shows how common graphical and nongraphical objects in a chart interact during execution. These objects include:
Conditions and condition actions
Exclusive (OR) states
Flow charts
Function calls
History junctions
Parallel (AND) states
State actions
Transitions guarded by input events
For details of the chart semantics, see Phases of Chart Execution.
This example uses the hotel check-in process to explain Stateflow chart semantics.
The sf_semantics_hotel_checkin
model consists of four Manual Switch blocks, one Mux block, one Multiport Switch block, a Hotel chart,
and a Display block.
Block | Purpose | Rationale |
---|---|---|
Enable toggling between two settings during simulation without having to pause or restart. | During simulation, you can interactively trigger the chart by sending one of these input events:
| |
Combine multiple input signals into a vector. | A chart can support multiple input events only if they connect to the trigger port of a chart as a vector of inputs. | |
Enable selection among more than two inputs. | This block provides a value for the chart input data
A Manual Switch block cannot support more than two inputs, but a Multiport Switch block can. | |
Show up-to-date numerical value for input signal. | During simulation, any change to the chart output data
|
The Hotel chart contains graphical objects, such as states and history junctions, and nongraphical objects, such as conditions and condition actions.
For a mapping of objects to their locations in the chart, see Stateflow Objects.
When simulation starts, the chart wakes up and executes its default transitions because the Execute (enter) Chart At Initialization option is on (see Execution of a Chart at Initialization). Then the chart goes to sleep.
Note
If this option is off, the chart does not wake up until you toggle one of the Manual Switch blocks. You can verify the setting for this option in the Chart properties dialog box. Right-click inside the top level of the chart and select Properties from the context menu.
The chart wakes up again only when an edge-triggered input
event occurs: check_in
, room_service
,
fire_alarm
, or all_clear
. When you toggle a
Manual Switch block for an input event during simulation, the chart
detects a rising or falling edge and wakes up. While the chart is awake:
The Multiport Switch block provides a value for the chart
input data room_type
.
The Display block shows any change in value for the chart
output data fee
.
After completing all possible phases of execution, the chart goes back to sleep.
The following sections explain chart execution for each shaded region of the Hotel chart.
Tip
Click a shaded region to jump directly to information about that phase of chart execution.
This section describes what happens in the Front_desk
state
just after the chart wakes up.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | Your first stop is at the front desk of the hotel. | At the chart level, the default transition to
For reference, see Enter a Chart or State. |
2 | You leave the front desk after checking in to the hotel. | The For reference, see How Stateflow Charts Respond to Events. |
3 | Just before you leave the front desk, you pick up your bags to move to your room. | Just before the transition occurs, the For reference, see Exit a State. |
Modeling Guidelines for Chart Initialization. The following guidelines apply to chart initialization.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use exclusive (OR) decomposition when no two states at a level of the hierarchy can be active at the same time. | This guideline ensures proper chart execution. For example,
| |
Use a default transition to mark the first state to become active among exclusive (OR) states. | This guideline prevents state inconsistency errors during chart execution. | |
Use events, instead of conditions, to guard transitions that depend on occurrences without inherent numerical value. | Since you cannot easily quantify the numerical value of checking into a hotel, model such an occurrence as an event. | |
Use an | Other types of state actions execute differently and do not apply:
|
This section describes what happens after exiting the
Front_desk
state: the evaluation of a group of outgoing
transitions from a single junction.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | You can move to one of three types of rooms. | After the For reference, see Order of Execution for a Set of Flow Charts. |
2 | If you choose an executive suite, the base fee is 1500. | If the Note If the top transition is not valid, control flow backtracks to the central junction so that testing of the next transition can occur. This type of backtracking is intentional. To learn about unintentional backtracking and how to avoid it, see Backtrack in Flow Charts and Best Practices for Creating Flow Charts. |
3 | If you choose a family suite, the base fee is 1000. | If |
4 | If you choose a single room, the base fee is 500. | If |
What happens if
room_type
has a value other than 1, 2, or
3?
Modeling Guidelines for Evaluation of Outgoing Transitions. The following guidelines apply to transition syntax.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use conditions, instead of events, to guard transitions that depend on occurrences with numerical value. | Because you can quantify a type of hotel room numerically, express the choice of room type as a condition. | |
Use condition actions instead of transition actions whenever possible. | Condition actions execute as soon as the condition evaluates to true. Transition actions do not execute until after the transition path is complete, to a terminating junction or a state. Unless an execution delay is necessary, use condition actions instead of transition actions. | |
Use explicit ordering to control the testing order of a group of outgoing transitions. | You can specify explicit or implicit ordering of transitions. By default, a chart uses explicit ordering. If you switch to implicit ordering, the transition testing order can change when graphical objects move. |
This section describes what happens after you enter the
Checked_in
state, regardless of which substate becomes
active.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | After reaching your desired room, you finish moving your bags. | The |
2 | If you order room service, your hotel bill increases by a constant amount. | If the chart receives an event broadcast for
For reference, see How Stateflow Charts Respond to Events. |
Modeling Guidelines for Execution of State Actions. The following guidelines apply to state actions.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use an | Other types of state actions execute differently and do not apply:
| State Action Types |
Use an | ||
Use a superstate to enclose multiple substates that share the same state actions. | This guideline enables reuse of state actions that apply to multiple substates. You write the state actions only once, instead of writing them separately in each substate. | Create Substates and Superstates |
This part of the chart describes how you can perform function calls while a state is active.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | Based on your room type and the total number of room service requests, you can track your hotel bill. |
If you double-click the function box, you see this script in the function editor: function y = expenses(x) if (room_type == 1) y = 1500 + (x*50); else if (room_type == 2) y = 1000 + (x*25); else y = 500 + (x*5); end end |
Modeling Guidelines for Function Calls. The following guidelines apply to function calls.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use MATLAB functions for performing numerical computations in a chart. | MATLAB functions are better at handling numerical computations than graphical functions, truth tables, or Simulink® functions. | Reuse MATLAB Code by Defining MATLAB Functions |
Use descriptive names in function signatures. | Descriptive function names enhance readability of chart objects. |
This part of the chart shows how a state with exclusive (OR) decomposition executes.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | When you reach the executive suite, you enter the bedroom first. Note The executive suite has separate bedroom and dining areas. Therefore, you can be in only one area of the suite at any time. | When the condition
For reference, see Enter a Chart or State. |
2 | When you order room service, you enter the dining area to eat. | When the |
3 | When you want the food removed from the dining area, you order room service again and then return to the bedroom. | When the |
4 | If you leave the executive suite because of a fire alarm, you return to your previous room after the all-clear signal. | If a transition out of |
Modeling Guidelines for Execution of Exclusive (OR) States. The following guidelines apply to exclusive (OR) states.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use exclusive (OR) decomposition when no two states at that level of the hierarchy can be active at the same time. | This guideline ensures proper chart execution. For example,
| |
If reentry to a state with exclusive (OR) decomposition depends on the previously active substate, use a history junction. This type of junction records the active substate when the chart exits the state. | If you do not record the previously active substate, the default transition occurs and the wrong substate can become active upon state reentry. For example, if you were eating when a fire alarm sounded, you would return to the bedroom instead of the dining room. |
This part of the chart shows how a state with parallel (AND) decomposition executes.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | When your family reaches the suite, family members can be in both bedrooms (for example, parents in the master bedroom and children in the second bedroom). A default room choice does not apply. | When the condition
For reference, see Enter a Chart or State. |
2 | You can occupy both rooms at the same time. |
|
Modeling Guidelines for Execution of Parallel (AND) States. The following guidelines apply to parallel (AND) states.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use parallel (AND) decomposition when all states at that level of the hierarchy can be active at the same time. | This guideline ensures proper chart execution. For example,
| |
Use no history junctions in states with parallel (AND) decomposition. | This guideline prevents parsing errors. Since all parallel states at a level of hierarchy are active at the same time, history junctions have no meaning. | |
Use explicit ordering to control the execution order of parallel (AND) states. | You can specify explicit or implicit ordering of parallel states. By default, a chart uses explicit ordering. If you switch to implicit ordering, the execution order can change when parallel states move. |
This part of the chart describes how events can guard transitions between exclusive (OR) states.
Stage | Hotel Scenario | Chart Behavior |
---|---|---|
1 | If a fire alarm sounds, you leave the hotel and move to a waiting area outside. | When the chart receives an event broadcast for
|
2 | If an all-clear signal occurs, you can leave the waiting area and return to your previous location inside the hotel. | When the chart receives an event broadcast for
The history
junction at each level of hierarchy in |
Modeling Guidelines for Guarding Transitions. The following guideline discusses the use of events versus conditions.
Modeling Guideline | Why This Guideline Applies | Reference |
---|---|---|
Use events, instead of conditions, to guard transitions that depend on occurrences without numerical value. | Because you cannot easily quantify the numerical value of a fire alarm or an all-clear signal, model such an occurrence as an event. | Activate a Stateflow Chart by Sending Input Events |