UML Statecharts

I have been using an implementation of UML statecharts in C++ similar to that defined in the book "Practical Statecharts in C/C++" by Miro Samek (https://www.state-machine.com/psicc). One of the "problems" I keep encountering in my development over and over, is how to associate an action with a transition that requires data from an event, while not requiring the code for the action to be rewritten in multiple places. Let me illustrate with an example. Suppose I have a state machine with the following state chart (where indents indicate child states): audio fault stopped playing playingFile playingStream and the following events (the items in the curly brackets are data associated with the event): Stop {} Play File {file name} Play Stream {multicast address} In words, the "Stop" event causes a transition from any other state (except the "stopped" state) to the "stopped" state. In the same way, the "Play File" event transitions to the "playingFile" state and the "Play Stream" event transitions to the "playingStream" state. Now, let's look at the "playingFile" state as an example. While in this state (i.e., when it is enetered), a file needs to be opened. While NOT in this state (i.e., when the state is exited), the file needs to be closed. To me, it makes sense to have the logic for opening a file occur within the handling of the special "Enter" event. The logic for closing a file should occur in the handling of the "Exit" event. In this way, the code to open a file (which only the "playingFile" state cares about) is only located within the state itself (not at the transition point). This presents a couple problems though: If an error occurs when opening the file, a transition to the "fault" state should be made. However, transitions are NOT allowed within the special "Enter" event. In order to open a file, the "file name" is required. But this data comes as part of the "Play File" event and is not available when handling the "Enter" event. One option would be to store the variables as part of the state machine's extended state variables (i.e., private variables of the class), but this could result in a LOT of temporary, extended state variables. Thus my question is, what it the best way to handle this problem?

Apr 18, 2025 - 00:57
 0
UML Statecharts

I have been using an implementation of UML statecharts in C++ similar to that defined in the book "Practical Statecharts in C/C++" by Miro Samek (https://www.state-machine.com/psicc).

One of the "problems" I keep encountering in my development over and over, is how to associate an action with a transition that requires data from an event, while not requiring the code for the action to be rewritten in multiple places.

Let me illustrate with an example.

Suppose I have a state machine with the following state chart (where indents indicate child states):

audio
    fault
    stopped
    playing
        playingFile
        playingStream

and the following events (the items in the curly brackets are data associated with the event):

Stop {}
Play File {file name}
Play Stream {multicast address}

In words, the "Stop" event causes a transition from any other state (except the "stopped" state) to the "stopped" state. In the same way, the "Play File" event transitions to the "playingFile" state and the "Play Stream" event transitions to the "playingStream" state.

Now, let's look at the "playingFile" state as an example. While in this state (i.e., when it is enetered), a file needs to be opened. While NOT in this state (i.e., when the state is exited), the file needs to be closed.

To me, it makes sense to have the logic for opening a file occur within the handling of the special "Enter" event. The logic for closing a file should occur in the handling of the "Exit" event. In this way, the code to open a file (which only the "playingFile" state cares about) is only located within the state itself (not at the transition point).

This presents a couple problems though:

  1. If an error occurs when opening the file, a transition to the "fault" state should be made. However, transitions are NOT allowed within the special "Enter" event.
  2. In order to open a file, the "file name" is required. But this data comes as part of the "Play File" event and is not available when handling the "Enter" event. One option would be to store the variables as part of the state machine's extended state variables (i.e., private variables of the class), but this could result in a LOT of temporary, extended state variables.

Thus my question is, what it the best way to handle this problem?