EA&UML日拱一卒-活动图::14.2 Behavior StateMachines (9)

来源:互联网 发布:理查德拉米雷斯 知乎 编辑:程序博客网 时间:2024/06/05 19:56

英文原文来自UML2.5,作者提供中文翻译。


14.2.3.9 Event Processing for StateMachines(状态机事件处理)


14.2.3.9.1 The run-to-completion paradigm(执行到完成样式)


The processing of Event occurrences by a StateMachine execution conforms to the general semantics defined in Clause 13. Upon creation, a StateMachine will perform its initialization during which it executes an initial compound transition prompted by the creation, after which it enters a wait point. In case of StateMachine Behaviors, a wait point is represented by a stable state configuration. It remains thus until an Event stored in its event pool is dispatched. This Event is evaluated and, if it matches a valid Trigger of the StateMachine and there is at least one enabled Transition that can be triggered by that Event occurrence, a single StateMachine step is executed. A step involves executing a compound transition and terminating on a stable state configuration (i.e., the next wait point). This cycle then repeats until either the StateMachine completes its Behavior or until it is asynchronously terminated by some external agent.

状态机执行对于事件发生的处理符合 在13节中定义的通常语义。创建之后,状态机将会执行初期化动作,在这个动作中它会执行一个由创建引起的初始复合迁移,然后它进入等待点。在状态机行为的 场景,等待点通过稳定状态构成来表现。这种状态一直维持直到保存在事件池中的事件被发送。这个事件被处理处理,如果它与状态机的有效的触发条件相匹配而且 至少有一个有效的迁移可以被那个事件发生触发,状态机会执行一步。这一步会启动执行一个复合迁移并停止于一个稳定状态构成(即下一个等待点)。重复这个循 环直至状态机完成了它的行为或者它被某些外部的代理者异步终止。


StateMachines can respond to any of the Event types described in Clause 13 as well as to completion events (see above).

状态机可以对13节中描述的任何事件类型或者完成事件(是参见前面)做出反应。


NOTE. As explained above, completion events have priority and will be dispatched ahead of any pending Event occurrences in the event pool.

注意:前面讲到过,完成事件拥有更高的优先级,会在事件池中等待处理的事件之前被发送。


Event occurrences are detected, dispatched, and processed by the StateMachine execution, one at a time.

事件发生被状态机执行检测,发送和处理,一次一个。


NOTE. The order of event dispatching is left undefined, allowing for varied scheduling algorithms.

事件发送的顺序没有定义,允许不同的调度算法。


This cycle is referred to as the run-to-completion paradigm, and the corresponding StateMachine step is called a run-to-completion step. Run-to-completion means that, in the absence of exceptions or asynchronous destruction of the context Classifier object or the StateMachine execution, a pending Event occurrence is dispatched only after the processing of the previous occurrence is completed and a stable state configuration has been reached. That is, an Event occurrence will never be dispatched while the StateMachine execution is busy processing the previous one. This behavioral paradigm was chosen to avoid complications arising from concurrency conflicts that may arise when a StateMachine tries to respond to multiple concurrent or overlapping events.

这个循环被称为执行到完成样式,对应的状态机步骤被成为执行到完成步骤。执行到完成意味着,除非发 生例外,或者异步析构一个上下文类目或状态机执行,等待中的事件发生只有在前一次事件发生处理完成而且到达了稳定状态构成时才能被发送。也就是说,在状态 机执行正忙于处理前一个事件时,另一个事件发生永远不会被发送。选择这个行为样式是为了避免当状态机响应多个并发或重叠的事件时引起并发冲突而导致的复杂 度上升。


When an Event occurrence is detected and dispatched, it may result in one or more Transitions being enabled for firing.If no Transition is enabled and the corresponding Event type is not in any of the deferrableTriggers lists of the active state configuration, the dispatched Event occurrence is discarded and the run-to-completion step is completed trivially.

当一个事件发生被检测并发送,它可能导致一个或多个有效的迁移被触发。如果没有迁移处于有效状态而且相应的事件类型不在活动状态构成的任何一个可延期触发条件列表中,发送的事件发生被抛弃,该执行到完成步骤结束而不做任何事情。


Due to the presence of orthogonal Regions, it is possible that multiple Transitions (in different Regions) can be triggered by the same Event occurrence. The order in which these Transitions are executed is left undefined. Each
orthogonal Region in the active state configuration that does not contain nested orthogonal Regions (i.e., a “bottomlevel” Region) can fire at most one Transition as a result of the current Event occurrence. When all orthogonal Regions have finished executing the Transition, the current Event occurrence is fully consumed, and the run-to-completion step is completed.

由于正交区域的存在,可能有(处于不同区域的)多个迁移被同一 个事件发生触发。迁移被执行的次序没有被定义。作为当前事件的结果,处于活动状态构成中的每个不含嵌套正交区域的正交区域(即底层区域)可以触发至多一个 迁移。当所有的正交区域的迁移执行都已经结束,当前的事件发生被完全消费,该执行到完成步骤结束。


As mentioned above, it is possible for multiple mutually exclusive Transitions in a given Region to be enabled for firing by the same Event occurrence. In those cases, only one is selected and executed. Which of the enabled Transitions is chosen is determined by the Transition selection algorithm described below.

如上面所讲,在一个给定的区域中,可能有多个互斥的迁移变为有效而准备被同一个事件发生触发。在这种情况下,只有一个被选中而执行。哪个有效迁移会被选中决定于下面描述的选择算法。


During a Transition, a number of actions Behaviors may be executed. If such a Behavior includes a synchronous invocation call on another object executing a StateMachine, then the Transition step is not completed until the invoked
object method completes its run-to-completion step. 

在迁移过程中,可以有一系列的动作行为被执行。如果这样一个行为包括一个对另外一个使用状态机的对象的同步调用,那么迁移步骤直到被调用的对象方法结束后,它的执行到完成步骤之后才会结束。


Run-to-completion may be implemented in various ways. For active Classes, it may be realized by an event-loop running in its own thread, and that reads event occurrences from a pool. For passive Classes it may be implemented
using a monitor.

执行到完成可以通过不同的方式实现。对于主动类,可以通过它自己线程内部的事件循环来实现,并且通过它从池中读取事件发生。对于被动类,可以通过监视者实现。


IMPLEMENTATION NOTE. Run-to-completion is often mistakenly interpreted as implying that an executing StateMachine cannot be interrupted, which, of course would lead to priority inversion issues in some time-sensitive systems. However, this is not the case; in a given implementation a thread executing a StateMachine step can be suspended, allowing higher-priority threads to run, and, once it is allocated processor time again by the underlying thread scheduler, it can safely resume its execution and complete its event processing.

实现注意事项:执行到完成通常被错误的解释,认为执行中的状态机不能被中 断,因此会在某些时间敏感的系统中导致优先级倒置的问题。但实际上不会发生那样的情况;对于一个给定的实现,执行状态机步骤的线程可以被阻塞,允许高优先 级的线程执行,而且,当底层的线程调度再次为其分配处理时间时,可以安全地恢复执行并完成它的事件处理。


14.2.3.9.2 Enabled Transitions(有效迁移)


A Transition is enabled if and only if:

迁移只有在下列情况才是有效的:


• All of its source States are in the active state configuration.

它所有的源状态都处于活动状态构成中。


• At least one of the triggers of the Transition has an Event that is matched by the Event type of the dispatched Event occurrence. In case of Signal Events, any occurrence of the same or compatible type as specified in the Trigger will match. If one of the Triggers is for an AnyReceiveEvent, then either a Signal or CallEvent satisfies this Trigger, provided that there is no other Signal or CallEvent Trigger for the same Transition or any other Transition having the same source Vertex as the Transition with the AnyReceiveEvent trigger (see also 13.3.1).

至少有一个迁移的触发条件得到了通过分发的事件发生的事件类型匹配的事件。在单个事件的情况下,任何被触发条件定义的相同 或兼容类型的事件发生都会被匹配。如果有一个对象为AnyReceiveEvent的触发条件,那么信号或调用事件都可以满足这个触发条件,只要同一个迁 移没有其他的信号或事件触发条件,同时没有其他的同源迁移具有AnyReceiveEvent触发条件(参见13.3.1)。


• If there exists at least one full path from the source state configuration to either the target state configuration or to a dynamic choice Pseudostate in which all guard conditions are true (Transitions without guards are treated as
if their guards are always true).

如果存在至少一个完整路径从源状态构成到目标状态构成或者监护条件为真的动态选择伪状态(没有监护条件的迁移被认为监护条件永远为真)


As more than one Transition may be enabled by the same Event occurrence, being enabled is a necessary but not sufficient condition for the firing of a Transition.

可以有多于一个迁移因为同一个事件变成有效状态,所以变成有效状态是触发迁移的必要条件而不是充分条件。


14.2.3.9.3 Conflicting Transitions(迁移冲突)


It is possible for more than one Transition to be enabled within a StateMachine. If that happens, then such Transitions may be in conflict with each other. For example, consider the case of two Transitions originating from the same State, triggered by the same event, but with different guards. If that event occurs and both guard conditions are true, then at most one of those Transitions can fire in a given run-to-completion step.

在一个状态机中可能存在多个有效的迁移。如果这样的情况发生了,那么这些迁移就可能彼此冲 突。例如,考虑发源于同一个状态的两个迁移的情况,它们接受同一个事件,但是有着不同的监护条件。如果那个事件发生并且两个监护条件的结果都为真,那么在 一个给定的执行到完成步骤中至多有一个迁移被触发。


Two Transitions are said to conflict if they both exit the same State, or, more precisely, that the intersection of the set of States they exit is non-empty. Only Transitions that occur in mutually orthogonal Regions may be fired simultaneously. This constraint guarantees that the new active state configuration resulting from executing the set of Transitions is well formed.

如果两个迁移都源于同一个状态,或者更加准确地讲,它们的源状态集合之间有交集,就可以说它们彼此冲突。只有发生于彼此正交的区域中的迁移可以被同时触发。这个约束保证一组迁移执行以后新的活动状态构成是结构良好的。


An internal Transition in a State conflicts only with Transitions that cause an exit from that State.

状态中内部迁移只会和引起状态退出的迁移发生冲突。


14.2.3.9.4 Firing priorities(触发优先级)


In situations where there are conflicting Transitions, the selection of which Transitions will fire is based in part on an implicit priority. These priorities resolve some but not all Transition conflicts, as they only define a partial ordering. The priorities of conflicting Transitions are based on their relative position in the state hierarchy. By definition, a Transition originating from a substate has higher priority than a conflicting Transition originating from any of its containing States.

在存在冲突迁移的情况下,哪个迁移被触发某种程度上有隐含的优先级决定的。因为只定义了部分的顺序, 所以这个优先级只可以解决某些但不是全部的迁移冲突。冲突迁移的优先级决定于它们在状态层级中的相对位置。根据定义,起源于子状态的迁移的优先级要高于与 之冲突的,起源于包含那个子状态的状态的迁移。


The priority of a Transition is defined based on its source State. The priority of Transitions chained in a compound transition is based on the priority of the Transition with the most deeply nested source State.

迁移的优先级取决于它的源状态。复合迁移中的迁移链的优先级决定于有着最深嵌套层级的源状态。


In general, if t1 is a Transition whose source State is s1, and t2 has source s2, then:

通常情况下,如果t1是源自状态s1的迁移,t2是源自状态s2的迁移,那么:

• If s1 is a direct or indirectly nested substate of s2, then t1 has higher priority than t2.

如果s1直接或间接地被嵌套于s2,那么t1的优先级高于t2.
• If s1 and s2 are not in the same state configuration, then there is no priority difference between t1 and t2.

如果s1和s2没有处于同一个状态构成,那么t1,t2的优先级相同。


14.2.3.9.5 Transition selection algorithm(迁移选择算法)


The set of Transitions that will fire are the Transitions in the Regions of the current state configuration that satisfy the following conditions:

在当前状态构成的区域中满足下列条件的迁移集合将会被触发:


• All Transitions in the set are enabled.

所有属于集合的迁移都处于有效状态


• There are no conflicting Transitions within the set.

集合中没有冲突迁移


• There is no Transition outside the set that has higher priority than a Transition in the set (that is, enabled Transitions with highest priorities are in the set while conflicting Transitions with lower priorities are left out).

集合外不存在优先级高于集合内迁移的迁移(也就是说,具有最高优先级的有效迁移都在集合里而优先级较低的冲突迁移留在了集合外边)。

This can be implemented by a greedy selection algorithm, with a straightforward traversal of the active state configuration. States in the active state configuration are traversed starting with the innermost nested simple States and
working outwards. For each State at a given level, all originating Transitions are evaluated to determine if they are enabled. This traversal guarantees that the priority principle is not violated. The only non-trivial issue is resolving
Transition conflicts across orthogonal States on all levels. This is resolved by terminating the search in each orthogonal State once a Transition inside any one of its components is fired. 

这可以通过使用贪心算法直接遍历活动状态构成来实 现。可以从最内层嵌套的简单状态开始向外,遍历活动状态构成中的状态。对于给定层级的每个状态,所有的初始迁移都被确认以决定是否有效。这个遍历保证优先 级规则不会被违反。唯一有意义的问题是解决所有层次中跨越正交状态的迁移冲突。这个问题是这样解决的:只要状态的任何一个元素中的迁移被触发,就停止正交 状态中的搜索。


14.2.3.9.6 Transition execution sequence(迁移执行顺序)


Every Transition, except for internal and local Transitions, causes exiting of a source State, and entering of the target State. These two States, which may be composite, are designated as the main source and the main target of a Transition respectively.

除了内部或局部迁移的所有迁移,都会引起退出源状态和进入目标状态。这两个状态,可能是组合状态,被分别定义为总起源状态和总目标状态。


The main source is a direct substate of the Region that contains the source States, and the main target is the substate of the Region that contains the target States.

总起源状态是包含源状态的区域的直接子状态,而总目标状态是包含所有目标状态的区域的子状态。


NOTE. A Transition from one Region to another in the same immediate enclosing composite State is not allowed.

注意:从处于直接属于同一个组合状态的一个区域迁移到另一个区域是不允许的。


Once a Transition is enabled and is selected to fire, the following steps are carried out in order:

当一个迁移处于有效状态并被选中而触发,下面的步骤被按顺序执行:

1 Starting with the main source State, the States that contain the main source State are exited according to the rules of State exit (or, composite State exit if the main source State is nested) as described earlier.

从总起源状态开始,包含总起源状态的状态根据状态的退出规则退出(或者如果总起源状态是被嵌套的,那么包含它的组合状态退出)。


2 The series of State exits continues until the first Region that contains, directly or indirectly, both the main source and main target states is reached. The Region that contains both the main source and main target states is called their least common ancestor. At that point, the effect Behavior of the Transition that connects the subconfiguration of source States to the sub-configuration of target States is executed. (A “sub-configuration” here refers to that subset of a full state configuration contained within the least common ancestor Region.)

一系列的状态退出动作继续执行直至到达同时包含(直接或间接)总起源状态和总目标状态的区域。同时包含总起源状态和总目标 状态的区域被称作它们的最小共同祖先。这时,连接源状态子构成和目标状态子构成的迁移的效应行为被执行。(子构成在这里指的是完整的状态构成中被最小共同 祖先区域包含的那部分。)


3 The configuration of States containing the main target State is entered, starting with the outermost State in the least common ancestor Region that contains the main target State. The execution of Behaviors follows the rules
of State entry (or composite State entry) described earlier. 

包含总目标状态的状态构成的进入动作,从包含总目标状态的最小共同祖先区域中的最外层状态开始。这些行为的执行遵守前面定义的状态进入(或组合状态进入)的原则。


This transition execution algorithm is illustrated by the StateMachine example in Figure 14.2. In this case, when event “sig” is dispatched while the StateMachine is in State “S11” (the main source), the following sequence of actions will
be executed:

迁移执行算法通过图14.2中的状态机例子来说明。在这个例子中,当状态机处于状态S11(总起源状态)时,事件“sig"被发送,动作将会按照下面的顺序被执行:

xS11; t1; xS1; t2; eT1; eT11; t3; eT111


参考资料:

《OMG Unified Modeling Language TM (OMG UML) Version 2.5》

  URL:http://www.omg.org/spec/UML/2.5

阅读更多更新文章,请扫描下面二维码,关注微信公众号【面向对象思考】


原创粉丝点击