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

时间:2023-03-09 16:43:26
【翻译】What is State Machine Diagram(什么是状态机图)?

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

写在前面

在上一篇学习类图的时候将这个网站上的类图的一篇文章翻译了出来,感觉受益良多,今天来学习UML状态机图,在网站找了很多的博客,但是都有些雷同的现象,所以又计划从该网站上学习UML状态机图,翻译出来以供大家参考。下面是原文链接:
https://www.visual-paradigm.com/guide/uml-unified-modeling-language/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.

一个实体的行为不仅是其输入的直接的结果,而且还取决于其之间的状态。一个实体的过去的历史能够通过有限状态机图或者传统上被称为自动机(automata(不知道是个什么东东,还望大佬解惑。))的图来建模。UML状态机图(UML State Machine Diagram)(有时被称为 状态图(state diagram), 状态机(stae machine) 或 状态图(state chart))展示了一个实体的不同的状态。状态机图同样也可以展示一个实体如何通过从一个状态到另一个状态的改变来相应各种各样的事件。

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

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(状态的图例)

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

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(例如):

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

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(例如):

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

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(简单的状态机图)

【翻译】What is 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” 类的状态机图。

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

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

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

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.

除非另有规定,当一个转移进入到一个复合状态,嵌套状态机的行为将在初始状态再次启动(除非该转移直接地执行子状态)。历史状态允许状态机重新进入最后一个离开前激活的最后一个子状态子状态。

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

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(什么是状态机图)?