# 概念
¥Concepts
这些 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) 中的概念。
¥🆕 Learn the concepts in all-new Stately and XState documentation (opens new window).
XState 是一个库,用于创建、解释和执行有限状态机和状态图,以及管理作为参与者的这些机器的调用。以下基本计算机科学概念对于了解如何充分利用 XState 以及一般情况下对于所有当前和未来的软件项目非常重要。
¥XState is a library for creating, interpreting, and executing finite state machines and statecharts, as well as managing invocations of those machines as actors. The following fundamental computer science concepts are important to know how to make the best use of XState, and in general for all your current and future software projects.
# 有限状态机
¥Finite State Machines
有限状态机是一种计算数学模型,它描述了在任何给定时间只能处于一种状态的系统的行为。例如,假设你可以用具有有限数量 (2) 个状态的状态机来表示:asleep
或 awake
。在任何给定时间,你要么是 asleep
,要么是 awake
。你不可能同时既是 asleep
又是 awake
,你也不可能既不是 asleep
又不是 awake
。
¥A finite state machine is a mathematical model of computation that describes the behavior of a system that can be in only one state at any given time. For example, let's say you can be represented by a state machine with a finite number (2) of states: asleep
or awake
. At any given time, you're either asleep
or awake
. It is impossible for you to be both asleep
and awake
at the same time, and it is impossible for you to be neither asleep
nor awake
.
从形式上来说,有限状态机有五个部分:
¥Formally, finite state machines have five parts:
有限数量的状态
¥A finite number of states
有限数量的事件
¥A finite number of events
初始状态
¥An initial state
给定当前状态和事件确定下一个状态的转换函数
¥A transition function that determines the next state given the current state and event
一组(可能为空)最终状态
¥A (possibly empty) set of final states
状态是指由状态机建模的系统的某些有限的、定性的 "mode" 或 "status",并且不描述与该系统相关的所有(可能是无限的)数据。例如,水可以处于以下 4 种状态之一:ice
、liquid
、gas
或 plasma
。然而,水的温度可能会发生变化,并且其测量是定量且无限的。
¥State refers to some finite, qualitative "mode" or "status" of a system being modeled by a state machine, and does not describe all the (possibly infinite) data related to that system. For example, water can be in 1 of 4 states: ice
, liquid
, gas
, or plasma
. However, the temperature of water can vary and its measurement is quantitative and infinite.
更多资源:
¥More resources:
维基百科上的 有限状态机 (opens new window) 条文章
¥Finite-state machine (opens new window) article on Wikipedia
了解状态机 (opens new window),马克·谢德 (Mark Shead)
¥Understanding State Machines (opens new window) by Mark Shead
▶️ A-Level 计算机科学:有限状态机 (opens new window)
¥▶️ A-Level Comp Sci: Finite State Machine (opens new window)
# 状态图
¥Statecharts
状态图是一种用于对有状态、反应式系统进行建模的形式。计算机科学家 David Harel 在他 1987 年的论文 状态图:复杂系统的视觉形式主义 (opens new window) 中提出了这种形式主义作为状态机的扩展。一些扩展包括:
¥Statecharts are a formalism for modeling stateful, reactive systems. Computer scientist David Harel presented this formalism as an extension to state machines in his 1987 paper Statecharts: A Visual Formalism for Complex Systems (opens new window). Some of the extensions include:
受保护的转换
¥Guarded transitions
动作(进入、退出、转换)
¥Actions (entry, exit, transition)
扩展状态(上下文)
¥Extended state (context)
正交(平行)状态
¥Orthogonal (parallel) states
分层(嵌套)状态
¥Hierarchical (nested) states
历史
¥History
更多资源:
¥More resources:
大卫·哈雷尔的《状态图:复杂系统的视觉形式主义 (opens new window)》
¥Statecharts: A Visual Formalism for Complex Systems (opens new window) by David Harel
埃里克·摩根森的《状态图的世界 (opens new window)》
¥The World of Statecharts (opens new window) by Erik Mogensen
# 角色模型
¥Actor Model
角色模型是另一个非常古老的计算数学模型,与状态机配合良好。它指出一切都是 "actor",可以做三件事:
¥The actor model is another very old mathematical model of computation that goes well with state machines. It states that everything is an "actor" that can do three things:
接收消息
¥Receive messages
向其他演员发送消息
¥Send messages to other actors
对收到的消息(其行为)执行某些操作,例如:
¥Do something with the messages it received (its behavior), such as:
改变其本地状态
¥change its local state
向其他参与者发送消息
¥send messages to other actors
催生新角色
¥spawn new actors
参与者的行为可以通过状态机(或状态图)来描述。
¥An actor's behavior can be described by a state machine (or a statechart).
更多资源:
¥More resources:
维基百科上的 角色模型 (opens new window) 条文章
¥Actor model (opens new window) article on Wikipedia
布莱恩·斯托蒂的《10 分钟看角色模型 (opens new window)》
¥The actor model in 10 minutes (opens new window) by Brian Storti