View Differences Between Stateflow Messages, Events, and Data
This example compares the behavior of messages, events, and data in Stateflow®.
This model has three sender charts: DataSender, EventSender, and MessageSender. Each sender chart has one state. In the entry action of the state, the charts assign a value to data, send a function-call event, or send a message.
For each of the sender charts, there is a corresponding receiver chart. Each receiver chart has a state diagram with states A0, A1, A2, and A3. The implicit event after(3,sec) triggers the transition from A0 to A1. The data, event, or message from the corresponding sender chart guards the transitions between A1, A2, and A3.
Each receiver chart has active state output enabled and connected to a scope. The scope shows which states are active in each time step. This output highlights the difference in behavior between output data, events, and messages.
Behavior of Data
The DataSender chart assigns a value of 1 to the output data M, which connects as an input to the DataReceiver chart.
The DataReceiver chart executes once at every time step. At the start of simulation, state A0 is active. At time t=3, the transition from A0 to A1 occurs. At time t=4, the chart tests whether M equals 1. This condition is true, so the chart transitions from A1 to A2. At time t=5, M still equals 1, so the chart transitions from A2 to A3. On the scope, you see that DataReceiver changes states three times.
After data is assigned a value, it holds its value throughout the simulation. Therefore, each time that the DataReceiver evaluates the condition [M == 1], it transitions to a new state.
Behavior of Event
The EventSender chart uses the command send(M) to send a function-call output event to wake up the EventReceiver chart.
The EventReceiver chart executes only when the input event M wakes up the chart. At the start of simulation, state A0 is active. The transition from A0 to A1 is based on absolute-time temporal logic and is not valid at time t=0. A0 remains active and the chart goes back to sleep. Because EventSender sends the event M only once, EventReceiver does not wake up again. On the scope, you see that EventReceiver never transitions out of A0.
Events do not remain valid across time steps, so the receiving chart has only one chance to respond to the event. When EventSender sends the event, EventReceiver is not ready to respond to it. The opportunity for EventReceiver to transition in response to the event is lost.
Behavior of Message
The MessageSender chart uses the syntax send(M) to send a message through the output message port. The message goes into the input message queue of the MessageReceiver chart. The message waits in the queue until MessageReceiver evaluates it.
The MessageReceiver chart executes once at every time step. At the start of simulation, state A0 is active. At time t=3, the transition from A0 to A1 occurs. At time t=4, the chart determines that M is present in the queue, so it takes the transition to A2. At the end of the time step, the chart removes M from the queue. At time t=5, there is no message present in the queue, so the chart does not transition to A3. A2 remains the active state. On the scope, you see that MessageReceiver changes state only two times.
Unlike events, messages are queued. The receiving chart can choose to respond to a message anytime after it was sent. Unlike data, the message does not remain valid indefinitely. The message is destroyed at the end of the time step.