具有嵌套状态的 UML 状态机并执行 Activity

UML State machine with nested states and do Activity

在下面的状态机上,让我们假设我在 "StateE"。然后是 Sig1,因此会发生转换。首先我退出 "StateE",然后假设 "Grd1" 守卫让我通过。

现在我输入"StateH",执行输入动作("Act2")。然后我将一个信号放入队列中以执行 "StateH" 的 "do" 操作。这意味着 "StateH" "do" 操作不必在输入操作后立即执行,但可能会先提供一些其他已经安排好的信号。

然后我继续"StateG"。再次在 "StateG" 中,执​​行入场操作 ("Act2") 并将 "do" 操作安排为信号。

信号调度程序到达信号队列中 StateH 执行操作信号所在的点。

问题是:我们是在执行StateH的do动作之前从StateG退出回到StateH还是可以说:在StateG也就是在StateH,所以不需要状态转换,让我们立即执行 StateH do 操作。

根据 UML Specification(formal-15-03-01,第 14.2.3.4.3 节)

A State may also have an associated doActivity Behavior. This Behavior commences execution when the State is entered (but only after the State entry Behavior has completed) and executes concurrently with any other Behaviors that may be associated with the State, until: • it completes (in which case a completion event is generated) or • the State is exited, in which case execution of the doActivity Behavior is aborted.

这意味着 1) 一旦进入状态(与您的理解相反)就会执行 doActivity 和 2) 在状态处于活动状态时 doActivity 保持 运行。

在您的情况下,StateH 处于活动状态,而 StateG 处于活动状态(请参阅活动状态配置的定义),因此,StateH 的 doActivity 保持 运行 而你在 StateG

OMG Unified Modeling Language 2.5.1中,这个说法有点强:

Also, if a doActivity Behavior is defined for the State, this Behavior commences execution immediately after the entry Behavior is executed. It executes concurrently with any subsequent Behaviors associated with entering the State, such as the entry Behaviors of substates entered as part of the same compound transition.

我不认为这意味着状态机必须是多线程的。假设您在单核处理器上有一个多线程状态机。各种行为和活动仍将按顺序执行,尽管它们可能由单独的线程执行。