【翻译】What is State Machine Diagram(什么是状态机图)?
【翻譯】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))展示了一個實體的不同的狀態。狀態機圖同樣也可以展示一個實體如何通過從一個狀態到另一個狀態的改變來相應各種各樣的事件。
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(狀態的圖例)
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(例如):
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(例如):
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:
- 狀態之間的轉移的發生如下所示:
- An element is in a source state(一個元素處于源狀態)
- An event occurs(一個事件發生)
- An action is performed(一個動作被被執行)
- 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(簡單的狀態機圖)
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” 類的狀態機圖。
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))
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.
除非另有規定,當一個轉移進入到一個復合狀態,嵌套狀態機的行為將在初始狀態再次啟動(除非該轉移直接地執行子狀態)。歷史狀態允許狀態機重新進入最后一個離開前激活的最后一個子狀態子狀態。
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.
在此例子中,首先進入的拍賣狀態機需要在開始的時候將流程分叉到兩個獨立的開始線程中。每一個子狀態有一個退出狀態來標記該線程的結束。 除非存在一個非正常退出(取消或者拒絕),當連個子狀態都退出的時候,符合狀態的退出才執行。
總結
以上是生活随笔為你收集整理的【翻译】What is State Machine Diagram(什么是状态机图)?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国人工智能产业发展联盟成立 360任副
- 下一篇: 服务器防渗透(1)--信息收集