Main Content

How Stateflow Objects Interact During Execution

During execution, Stateflow® objects interact with each other to simulate real-world behavior. This example uses the hotel check-in process to explain how common graphical and nongraphical objects in a Stateflow chart interact during execution.

Model of the Check-In Process for a Hotel

This model contains a Stateflow chart called Hotel. The chart receives input events from four Manual Switch (Simulink) blocks that you toggle to:

  • Check in to the hotel

  • Call room service

  • Set off a fire alarm

  • Send an all-clear signal after a fire alarm

A Mux (Simulink) block combines these input events into a vector of inputs that connects to the trigger port on the top side of the chart.

The chart also receives an input signal called room_type from a Multiport Switch (Simulink) block. The value for this signal corresponds to the type of room you want to stay in. The possible options are "Executive" for an executive suite, "Family" for a family suite, and "Single" for a single room.

During simulation, the total amount due, including charges for room service, appears in the Display (Simulink) block.

The Hotel chart contains graphical objects, such as states and history junctions, and nongraphical objects, such as data and events. To see an image that labels the objects in this chart, see Stateflow Objects.

When you start the simulation, the chart does not wake up until it detects a rising or falling edge in one of its input events.

When you toggle a Manual Switch block, you trigger an input event that wakes up the chart. While the chart is awake, it reads a value for the chart input room_type from the Multiport Switch block, performs any valid state or transition actions, and outputs the new value of fee to the Display block.

After completing all possible phases of execution, the chart goes back to sleep and waits for the next input event.

Chart Initialization

Start the simulation and trigger one of the input events. This action corresponds to entering the hotel and stopping at the front desk.

Because the chart property Execute (enter) chart at initialization is disabled, the chart remains asleep until it detects a rising or falling edge in one of its input events. Then the chart wakes up and executes its default transitions. The default transition to the state Check_in occurs, making that state active. Then, the default transition to the substate Front_desk occurs, making that state active. Then the chart goes to sleep. For more information, see Execution of a Chart at Initialization and Enter a Chart or State.

Transition Between States

While the substate Front_desk is active, trigger the input event check_in. This action corresponds to checking in to the hotel. You pick up your bags, move from the front desk to your room, and put your bags down.

In the chart, the check_in event guards the outgoing transition from the substate Front_desk to the substate Checked_in. When you trigger the event, the transition becomes valid. The exit action of Front_desk sets the value of the local data object move_bags to 1 and the substate becomes inactive. Then, Checked_in becomes active and the entry action sets move_bags to 0. For more information, see How Stateflow Charts Respond to Events, Exit a State, and Enter a Chart or State.

Evaluation of Default Transition Paths

After the chart executes the entry actions in the Checked_in state, it evaluates the default transition path to one of the substates. The substate that becomes active corresponds to the room type. If you choose an executive suite, the base fee is $1500. If you choose a family suite, the base fee is $1000. If you choose a single room, the base fee is $500.

The chart tests the branches of the default transition path in this order:

  • If the chart input room_type equals "Executive", the top transition is valid. The condition action sets the chart output fee to 1500 and the substate Executive_suite becomes active.

  • If the chart input room_type equals "Family", the middle transition is valid. The condition action sets the fee to 1000 and the substate Family_suite becomes active.

  • Otherwise, the chart input room_type equals "Single" and the bottom transition is valid. The condition action sets the fee to 500 and the substate Single_room becomes active.

For more information, see Order of Execution for a Set of Flow Charts.

Execution of States with Exclusive Substates

If you trigger the input event check_in while the value of the chart input room_type is "Executive", the substate Executive_suite becomes active. This substate corresponds to staying in the executive suite. This suite has separate bedroom and dining areas, so you can be in only one area of the suite at any time. When you reach the executive suite, you enter the bedroom first. When you order room service, you enter the dining area to eat. When you want the food removed from the dining area, you order room service again and then return to the bedroom.

The state Executive_suite has exclusive (OR) decomposition. The state has two substates, Bedroom and Dining_area. When Executive_suite first becomes active, the default transition to Bedroom occurs, making that substate active. A broadcast of the input event room_service triggers the transition from Bedroom to Dining_area, making Bedroom inactive and Dining_area active. A subsequent broadcast of room_service triggers the transition back from Dining_area to Bedroom, making Bedroom active and Dining_area inactive. For more information, see Enter a Chart or State.

Execution of State with Parallel Substates

If you trigger the input event check_in while the value of the chart input room_type is "Family", the substate Family_suite becomes active. This substate corresponds to staying in the family suite. When your family reaches the suite, family members can spend time in two bedrooms. For example, the parents can watch a movie in the first bedroom while the children sleep in the second bedroom.

The state Family_suite has parallel (AND) decomposition. The state has two substates, First_bedroom and Second_bedroom. When Family_suite becomes active, the parallel states wake up according to their execution order, as indicated by the number in the upper right corner of each state. The substates remain active at the same time. For more information, see Execution Order for Parallel States and Enter a Chart or State.

Function Call from a State Action

While the substate Checked_in is active, trigger the input event room_service. This action corresponds to calling for room service. Your hotel bill depends on your room type and the number of room service requests you make.

When the chart detects a rising or falling edge in the input event room_service, the Checked_In state executes the on action for this event. The state increments the local data object service and calls the MATLAB® function expenses. This function takes the total number of room service requests as an input and returns the current hotel bill as an output. For more information, see Control Chart Execution by Using Event Actions in a Superstate.

Execution of States with History Junctions

While the substate Checked_in is active, trigger the input event fire_alarm, which corresponds to setting off a fire alarm. You leave the building and wait outside in the designated waiting area. Then trigger the input event all_clear, which corresponds to sending an all-clear signal that allows you to return to your previous location inside the hotel.

When the chart receives an event broadcast for fire_alarm, the transition from Check_in to Waiting_area occurs. The history junctions in Check_in, Checked_in, and Executive_suite record the last active substates in each of these states. The active states become inactive in ascending order of hierarchy, starting with the innermost substates. After Check_in becomes inactive, the Waiting_area becomes active.

When the chart receives an event broadcast for all_clear, the transition from Waiting_area to the previously active substate of Check_in occurs. Waiting_area becomes inactive before the substates of Check_in become active in descending order of hierarchy, starting with Check_in.

For more information, see How Stateflow Charts Respond to Events, Exit a State, and Enter a Chart or State.

See Also

(Simulink) | (Simulink) | (Simulink) | (Simulink)

Related Topics