In general, a state machine is any device that stores the status of something at a given time and can operate on input to change the status and/or cause an action or output to take place for any given change.

A computer is basically a state machine and each machine instruction is input that changes one or more states and may cause other actions to take place.

Each computer’s data register stores a state. The read-only memory from which a boot program is loaded stores a state (the boot program itself is an initial state).

The operating system is itself a state and each application that runs begins with some initial state that may change as it begins to handle input.

Thus, at any moment in time, a computer system can be seen as a very complex set of states and each program in it as a state machine. In practice, however, state machines are used to develop and describe specific device or program interactions.

To summarize it, a state machine can be described as:

  • An initial state or record of something stored someplace
  • A set of possible input events
  • A set of new states that may result from the input
  • A set of possible actions or output events that result from a new state

In their book Real-time Object-oriented Modeling, Bran Selic & Garth Gullekson view a state machine as:

  • A set of input events
  • A set of output events
  • A set of states
  • A function that maps states and input to output
  • A function that maps states and inputs to states (which is called a state transition function)
  • A description of the initial state

A finite state machine(有限状态机) is one that has a limited or finite number of possible states. (An infinite state machine can be conceived but is not practical.)

A finite state machine can be used both as a development tool for approaching and solving problems and as a formal way of describing the solution for later developers and system maintainers.

There are a number of ways to show state machines, from simple tables through graphically animated illustrations.


定义

我们先来给出状态机的基本定义。一句话:

状态机是有限状态自动机的简称,是现实事物运行规则抽象而成的一个数学模型

先来解释什么是“状态”( State )。现实事物是有不同状态的,例如一个自动门,就有 open 和 closed 两种状态。我们通常所说的状态机是有限状态机,也就是被描述的事物的状态的数量是有限个,例如自动门的状态就是两个 open 和 closed 。

状态机,也就是 State Machine ,不是指一台实际机器,而是指一个数学模型。说白了,一般就是指一张状态转换图。

例如,根据自动门的运行规则,我们可以抽象出下面这么一个图。
image.png
自动门有两个状态,open 和 closed ,closed 状态下,如果读取开门信号,那么状态就会切换为 open 。open 状态下如果读取关门信号,状态就会切换为 closed 。

状态机的全称是有限状态自动机,自动两个字也是包含重要含义的。给定一个状态机,同时给定它的当前状态以及输入,那么输出状态是可以明确的运算出来的。

例如对于自动门,给定初始状态 closed ,给定输入“开门”,那么下一个状态时可以运算出来的。

这样状态机的基本定义我们就介绍完毕了。重复一下:状态机是有限状态自动机的简称,是现实事物运行规则抽象而成的一个数学模型。

四大概念

下面来给出状态机的四大概念。

第一个是 State ,状态。一个状态机至少要包含两个状态。例如上面自动门的例子,有 open 和 closed 两个状态。
第二个是 Event ,事件。事件就是执行某个操作的触发条件或者口令。对于自动门,“按下开门按钮”就是一个事件。
第三个是 Action ,动作。事件发生以后要执行动作。例如事件是“按开门按钮”,动作是“开门”。编程的时候,一个 Action 一般就对应一个函数。
第四个是 Transition ,变换。也就是从一个状态变化为另一个状态。例如“开门过程”就是一个变换。

上面这四大概念,在使用状态机思想来写程序时候经常用到,参考:https://github.com/jakesgordon/javascript-state-machine
https://www.npmjs.com/package/javascript-state-machine

应用

状态机是一个对真实世界的抽象,而且是逻辑严谨的数学抽象,所以明显非常适合用在数字领域。可以应用到各个层面上,例如硬件设计,编译器设计,以及编程实现各种具体业务逻辑的时候。

来举个例子。街上的自动售货机中明显能看到状态机逻辑。我们做一下简化,假设这是一台只卖2元一瓶的汽水的售货机,只接受五毛和一块的硬币。初始状态是”未付款“,中间状态有”已付款5毛“,”已付款1块“,”已付款1.5块“,”已足额付款“,四个状态。状态切换的触发条件是”投一块硬币“和”投5毛硬币“两种,”到达足额付款“状态,还要进行余额清零和弹出汽水操作。所以如果画出一张完整的状态转换图,也会是比较复杂的一张图了。而实际中的售货机对应的状态机就会更加复杂了。

总之,状态机应用范围很广,这里就不展开了。插一句,跟状态机类似的概念还有图灵机,图灵机就是计算机底层采用的计算模型。

总结

这就是对状态机概念的一个通俗的简述了。

总结一下,状态机不是实际机器设备,而是一个数学模型,通常体现为一个状态转换图。涉及到的相关概念是 State 状态,Event 事件,Action 动作,Transition 转换。

状态机是计算机科学的重要基础概念之一,也可以说是一种总结归纳问题的思想,应用范围非常广泛。

参考:

https://blog.markshead.com/869/state-machines-computer-science/
https://en.wikipedia.org/wiki/Finite-state_machine
https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA