State machine pushing events to its own event queue

bgr*_*bgr 4 events state-machine fsm transitions

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. 以下机器将处于A等待事件的状态A_to_B,之后它将通过调度事件作为转换操作进入无限循环:

          +-----+                    +-----+                    +-----+
          |  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     |
             +-----------------------------------------------------+
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过将事件作为入口操作调度,以下机器将立即进入无限循环:

          +-------------------+           +-------------------+           +-----+
          |         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      |
             +---------------------------------------------------------------+
    
    Run Code Online (Sandbox Code Playgroud)

Mir*_*mek 6

状态机可以将事件发布到自己,但这具有特殊目的,例如将更长的运行完成(RTC)步骤分解为更短的部分.您可能希望这样做以在您太长的RTC步骤之间启用系统中其他状态机(或更常见的活动对象)的调度.

具体到你的例子,我会尽量避免在这种情况下将事件发布给自己.通常我会看到人们在将状态图与流程图混淆时执行此操作.状态图需要事件从状态转换到状态.在完成框中指定的计算后,流程图自动从一个处理框转换到另一个处理框.显然,当您向自己发布事件时,您将状态图转换为流程图.所以,你真的需要一个流程图,而不是状态图,因为你真的不需要等待任何事情.你继续全速处理.

您也可以这样查看.事件的目的是为状态机提供新信息.这就是状态机"学习"的方式.但是当你向自己发布事件时,你不会获得任何新知识.您需要的所有知识都已经由原始的"真实"事件提供.因此,您有足够的信息只在一个转换中执行所有这些处理,而不是在许多"状态"中进行扩展,这实际上是这个冗长处理的阶段.