umlstate-machinestatechart

How to diagram state machine that changes next state based on previously received events


I do not know if there is a good way to represent this in a UML2 statechart diagram.

Given a set of states, State 1, State 2, State 3, and State 4. Given a set of events, Event 2, Event 3, and Event 4.

States have a relative priority indicated by the State number. A higher number is higher priority. Receiving an event with a number that matches the state number toggles if that state is potentially active. Given that State 1 is a default priority, there is no event to toggle this priority.

So, for the following sequence of events I would expect the following transition:

  1. State machine initialization -> Enter State 1
  2. Event 2 -> State 2 becomes potentially active. Transition from State 1 to State 2.
  3. Event 4 -> State 4 becomes potentially active. Transition from State 2 to State 4.
  4. Event 3 -> State 3 becomes potentially active. No transition.
  5. Event 4 -> State 4 leaves potentially active. Transition from State 4 to State 3.
  6. Event 2 -> State 2 leaves potentially active. No transition.
  7. Event 3 -> State 3 leaves potentially active. Transition from State 3 to State 1.

The only way that I can think of to diagram this as a statechart is to have an orthogonal state. One side captures events to determine if each state is potentially active. The other side would have to transition based on the current set of states in the first side. This seems pretty ugly and confusing.

Is there a good way to represent this in a UML2 statchart diagram?


Solution

  • Well, it can be done with orthogonal states. However, as you say, it's pretty ugly. First of all, modeling a simple boolean variable with an orthogonal region and two states is really overkill (maybe you didn't plan to do that, but I have seen it many times). And secondly, because the selection of the right state is actually an algorithm and an activity diagram suites this much better.

    So my solution looks like this: four states The internal transitions in state 3 and 4 just consume the event without doing anything, thereby preventing that the state is left and reentered unnecessarily.

    I think the interesting part of this model will be how the behavior differs in the different states. By moving the selection of the state into another diagram, these two concerns are nicely separated.