# 词汇表

¥Glossary

这些 XState v4 文档不再维护

XState v5 现已推出!阅读有关 XState v5 的更多信息 (opens new window)

¥XState v5 is out now! Read more about XState v5 (opens new window)

🆕 在我们的 全新 Stately 和 XState 术语表 (opens new window) 中查找更多术语。

¥🆕 Find more terms in our all-new Stately and XState glossary (opens new window).

本术语表是状态图和状态机中最常见术语的指南。

¥This glossary is a guide to the most common terms in statecharts and state machines.

# 状态机

¥State machines

状态机是描述当事件发生时进程的状态如何转换到另一种状态的模型。

¥A state machine is a model that describes how the state of a process transitions to another state when an event occurs.

状态机使构建可靠的软件变得更加容易,因为它们可以防止不可能的状态和不需要的转换。

¥State machines make building reliable software easier because they prevent impossible states and undesired transitions.

# 状态图

¥Statecharts

状态图是状态机的可视化扩展,使你能够对更复杂的逻辑进行建模,包括层次结构、并发性和通信。

¥Statecharts are a visual extension to state machines enabling you to model more complex logic, including hierarchy, concurrency, and communication.

你过去可能使用过类似的图表来设计用户流程、规划数据转换或映射应用逻辑。状态图是使用方框和箭头表示此类流的另一种方式。这些流也是可执行代码,你可以使用它来直接控制应用代码中的逻辑。

¥You may have used similar diagrams in the past to design user flows, plan data transformations or map app logic. Statecharts are another way of using boxes and arrows to represent these kinds of flows. These flows are also executable code you can use to control the logic directly in your application code.

basic statechart with an initial state transitioning through an event to another state, then transitioning through another event to a final state.

# 状态

¥States

状态描述了机器的状态。此状态定义机器在收到事件时的行为方式。状态可以很简单,例如活动或非活动。这些状态是有限的;机器只能在预定义的状态之间移动。机器一次只能处于一种状态。父状态并行状态 可用于表达作为子状态组合的整体状态。

¥A state describes the status of the machine. This status defines how the machine behaves when it receives an event. A state can be as simple as active or inactive. These states are finite; the machine can only move through the predefined states. Machines can only be in one state at a time. Parent states and parallel state can be used to express an overall state that is the combination of child states.

# 初始状态

¥Initial state

当状态机启动时,它首先进入初始状态。实心圆,箭头从圆指向初始状态 图标代表初始状态。一台机器只能有一个顶层初始状态。

¥When a state machine starts, it enters the initial state first. The filled circle with an arrow pointing from the circle to the initial state icon represents the initial state. A machine can only have one top-level initial state.

# 子状态中的初始状态

¥Initial states in child states

在父状态中,必须指定哪个子状态是初始状态,机器进入父状态时会自动进入该状态。实心圆,箭头从圆指向初始状态 图标代表初始状态。

¥Inside a parent state, you must specify which child state is the initial state, which the machine enters automatically when it enters the parent state. The filled circle with an arrow pointing from the circle to the initial state icon represents the initial state.

# 父子状态

¥Parent and child states

状态可以包含更多状态,也称为子状态。这些子状态只能在父状态发生时发生。

¥States can contain more states, also known as child states. These child states can only happen when the parent state is happening.

statechart with an initial state transitioning through an event to a parent state which contains two states. The second state transitions through an event to the final state.

# 最终状态

¥Final state

当机器达到最终状态时,它无法再接收任何事件,并且其内部运行的任何内容都将被取消和清理。有边框的盒子 图标代表最终状态。

¥When a machine reaches the final state, it can no longer receive any events, and anything running inside it is canceled and cleaned up. The box with a surrounding border icon represents the final state.

一台机器可以有多个最终状态或没有最终状态。

¥A machine can have multiple final states or no final states.

# 并行状态

¥Parallel states

并行状态是父状态分为多个子状态区域,其中每个区域同时处于活动状态。区域由每个区域周围的虚线表示。

¥A parallel state is a parent state separated into multiple regions of child states, where each region is active simultaneously. Regions are represented by a dashed line around each region.

basic statechart with an initial state transitioning through an event to a parallel state which contains two regions. Each region has its own states. There’s an event from the parallel state which ends in the final state.

# 历史状态

¥History state

历史状态使机器返回到最近的活动状态。里面有一个 H 的盒子 图标代表历史状态。

¥A history state returns the machine to the most recently active state. The box with an H inside icon represents the history state.

历史状态可以是深的,也可以是浅的:

¥The history state can be deep or shallow:

  • 浅层历史状态会记住直接子级的状态。

    ¥A shallow history state remembers the immediate child’s state.

  • 深层历史状态会记住其子状态中最深层的活动状态或状态。

    ¥A deep history state remembers the deepest active state or states inside its child states.

里面有 H 和星号的盒子 图标代表深层历史状态。

¥The box with an H and asterisk inside icon represents the deep history state.

# 转场和事件

¥Transitions and events

机器通过转换从一个状态移动到另一个状态。这些转变是由事件引起的。事件是确定性的;状态和事件的每个组合总是指向相同的下一个状态。

¥A machine moves from state to state through transitions. These transitions are caused by events. Events are deterministic; each combination of state and event always points to the same next state.

# 受保护的转换

¥Guarded transitions

防护是机器在经历事件时检查的条件。如果条件为真,机器就会转移到下一个状态。如果条件为假,机器将遵循其余条件进入下一个状态。任何转换都可以是受保护的转换。

¥A guard is a condition that the machine checks when it goes through an event. If the condition is true, the machine follows the transition to the next state. If the condition is false, the machine follows the rest of the conditions to the next state. Any transition can be a guarded transition.

# 无事件转场

¥Eventless transitions

无事件转换是没有事件的转换。如果启用的话,这些转换总是在其状态的任何转换之后进行;不需要任何事件来触发转换。无事件转换被标记为“始终”,并且通常被称为“始终”转换。

¥Eventless transitions are transitions without events. These transitions are always taken after any transition in their state if enabled; no event is necessary to trigger the transition. Eventless transitions are labeled “always” and often referred to as “always” transitions.

# 通配符转换

¥Wildcard transitions

通配符转换由当前状态尚未处理的任何事件触发。通配符转换由星号 * 表示。

¥Wildcard transitions are triggered by any event not already handled by the current state. Wildcard transitions are represented by an asterisk *.

通配符转换对于记录未跟踪的事件和减少代码重复很有用。

¥Wildcard transitions are useful for logging untracked events and reducing code duplication.

# 角色、动作、以及调用的角色

¥Actors, actions and invoked actors

状态图是参与者的可执行模型。当你运行一个状态图时,它就变成了一个参与者;一个正在运行的进程,可以接收消息、发送消息并根据接收到的消息更改其行为,这可能会在参与者之外造成影响。

¥A statechart is an executable model of an actor. When you run a statechart, it becomes an actor; a running process that can receive messages, send messages and change its behavior based on the messages it receives, which can cause effects outside of the actor.

当状态图参与者运行时,它可以运行称为操作的其他进程。

¥While the statechart actor is running, it can run other processes called actions.

动作可以在进入或退出状态时触发,也可以在转换时触发。进入和退出操作是即发即忘的过程;一旦机器启动了该动作,它就会继续前进并忘记该动作。

¥An action can be fired upon entry or exit of a state and can also be fired on transitions. Entry and exit actions are fire-and-forget processes; once the machine has fired the action, it moves on and forgets the action.

被调用的参与者是一个可以执行自己的操作并与机器通信的参与者。这些被调用的参与者在某个状态下启动,并在退出该状态时停止。

¥An invoked actor is an actor that can execute its own actions and communicate with the machine. These invoked actors are started in a state and stopped when the state is exited.