State machine pushing events to its own event queue
Asked Answered
D

1

5

I am currently researching Hierarchical State Machines (UML State Machines, Statecharts, etc.), and the following is unclear to me:

Is pushing events to machine's own event queue during transitions and from states valid, and if it is, is it safely used in practice or should it be avoided? Are there certain implications when doing this (implementation quirks at least, problems when orthogonal regions come into play, or similar)?

I will illustrate the question with two dummy machines:

  1. the following machine would be in state A waiting for event A_to_B, after which it'd get into infinite loop by dispatching events as transition actions:

          +-----+                    +-----+                    +-----+
          |  A  |  A_to_B /          |  B  |  B_to_C /          |  C  |
          |-----|   dispatch B_to_C  |-----|   dispatch C_to_A  |-----|
    O---->|     +------------------->|     +------------------->|     |
          |     |                    |     |                    |     |
          +-----+                    +-----+                    +-----+
             ^                                C_to_A /             |
             |                                 dispatch A_to_B     |
             +-----------------------------------------------------+
    
  2. the following machine would immediately get into infinite loop by dispatching events as entry actions:

          +-------------------+           +-------------------+           +-----+
          |         A         |           |         B         |           |  C  |
          |-------------------|  A_to_B   |-------------------|  B_to_C   |-----|
    O---->| on entry:         +---------->| on entry:         +---------->|     |
          |  dispatch A_to_B  |           |  dispatch B_to_C  |           |     |
          |                   |           |  dispatch C_to_A  |           |     |
          +-------------------+           +-------------------+           +-----+
             ^                                                               |
             |                                                   C_to_A      |
             +---------------------------------------------------------------+
    
Decagon answered 22/6, 2013 at 17:5 Comment(3)
I don't know wth is going on. But if you are queuing events then it's best to separate the processes to separate processors/cores. You can then control the priority of each. Shared memory could be useful too.Keos
+ for ASCII drawing to explain question, How do you draw?Causative
I used asciiflow.com to draw theseDecagon
M
7

A state machine can post events to self, but this has special purposes, such as to break up longer run to completion (RTC) steps into shorter pieces. You might want to do this to enable scheduling of other state machines in the system (or more generally active objects) in between your otherwise too long RTC step.

Specifically to your examples, I would try to avoid posting events to self in this case. Typically I see people do this when they confuse a statechart with a flowchart. A statechart needs events to transition from state to state. A flowchart transitions from one processing box to another automatically upon completion of the computation specified in the box. Obviously, when you post events to self, you turn a statechart into a flowchart. So, you really need a flowchart and and not a statechart, because you don't really wait for anything. You keep processing at full speed.

You can also view it this way. The purpose of events is to provide new information to the state machine. This is how a state machine "learns". But when you post events to self, you don't acquire any new knowledge. All the knowledge you need is already there delivered by the original "true" event. So, you have enough information to perform all this processing in just one transition instead of spreading it among many "states", which really are stages of this lengthy processing.

Manic answered 23/6, 2013 at 2:13 Comment(1)
Wow, an answer by no one else but Miro Samek himself! Your material on UML Statecharts is indispensable and has helped me a lot during my research on the topic. Indeed, this is why I initially asked, it struck me as the equivalent of "code smell" for state machines. Since I'll be implementing the HSM framework, I want to keep it as light as possible but still powerful enough. Along with it I'm designing a moderately complex GUI application as a proof-of-concept, and my lack of experience with HSMs makes me doubt whether I need more powerful features or my design is wrong :)Decagon

© 2022 - 2024 — McMap. All rights reserved.