天天看點

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

14.4.1 Summary(概要)

ProtocolStateMachines are used to express usage protocols. ProtocolStateMachines express the legal sequences of Event occurrences to which the Behaviors of an associated BehavioredClassifier must conform. The  StateMachine notation is a convenient way to define the order of invocations of the behavioral features of a Classifier.

協定狀态機用于表現使用方法的協定。協定狀态機表示事件發生的合法的序列,關聯的行為類目的行為必須遵循這個序列。狀态機記法是一種定義類目的行為特征的調用次序的簡便方式。

ProtocolStateMachines can be associated with Classifiers, Interfaces, and Ports.

協定狀态機可以和類目,接口和端口關聯。

14.4.2 Abstract Syntax

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.41 ProtocolStateMachines

14.4.3 Semantics(語義)

14.4.3.1 ProtocolStateMachine(協定狀态機)

A ProtocolStateMachine is always defined in the context of a Classifier. It specifies which BehavioralFeatures of that Classifier can be invoked in a given protocol state and under what conditions, thereby specifying allowed invocation sequences. In this manner, a specification of the lifecycle of an instance of the Classifier is defined from an external perspective.

協定狀态機總是在類目的上下文中定義。它定義了類目的在一個給定的協定狀态中,什麼條件下,哪個行為特征可以被調用。這樣,從外部觀點定義了類目的某個執行個體的生命周期的規範。

ProtocolStateMachines help define the order in which BehavioralFeatures of a Classifier are invoked by specifying:

協定狀态機幫助定義類目的行為特征被調用的順序,它明确了:

• the behavioral context (i.e., which states and pre-conditions) in which they can be validly invoked,

它們可以被正确調用時的行為上下文(即:哪個狀态和前置條件)

• the valid orderings of invocations,

正确的調用次序,

• the expected outcomes (post-conditions) of invocations.

期待的調用輸出(後置條件)

ProtocolStateMachine present an external view of the owning Classifier as perceived by its collaborators. This extends beyond what can be captured via pre- and post-conditions on individual BehavioralFeatures, as ProtocolStateMachines also specify the valid orderings of invocations of the different features. This is achieved by a state machine specification in which the transition triggers are feature invocations and the guards of the transitions (ProtocolTransitions) specify the pre-condition that must apply for the invocation to be valid. The states (ProtocolStates) of this state machine, being a consequence of past invocation sequences, capture the state of the protocol and are also a form of pre-condition.

協定狀态機向協作者呈現了一個包含協定狀态機的類目的外部視圖。這種擴充依據的是通過獨立的 行為特征的前置條件和後置條件可以捕捉到什麼,協定狀态機也可以定義不同特征的正确的調用次序。這是通過狀态機定義實作的,在這個定義中,為了保證調用的 正确性,功能調用的觸發條件和遷移的定義後置條件的監護條件必須使用以保證調用的正确性。作為過去的調用序列的結果,狀态機的狀态(協定狀态)捕捉協定的 狀态并且也是前置條件的一種形式。

NOTE. Because ProtocolStateMachines provide a “black box” view of the behavior of a Classifier, their States may not necessarily correspond to the States of internal behavioral StateMachines.

因為協定狀态機提供的是類目行為的“黑盒”視圖,狀态不必和内部行為狀态機的狀态相對應。

ProtocolStateMachine interpretation can vary from:

協定狀态機可以有不同的形式:

1 Declarative ProtocolStateMachines, which specify the legal Transitions for BehavioralFeature invocations.The effects of a BehavioralFeature invocation is not specified. This type of specification only provides a contract for the user of the context Classifier.

陳述性協定狀态機,它定義了行為特征調用的有效遷移。不定義行為特征調用的效應。這種類型的定義隻是向上下文類目的使用者提供一種約定。

2 Executable (run time) ProtocolStateMachines, which specify all Event occurrences that an object may receive and handle, together with the Transitions that these trigger. In this case, the legal Transitions for BehavioralFeature invocations must match exactly the triggered Transitions or a run-time exception occurs.The invocation results in the execution of the method associated with the invoked BehavioralFeatures.

可執行(執行時)協定狀态機,它定義了一個對象可以接受和處理的所有事件發生和它們觸發的遷移。在這種情況下,正當的行為特征調用遷移必須和被觸發的遷移或運作時例外完全比對。方法執行的調用結果和被調用的行為特征相關聯。

The specifications for both interpretations is the same, the only difference being the direct dynamic implication that the latter interpretation provides.

兩種解釋的定義都相同,唯一的差別就是後一種解釋提供了直接的動态含義。

The more sophisticated forms of modeling encountered in behavioral StateMachines such as compound Transitions, submachine StateMachines, composite States, and concurrent orthogonal Regions, can also be used for ProtocolStateMachines. For example, concurrent Regions make it possible to express protocols where an instance can have several active States simultaneously. Submachine StateMachines and compound transitions can be used for factorizing complex ProtocolStateMachines.

在行為狀态機中遇到的更加複雜的形式,例如複合遷移,子狀态機,組合狀态,并發正交區域等,也可以用于協定狀态機。例如并發區域可以用于表現一個執行個體同時擁有多個活動狀态的情況。子狀态機和複合遷移可以用于分解複雜的協定狀态機。

A Classifier may have several ProtocolStateMachines. This can be used, for example, when a Classifier has multiple parents, each having its own ProtocolStateMachine, and the protocols are orthogonal. An alternative to this is to simply have one ProtocolStateMachine, with distinct StateMachines in concurrent Regions.

一個類目可以有多個協定狀态機。例如下面的場景:一個類目擁有多個父母(類目),每個擁有自己的協定狀态機,且協定之間為正交關系。另一種方式是隻擁有一個協定狀态機,但是在它的并發區域中包含不同的狀态機。

State in ProtocolStateMachines(協定狀态機中的狀态)

The States of ProtocolStateMachines are exposed to the users of their context Classifiers. A protocol State represents an exposed stable situation of its context Classifier: When an instance of the Classifier classifier is not processing any BehavioralFeature invocation, users of this instance can always know its state configuration.

協定狀态機的狀态被暴露給它們的上下文類目的使用者。一個協定狀态表現了它的上下文類目的一個外部可見的穩定情況:當類目的某個執行個體沒有處理任何的行為特征調用時,那個執行個體的使用者總是知道它的狀态構成。

The States of a ProtocolStateMachine cannot have defined entry, exit, or  doActivity Behaviors.

協定狀态機的狀态不允許定義進入,退出和doActivity行為。

14.4.3.2 ProtocolTransition(協定遷移)

A ProtocolTransition specifies a legal Transition for an invocation of a BehavioralFeature of the context Classifier. ProtocolTransitions have the following features:

協定遷移定義了上下文類目的行為特征在被調用時允許的遷移。協定遷移有如下特征:

· a pre-condition (preCondition), which specializes the guard attribute of Transition,

定義遷移的監護屬性的前置條件,

· a trigger,

觸發條件,

· a post-condition (postCondition).

後置條件。

The protocol Transition specifies that (a) the associated (referred) feature can be invoked on an instance of the context Classifier, if it is in the origin State and the guard condition holds, and that (b) upon completion of the Transition, the instance will be in the target State in which the post-condition will hold.

協定遷移定義了上下文類目的執行個體可以被調用的關聯(引用)特征,如果它位于初始狀态内,有監護條件,被遷移完成觸發,那麼這個執行個體将會處于擁有後置條件的目标狀态中。

ProtocolTransitions do not have an associated effect Behavior. The consequence of a ProtocolTransition executed as a result of a BehavioralFeature invocation is implicit: it is the execution of the method corresponding to the invoked BehavioralFeature. In case of other types of Triggers, the consequences are unspecified except that a Transition will lead to another State under a specific post-condition, regardless of any Behaviors associated with this Transition.

協定遷移沒有關聯的效應行為。作為行為特征調用的結果,協定遷移的意味着與被調用行為特征相關聯的方法的執行。在其他類型觸發條件的情況下,影響是沒有定義的,除非該遷移會引出另外一個滿足特定前置條件的狀态,這時不考慮與這個遷移相關聯的任何行為。

14.4.3.2.1 Unexpected trigger reception(非預期觸發接受)

The interpretation of the reception of an Event occurrence that does not match a valid trigger for the current State, state invariant, or pre-condition is not defined (e.g., it can be ignored, rejected, or deferred; an exception can be raised; or the application can stop on an error). It corresponds semantically to a pre-condition violation, for which no predefined Behavior is defined in UML.

關于沒有和目前狀态,狀态不變量或前置條件比對的事件發生的接受的解釋沒有被定義(即,它可以被忽略,拒絕或推遲;升起異常;或作為錯誤而終止應用的執行)。在語義上它對應于前置條件的違反,在UML中沒有為它設定預先定義的行為。

14.4.3.2.2 Unexpected Behavior(非預期行為)

The interpretation of an unexpected Behavior, that is an unexpected result of a Transition (wrong FinalState or FinalState invariant, or post-condition) is also not defined. However, this should be interpreted as an error of the implementation of the ProtocolStateMachine.

作為遷移的非預期結果(錯誤的完了狀态,或完了狀态不變量,或後置條件),非預期行為的解釋也無定義。然而它應該被解釋成協定狀态機實作的錯誤。

14.4.3.2.3 Equivalences to pre- and post-conditions of operations(操作的前置和後置條件的等價性)

A protocol Transition can be semantically interpreted in terms of pre- and post-conditions on the associated operation.For example, the Transition in 

Figure 14.42 can be interpreted in the following way:

在語義上,協定遷移可以從操作的前置和後置條件兩個方面來解釋。例如圖14.42中的遷移可以按以下方式解釋:

1 The operation “m1” can be called on an instance when it is in the ProtocolState “S1” under the condition “C1.”

當執行個體處于協定狀态"S1",滿足條件“C1”時,操作m1可以被調用。

2 When “m1” is called in the ProtocolState “S1” under the condition “C1,” then the ProtocolState “S2” must be reached under the condition “C2.”

當處于協定狀态“S1”s時,“m1”被調用,那麼滿足條件”C2“時,必須到達協定狀态"S2“。

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.42 An example of a ProtocolTransition associated with the operation "m1"

Operations referred by several Transitions(被多個遷移引用的操作)

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.43 Example of several ProtocolTransitions associated with the same operation (m1)

In a ProtocolStateMachine, several Transitions can refer to the same operation as illustrated in Figure 14.43. In that case, all pre-and post-conditions will be combined in the operation pre-condition as shown below.

圖14.43表示了在協定狀态機中,多個遷移可以引用同一個操作的例子。在這個情況下,所有的前置和後置條件都被組入操作的前置(/後置)條件,表示如下

Operation m1()

Pre: in state S1 and condition C1

or

in state S3 and condition C3

Post: if the initial condition was “in state S1 and condition C1”

then in S2 and C2

else

if the initial condition was “in state S3 and condition C3”

then in S4 and C4

A ProtocolStateMachine specifies all the legal ProtocolTransition for each BehavioralFeature referred by its Transitions.

協定狀态機為它的遷移所引用的每個行為特征定義了所有的協定遷移。

Unreferred Operations(沒有被引用的操作)

If a BehavioralFeature is not referred by any ProtocolTransition, then the operation can be called for any State of the ProtocolStateMachine, and will not change the current State or pre- and post-conditions.

如果行為特征沒有被任何的行為特征引用,那麼該操作可以被協定狀态機的任何狀态調用,并且不會改變目前狀态或前置/後置條件。

14.4.3.2.4 Using other types of Events in ProtocolStateMachines(在協定狀态機中使用其他類型的事件)

Apart from invocations of BehavioralFeatures, other Events may be used for expressing the behavior of ProtocolStateMachines. A Trigger that is not a BehavioralFeature invocation can be specified for a protocol Transition. In that case, this specification is a requirement for the environment external to the ProtocolStateMachine. That is, it is legal to send an Event occurrence of this type to an instance of the context Classifier only under the conditions specified by the ProtocolStateMachine. The precise semantic interpretation of this is not defined.

除了調用行為特征,可以使用另外的事件來表現協定狀态機的行為。不是行為特征調用的觸發條件可以用于定義協定遷移。這種 定義是對于行為狀态機外部環境的需求。也就是說,隻有滿足協定狀态機定義的條件時,向上下文類目發送事件發生才是合乎要求的。與此有關的準确的語義解釋沒 有被定義。

14.4.3.3 ProtocolConformance(協定一緻)

ProtocolStateMachines can be refined into more specific ProtocolStateMachines. Protocol conformance declares that the specific ProtocolStateMachine specifies a protocol that conforms to that specified by the general ProtocolStateMachine.

協定狀态機可以被定義為更加具體的協定狀态機。協定一緻聲明了具體的協定狀态機定義了一個協定,它與泛用協定狀态機定義的協定一緻。

A ProtocolStateMachine is owned by a Classifier. The Classifiers owning a general StateMachine and an associated specific StateMachine are generally also connected by a Generalization or a Realization.

協定狀态機屬于類目。該類目擁有一個泛用狀态機,而且通常情況下,也會通過泛化和實作關聯一個具體的狀态機。

Protocol conformance represents a declaration that every rule and constraint specified for the general ProtocolStateMachine (state invariants, pre- and post-conditions for the operations referred by the ProtocolStateMachine) apply to the specific ProtocolStateMachine.

協定一緻表示一個聲明:為泛用協定狀态機定義的所有規則,限制(狀态不變量,協定狀态機引用的操作的前置/後置條件)都會适用與具體的協定狀态機。

14.4.4 Notation(記法)

14.4.4.1 ProtocolStateMachine(協定狀态機)

The notation for ProtocolStateMachine is very similar to the one for behavioral StateMachines. The keyword «protocol» placed close to the name of the StateMachine differentiates graphically ProtocolStateMachine diagrams.

協定狀态機的記法和行為狀态機非常相似。隻是通過靠近狀态機名稱的«protocol»關鍵詞從圖形上差別了協定狀态機圖。

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.44 ProtocolStateMachine example

The textual expression of an invariant associated with a State in a ProtocolStateMachine is represented by placing it after or under the name of the State, enclosed in square brackets (Figure 14.45).

與協定狀态機中的狀态相關聯的不變量的文字表達式的表示方法,是通過将其封裝入括号中,放到狀态名之後或之下。

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.45 Notation for a State with an invariant

14.4.4.2 ProtocolTransition(協定遷移)

The usual StateMachine notation applies. The difference is that no effect Behaviors are specified for ProtocolTransitions, and that post-conditions can exist. Post-conditions have the same syntax as guard conditions, but appear at the end of the Transition syntax.

使用通常的狀态機中的記法。差別是不會定義效應行為和可以有後置條件。後置條件和監護條件的文法相同,但是遷移文法的最後表示。

EA&UML日拱一卒-活動圖::14.4 ProtocolStateMachines

Figure 14.46 ProtocolTransition notation

參考資料:

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

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

閱讀更多更新文章,請掃描下面二維碼,關注微信公衆号【面向對象思考】

繼續閱讀