【翻譯】What is State Machine Diagram(什麼是狀態機圖)?

魯迅認識的那隻猹發表於2018-09-06

【翻譯】What is State Machine Diagram(什麼是狀態機圖)?

寫在前面

在上一篇學習類圖的時候將這個網站上的類圖的一篇文章翻譯了出來,感覺受益良多,今天來學習UML狀態機圖,在網站找了很多的部落格,但是都有些雷同的現象,所以又計劃從該網站上學習UML狀態機圖,翻譯出來以供大家參考。下面是原文連結: www.visual-paradigm.com/guide/uml-u…

正文

The behavior of an entity is not only a direct consequence of its inputs, but it also depends on its preceding state. The past history of an entity can best be modeled by a finite state machine diagram or traditionally called automata. UML State Machine Diagrams (or sometimes referred to as state diagram, state machine or state chart) show the different states of an entity. State machine diagrams can also show how an entity responds to various events by changing from one state to another. State machine diagram is a UML diagram used to model the dynamic nature of a system.

一個實體的行為不僅是其輸入的直接的結果,而且還取決於其之間的狀態。一個實體的過去的歷史能夠通過有限狀態機圖或者傳統上被稱為自動機(automata(不知道是個什麼東東,還望大佬解惑。))的圖來建模。UML狀態機圖(UML State Machine Diagram)(有時被稱為 狀態圖(state diagram), 狀態機(stae machine) 或 狀態圖(state chart))展示了一個實體的不同的狀態。狀態機圖同樣也可以展示一個實體如何通過從一個狀態到另一個狀態的改變來相應各種各樣的事件。

01-uml-state-machine-diagram-in-uml-hierarchy

Why State Machine Diagrams(為什麼是狀態機圖)?

State machine diagram typically are used to describe state-dependent behavior for an object. An object responds differently to the same event depending on what state it is in. State machine diagrams are usually applied to objects but can be applied to any element that has behavior to other entities such as: actors, use cases, methods, subsystems systems and etc. and they are typically used in conjunction with interaction diagrams (usually sequence diagrams).

狀態機圖經常地用來描述一個物件的狀態依賴行為。 一個物件會依據其當前所處的狀態來以不同的方式響應同樣的事件。狀態機圖通常被應用於物件,但是也可以被應用於對其他實體有行為的任何元素比如: 參與者(actors)、 用例(use case)、 方法(methods)、 (子系統(subsystems systems))等。他們經常與互動圖被結合使用(通常是時序圖(sequence diagram))。

For example: 舉例來說:

Consider you have $100,000 in a bank account. The behavior of the withdraw function would be: balance := balance - withdrawAmount: ; provided that the balance after the withdrawal is not less than $0; this is true regardless of how many times you have withdrawn money from the bank. In such situations, the withdrawals do not affect the abstraction of the attribute values, and hence the gross behavior of the object remains unchanged.

假設你的銀行賬戶上有 $100,000刀。 withdraw(取錢)函式的行為將是: balance := balance - withdrawAmount; 假設 取錢之後的餘額不小於 $0 ; 不管你從銀行取了多少錢,這都是真的。 在這種情況下,取錢行為不會影響到屬性值的抽象,因此該物件的整體行為保持不變。

However, if the account balance would become negative after a withdrawal, the behavior of the withdraw function would be quite different. This is because the state of the bank account is changed from positive to negative; in technical jargon, a transition from the positive state to the negative state is fired.

不管怎樣,如果 賬戶餘額在取錢之後變為了負數,withdraw(取錢)函式的行為將會變為十分不同。這是因為銀行賬戶的狀態從積極狀態(positive)改變為了消極狀態(negative); 用專業術語來表示就是,一個從積極狀態到消極狀態的改變被觸發。

The abstraction of the attribute value is a property of the system, rather than a globally applicable rule. For example, if the bank changes the business rule to allow the bank balance to be overdrawn by 2000 dollars, the state of the bank account will be redefined with condition that the balance after withdrawal must not be less than $2000 in deficit.

屬性值的抽象是系統的一個屬性,而不是全域性適用的規則。 舉例來說,如果銀行改變了業務規則為允許銀行餘額透支2000美刀,銀行賬戶的狀態將會以該條件重新定義,該條件是取款後的餘額必須不低於$2000刀的赤字。

Note That(注意):

  • A state machine diagram describes all events (and states and transitions for a single object)
  • 一個狀態機圖描述了所有的事件(還有單個物件的狀態和轉移)
  • A sequence diagram describes the events for a single interaction across all objects involved
  • 一個時序圖描述了一次單一的互動相關聯的所有的物件的事件

譯者注: 狀態機圖以物件為中心,時序圖以互動為中心

Basic Concepts of State Machine Diagram(狀態機圖的基本概念)

What is a State(什麼是一個狀態)?

Rumbaugh defines that(Rumbaugh這樣定義):

譯者注:Rumbaugh 全名為 James Rumbaugh UML的三位創始人之一,詳情可參考這篇文章.

"A state is an abstraction of the attribute values and links of an object. Sets of values are grouped together into a state according to properties that affect the gross behavior of the object."

“一個狀態是一個物件的屬性值和連結的抽象。 根據影響物件整體行為的屬性,一組值將會被合成為一個狀態。”

State Notation(狀態的圖例)

02-state-notations

Characteristics of State Machine Notations(狀態機圖例的特性)

There are several characteristics of states in general, regardless of their types:

不管他們是什麼型別的,一般來說,他們都有這麼幾個特性:

  • A state occupies an interval of time.
  • 一種狀態佔據一個時間段
  • A state is often associated with an abstraction of attribute values of an entity satisfying some condition(s).
  • 狀態經常與一個滿足某些條件的實體的屬性值的抽象向關聯。
  • An entity changes its state not only as a direct consequence of the current input, but it is also dependent on some past history of its inputs.
  • 一個實體改變它的狀態不僅是當前輸入的結果,同樣也依賴於它的輸入的一些過去的歷史。

State(狀態)

A state is a constraint or a situation in the life cycle of an object, in which a constraint holds, the object executes an activity or waits for an event.

狀態是一個物件的生命週期中的一種約束或者情況,在該約束下,該物件執行一個動作或者等待一個事件。

A state machine diagram is a graph consisting of: 一個狀態機圖是一個包含下列元素的圖表:

  • States (simple states or composite states)
  • 狀態(單一狀態或者複合狀態)
  • State transitions connecting the states
  • 連線該狀態的狀態轉換(State transitions)

Example(例如):

03-state-notation

Characteristics of State(狀態的特性)

  • State represent the conditions of objects at certain points in time.
  • 狀態表示物件在某一時間點的情況(conditions)。
  • Objects (or Systems) can be viewed as moving from state to state
  • 物件(或系統)可以被視為從一個狀態轉移到另一個狀態
  • A point in the lifecycle of a model element that satisfies some condition, where some particular action is being performed or where some event is waited
  • 模組元素的宣告週期中的一個滿足某些條件的一個點,這個點是執行某些特定動作或者等待某些事件的點

Initial and Final States(初始狀態和最終狀態)

  • The initial state of a state machine diagram, known as an initial pseudo-state, is indicated with a solid circle. A transition from this state will show the first real state
  • 狀態機圖的初始狀態,被認為是一個偽狀態,以一個實心的圓形表示,從此狀態的一個轉移(transition)將顯示第一個真正的狀態。
  • The final state of a state machine diagram is shown as concentric circles. An open loop state machine represents an object that may terminate before the system terminates, while a closed loop state machine diagram does not have a final state; if it is the case, then the object lives until the entire system terminates.
  • 狀態機圖的最終狀態被顯示為一個同心圓。一個開環迴路(open loop)狀態機代表著一個在系統終止之前可以被終止的物件,雖然一個閉環迴路(closed loop)狀態機不存在終止狀態; 如果處於這種情況,該物件將生存到系統終止的時候為止。

Example(例如):

04-start-and-final-state-example

Events(事件)

An event signature is described as Event-name (以逗號分隔的引數列表 (comma-separated-parameter-list)). Events appear in the internal transition compartment of a state or on a transition between states. An event may be one of four types: 事件的簽名是 Event-name(comma-separated-parameter-list). 事件出現於一個狀態的內部過度區域或者狀態之間的轉移(transition)中。一個事件可能有一下四個型別:

  • Signal event - corresponding to the arrival of an asynchronous message or signal
  • 訊號事件 - 與非同步訊息或者訊號的到達想對應
  • Call event - corresponding to the arrival of a procedural call to an operation
  • 呼叫事件 - 與對於一個操作的程式上的呼叫的到達向對應
  • Time event - a time event occurs after a specified time has elapsed
  • 時間事件 - 時間事件發生在一個一個特定的時間消逝之後
  • Change event - a change event occurs whenever a specified condition is met
  • 改變事件 - 每當特殊的條件被滿足,一個改變事件將會發生

Characteristics of Events(事件的特性)

  • Represents incidents that cause objects to transition from one state to another.
  • 代表引起物件從一個狀態到一個狀態轉移的事件
  • Internal or External Events trigger some activity that changes the state of the system and of some of its parts
  • 內部或外部的事件引發一些活動,這些活動會改變系統和其本身的部分狀態
  • Events pass information, which is elaborated by Objects operations. Objects realize Events
  • 事件傳遞資訊,這些資訊被物件詳細說明,物件實現事件
  • Design involves examining events in a state machine diagram and considering how those events will be supported by system objects
  • 設計在狀態機圖中涉及到的檢查事件(examining events) 並且考慮這些物件將如何被系統物件所支援。

Transition(轉移)

Transition lines depict the movement from one state to another. Each transition line is labeled with the event that causes the transition. 轉移線描述了從一個狀態到另一個狀態的活動。每一個轉移線都會被引發該轉移的事件所標記。

  • Viewing a system as a set of states and transitions between states is very useful for describing complex behaviors
  • 將系統視為一系列的狀態和狀態之間的轉移,對於描述負責的行為是非常有用的。
  • Understanding state transitions is part of system analysis and design
  • 理解狀態轉移是分析和設計系統的一部分
  • A Transition is the movement from one state to another state
  • 轉移是從一個狀態的另一狀態的行為
  • Transitions between states occur as follows:
  • 狀態之間的轉移的發生如下所示:
    1. An element is in a source state(一個元素處於源狀態)
    2. An event occurs(一個事件發生)
    3. An action is performed(一個動作被被執行)
    4. The element enters a target state(該元素進入目標狀態)
  • Multiple transitions occur either when different events result in a state terminating or when there are guard conditions on the transitions
  • 多重轉移要麼發生在不同的時間導致狀態的終止的時候,要麼發生在此次轉移存在監護條件的時候
  • A transition without an event and action is known as automatic transitions
  • 不存在事件和動作的轉移被視為自動轉移

Actions

Action is an executable atomic computation, which includes operation calls, the creation or destruction of another object, or the sending of a signal to an object. An action is associated with transitions and during which an action is not interruptible - e.g., entry, exit

Action(行為) 是一個可執行的原子的運算,該運算包括 操作的呼叫、另一個物件的建立或銷燬、或傳送訊號給另一個物件。 一個Action(行為)與一個Transition(轉移)相關聯,並且在此過程中該Action不能被阻斷。- 例如, entry、exit.

Activity

Activity is associated with states, which is a non-atomic or ongoing computation. Activity may run to completion or continue indefinitely. An Activity will be terminated by an event that causes a transition from the state in which the activity is defined

Activity(活動)與狀態相關聯,是一個非原子性的或不間斷的運算。Activity可以執行到完成或者無限期地繼續下去。一個活動將被一個引發定義該活動(activity)的狀態的轉移的事件終止。

Characteristics of Action and Activities(行為和活動的特性)

  • States can trigger actions
  • 狀態能夠引發行為(actions)
  • States can have a second compartment that contains actions or activities performed while an entity is in a given state
  • 狀態可以有一個第二分割槽,其中包含了當一個實體處於給定的狀態的時候的行為(actions)或者活動(activities)
  • An action is an atomic execution and therefore completes without interruption
  • 一個行為(action)是一個原子執行,因此完成此次行為的過程不能被打斷
  • Five triggers for actions: On Entry, Do, On Event, On Exit, and Include
  • 行為(actions)的五個觸發點: On Entry, Do, On Event, On Exit 還有 Include
  • An activity captures complex behavior that may run for a long duration - An activity may be interrupted by events, in which case it does not complete occur when an object arrives in a state.
  • 活動(Activity)捕獲複雜的可能長時間執行的動作——一個活動可能被打斷,在這種情況下, 當一個物件到達一個狀態的時候,活動不能完全的執行。

Simple State Machine Diagram Notation(簡單的狀態機圖)

05-simple-state-machine-diagram

Entry and Exit Actions(進入和退出行為)

Entry and Exit actions specified in the state. It must be true for every entry / exit occurrence. If not, then you must use actions on the individual transition arcs

Entry(進入)和Exit(退出)動作規定在狀態(state)中。對於每一個 entry / exit 事件來說,它必須是真實的。否則,你必須對這個獨立的轉移弧(transition arcs)施加 actions.

  • Entry Action executed on entry into state with the notation: Entry / action
  • Entry Action 在每次進入到狀態的時候執行,以此符號表示: Entry / action
  • Exit Action executed on exit from state with the notation: Exit / action
  • Exit Action 在每次從狀態中退出的時候執行,以此符號表示: Exit / action

Example - Entry / Exit Action (Check Book Status)

This example illustrates a state machine diagram derived from a Class - "BookCopy": 這個例子闡明瞭 “BookCopy” 類的狀態機圖。

06-entry-and-exit-actions

Note(注意): Note:

  • This state machine diagram shows the state of an object myBkCopy from a BookCopy class
  • 該狀態機圖顯示了一個BookCopy類的物件 mBkCopy 的狀態
  • Entry action : any action that is marked as linked to the entry action is executed whenever the given state is entered via a transition
  • Entry action: 任何被標記連結到Entry Action(進入動作)的action,每當該狀態被通過轉移進入的時候都被執行
  • Exit action : any action that is marked as linked to the exit action is executed whenever the state is left via a transition
  • Exit action: 任何被標記連結到Exit Action(進入動作)的action,每當該狀態被通過轉移離開的時候都被執行

Substates(子狀態)

A simple state is one which has no substructure. A state which has substates (nested states) is called a composite state. Substates may be nested to any level. A nested state machine may have at most one initial state and one final state. Substates are used to simplify complex flat state machines by showing that some states are only possible within a particular context (the enclosing state).

一個簡單的狀態是沒有子結構的。一個擁有子狀態(巢狀狀態)的狀態被稱為複合狀態。字狀態可以被巢狀值任意層級。 一個巢狀狀態機最多可以有一個初始狀態和一個最終狀態。子狀態被用通過顯示一些只可能在特定的上下文環境中才顯示的狀態(封閉的狀態)來簡化複雜的平面狀態機。

Substate Example - Heater(子狀態的例子 - 加熱器(Heater))

07-submachine-example

State Machine Diagrams are often used for deriving testing cases, here is a list of possible test ideas: 狀態圖經常被用來驅動測試用例(testing case)(譯者注: 軟體測試中的一個概念),這是一個可能的測試思想的列表。

  • Idle state receives Too Hot event
  • 閒置狀態接收 Too Hot 事件
  • Idle state receives Too Cool event
  • 閒置狀態接收 Too Cool 事件
  • Cooling/Startup state receives Compressor Running event
  • Cooling/Startup(冷卻/啟動)狀態 接收 Compressor Running(壓縮機執行) 事件
  • Cooling/Ready state receives Fan Running event
  • Cooling/Ready(冷卻/已經預備)狀態 接收 Fan Running 事件
  • Cooling/Running state receives OK event
  • Cooling/Running(冷卻/執行)狀態 接收 OK 事件
  • Cooling/Running state receives Failure event
  • Cooling/Running(冷卻/執行)狀態 接收 Failure(故障) 事件
  • Failure state receives Failure Cleared event
  • 故障狀態接收 Failure Cleared 事件
  • Heating state receives OK event
  • 加熱狀態接收 OK 事件
  • Heating state receives Failure event
  • 加熱狀態接收 Failure 事件

History States(歷史狀態)

Unless otherwise specified, when a transition enters a composite state, the action of the nested state machine starts over again at the initial state (unless the transition targets a substate directly). History states allow the state machine to re-enter the last substate that was active prior to leaving the composite state. An example of history state usage is presented in the figure below.

除非另有規定,當一個轉移進入到一個複合狀態,巢狀狀態機的行為將在初始狀態再次啟動(除非該轉移直接地執行子狀態)。歷史狀態允許狀態機重新進入最後一個離開前啟用的最後一個子狀態子狀態。

08-history-state-example

Concurrent State(併發狀態)

As mentioned above, states in state machine diagrams can be nested. Related states can be grouped together into a single composite state. Nesting states inside others is necessary when an activity involves concurrent sub-activities. The following state machine diagram models an auction with two concurrent substates: processing the bid and authorizing the payment limit.

正如上面提到的那樣,狀態能夠巢狀於一個狀態機圖中。相關的狀態能夠被一起組成一個單一的複合狀態。當一個活動涉及到並行子活動的時候,在狀態中巢狀狀態是必須的。下方的狀態機圖中構建了一個競拍的模型,其中包含了兩個並行的子狀態: 進行投標和授權支付限制。

Concurrent State Machine Diagram Example - Auction Process 並行狀態機圖的例子 —— 拍賣流程

In this example, the state machine first entering the Auction requires a fork at the start into two separate start threads. Each substate has an exit state to mark the end of the thread. Unless there is an abnormal exit (Canceled or Rejected), the exit from the composite state occurs when both substates have exited.

在此例子中,首先進入的拍賣狀態機需要在開始的時候將流程分叉到兩個獨立的開始執行緒中。每一個子狀態有一個退出狀態來標記該執行緒的結束。 除非存在一個非正常退出(取消或者拒絕),當連個子狀態都退出的時候,符合狀態的退出才執行。

09-concurrent-state-machine-example

相關文章