【翻译】What is State Machine Diagram(什么是状态机图)?

正文

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.

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).

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.

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.

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这样定义):

"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."

“一个状态是一个对象的属性值和链接的抽象。 根据影响对象整体行为的属性，一组值将会被合成为一个状态。”

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:

• 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)捕获复杂的可能长时间运行的动作——一个活动可能被打断，在这种情况下, 当一个对象到达一个状态的时候，活动不能完全的执行。

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":

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:

• 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(冷却/已经预备)状态 接收 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.

Last modification：September 6th, 2018 at 03:45 pm
If you think my article is useful to you, please feel free to appreciate