有限状态机(fsm)_有限状态机

有限状态机(fsm)

There’s a lot of talk about Finite State Machines these days in JavaScript land.

如今,在JavaScript领域有很多关于有限状态机的讨论。

There’s this popular library called XState, with more than 11000 stars on GitHub, which I run into lately, and I keep reading about it on Twitter and other places. It’s a very cool project.

有一个流行的库叫做XState ,在GitHub上有超过11000个星,最近我遇到了这个库,我一直在Twitter和其他地方继续阅读它。 这是一个非常酷的项目。

I have first met Finite State Machines and Automata back in High School, 20 years ago, then I met them again in my Digital Design course at the University.

20年前,我在高中时第一次遇到了有限状态机和自动机,然后在大学的数字设计课程中又遇到了它们。

This Digital Design course was about encoding information, boolean algebra, combinatorial networks, sequential circuits, sequential state machines, arithmetical circuits, VHDL, and more.

该数字设计课程涉及编码信息,布尔代数,组合网络,顺序电路,顺序状态机,算术电路,VHDL等内容。

I found the topic of Finite State Machines applied to Frontend Engineering pretty amazing, and I went back to my old books to see if I could find a good explanation of them.

我发现应用于前端工程的有限状态机这一主题非常令人惊讶,然后我回到旧书中,看看是否能找到对它们的很好的解释。

I did find a lot of information, and of course being textbooks things are made more complex than needed (IMHO). I like to simplify things, and I decided to write a little explanation for normal human beings, not theoretical or anything made to pass an exam. Things you can apply to the real world.

我确实发现了很多信息,当然,作为教科书,事情变得比需要的更为复杂(恕我直言)。 我喜欢简化事情,因此决定为普通人写一些解释,而不是理论上的解释或通过考试的任何解释。 您可以应用于现实世界的事物。

状态机 (State Machines)

Before looking into what a Finite State Machine is, I want to first introduce what a State Machine is.

在研究什么是有限状态机之前,我想先介绍一下状态机。

The world is filled with state machines. You can see them everywhere, but perhaps you didn’t think of them as such. After reading this tutorial, I am sure you’ll point out objects in the real world saying to your friends “that’s a state machine” (depending on the nerdy level of your friends).

世界充满了状态机。 您到处都可以看到它们,但也许您没有想到它们。 阅读完本教程后,我确定您会指出现实世界中的对象对您的朋友说“这是一个状态机 ”(取决于您的朋友的书呆子程度)。

The most popular and commonly found example is traffic lights.

最受欢迎和最常见的例子是交通信号灯

At any point in time, a traffic semaphore has a defined state. Typically, it either:

在任何时间点,交通信号灯都具有定义的状态。 通常,它要么:

  • has the green light on, and the other 2 lights off

    绿灯亮,其他2灯灭
  • has the red light on, and the other 2 lights off

    呈红色亮起,另两个熄灭
  • as the yellow light on, and the other 2 lights off

    当黄灯亮起时,其他2灯熄灭

Traffic lights

(Some semaphores are slightly different, but we don’t care for the sake of this example)

(某些信号量略有不同,但是我们不在乎此示例)

In State Machines terminology, lights being on or off is called output.

在State Machines术语中,点亮或熄灭的灯称为输出

Each of those 3 scenarios is called state. The traffic semaphore will change state when it receives an input, typically just a fixed timer that decides how much time the traffic lights should be green, yellow and red.

这三种情况中的每一种都称为状态 。 交通信号灯在接收到输入时将改变状态,通常只是一个固定的计时器,该计时器决定交通信号灯应保持绿色,黄色和红色的时间。

The timer in this case is the input of the system. Some semaphores have a button that pedestrian can press to cause the red to display to cars, and that would be another input.

在这种情况下,计时器是系统的输入 。 一些信号灯具有一个按钮,行人可以按下该按钮以使红色显示在汽车上,这将是另一个输入。

In a state machine, the state can only change (and we have a transition) in response to an input.

在状态机中,状态只能响应输入而改变(并且我们有一个transition )。

有限状态机 (Finite State Machines)

Our traffic lights state machine is said to be finite because we have a finite number of states.

据说我们的交通灯状态机是有限的,因为我们有有限数量的状态。

Some systems might have an infinite number of states.

某些系统可能具有无限数量的状态。

Like the world ecosystem model, or the life of an insect. We can’t define it in a finite number of states.

例如世界生态系统模型或昆虫的生命。 我们不能在有限数量的状态下定义它。

But traffic lights? That’s a simple thing, and it has 3 states, like we said above.

但是交通灯? 就像我们上面所说的,这很简单,它具有3个状态。

There are many more examples of finite state machines we could use:

我们可以使用更多的有限状态机示例:

  • a vending machine

    自动贩卖机
  • a subway entrance turnstile

    地铁入口旋转门
  • a heating system

    加热系统
  • an automated subway system

    自动化地铁系统
  • a self-driving car system

    自动驾驶汽车系统
  • an elevator

    一部电梯

But let’s stick to our traffic lights example, which is very simple and we can reason about it easily.

但是,让我们继续使用我们的交通信号灯示例,它非常简单,我们可以轻松地对此进行推理。

对状态机建模 (Modeling a State Machine)

Of course traffic lights do not exist in isolation. That’s another finite state machine, which contains multiple different finite state machines for each traffic lights device installed for each side of each road of an intersection, which work in sync.

当然,交通信号灯并不是孤立存在的。 那是另一种有限状态机,对于交叉路口每条道路的每一侧所安装的每个交通信号灯设备,它都包含多个不同的有限状态机,这些设备可以同步工作。

Crossroads

Don’t think about it now. Let’s focus on 1 single traffic semaphore.

现在不要考虑。 让我们专注于1个单一的交通信号灯。

As we said above, we have 3 states, which can call green, red, yellow.

如上所述,我们有3个状态,分别可以称为绿色,红色,黄色。

The 3 states

We have an initial state. Let’s say our traffic lights start, when we reset them, at the green state.

我们有一个初始状态。 假设我们的交通灯在重置时以green状态启动。

Initial state

We have a timer that after 50 seconds of green state, moves the semaphore into the yellow state. We have 8 seconds of yellow state, then we move to the red state, and we sit there for 32 seconds, because that road is secondary and it deserves less time of green. After this, the semaphore gets back to the green state and the cycle continues indefinitely, until the electricity halts and the semaphore resets when it gets the power again.

我们有一个计时器,在绿色状态50秒钟后,将信号灯移到yellow状态。 我们有8秒的yellow状态,然后我们移到red状态,然后在那儿坐了32秒,因为那条路是次要的,它应该花更少的绿色时间。 此后,信号量将恢复为绿色状态,并且循环将无限期继续,直到断电并且信号量在再次获得电源时重置为止。

In total, we have a 90 seconds cycle.

总共有90秒的周期。

This is how we can model it:

这是我们可以建模的方式:

The model

We can also model it with a state transition table, which shows the state the machine is currently in, what is the input it receives, what is the next state, and the output:

我们还可以使用状态转换表对其进行建模,该显示了机器当前所处的状态 ,所接收的输入是什么, 下一个状态是什么以及输出

State Input Next state Output
Green Timer 50s Yellow Green light on, others off
Yellow Timer 8s Red Yellow light on, others off
Red Timer 32s Green Red light on, others off
输入项 下一个状态 输出量
绿色 计时器50秒 黄色 绿灯亮,其他灯灭
黄色 定时器8秒 黄灯亮,其他灯灭
计时器32秒 绿色 红灯亮,其他灯灭

In our simple case, given any state of the machine we just have one logical state to go to, so the table and the chart I made are very, very simple.

在我们的简单情况下,给定机器的任何状态,我们只有一个逻辑状态要去,所以我制作的表和图表非常非常简单。

But when your system starts to get more complex, it’s very helpful to have such diagrams and analysis in place because you can reason about your application much easier than by simply keeping everything in your head.

但是,当您的系统开始变得更加复杂时,适当地放置此类图表和分析将非常有帮助,因为与简单地掌握所有内容相比,您可以更轻松地推理应用程序。

具有更复杂状态转换的有限状态机 (A Finite State Machine with more complex State Transitions)

The above example is very simple. Let’s model another Finite State Machine now.

上面的例子很简单。 现在让我们建模另一个有限状态机。

Our real world scenario is this: we have a house, with one door, 2 buttons and 3 lights.

我们的现实世界场景是:我们有一所房子,有一个门,2个按钮和3个灯。

At the default state the lights are all turned off.

在默认状态下,所有指示灯均熄灭。

lights

When you enter the house, you can press one of the 2 push buttons you have, p1 or p2. When you press any of those buttons, the l1 light turns on.

进入房屋时,您可以按两个按钮之一p1p2 。 当您按这些按钮中的任何一个时, l1指示灯将点亮。

Imagine this is the entrance light, and you can take your jacket off. Once you are done, you decide which room you want to go into (kitchen or bedroom, for example).

想象这是入口灯,您可以脱下外套。 完成后,您可以决定要进入哪个房间(例如厨房或卧室)。

If you press the button p1, l1 turns off and l2 turns on. Instead if you press the button p2, l1 turns off and l3 turns on.

如果按按钮p1 ,则l1关闭, l2打开。 相反,如果您按下按钮p2 ,则l1关闭, l3打开。

Pressing another time any of the 2 buttons, p1 or p2, the light that is currently on will turn off, and we’ll get back at the initial state of the system.

再次按下两个按钮p1p2任何一个,当前打开的指示灯将熄灭,我们将返回到系统的初始状态。

This Finite State Machine is slightly more complex than the first, because this time we can have multiple routes depending on the input.

这个有限状态机比第一个有限状态机要复杂一些,因为这次我们可以根据输入有多个路由。

Let’s model this.

让我们对此建模。

We basically have 4 states:

我们基本上有4种状态:

  • no lights turned on

    没有灯亮
  • l1 turned on

    l1开启

  • l2 turned on

    l2开启

  • l3 turned on

    l3开启

model lights

We have 2 inputs:

我们有2个输入:

  • p1

    p1

  • p2

    p2

If we start from the initial state, and we try to model everything that can happen depending on how the inputs are triggered over time, we end up with:

如果我们从初始状态开始,并且尝试根据随时间推移触发输入的方式来建模可能发生的所有事情,那么最终结果是:

State transitions

Which can be summarized using this table:

可以使用此表对其进行汇总:

State Input Next state
no lights turned on p1 pressed l1 turned on
no lights turned on p2 pressed l1 turned on
l1 turned on p1 pressed l2 turned on
l1 turned on p2 pressed l3 turned on
l2 turned on p1 pressed no lights turned on
l2 turned on p2 pressed no lights turned on
l3 turned on p1 pressed no lights turned on
l3 turned on p2 pressed no lights turned on
输入项 下一个状态
没有灯亮 p1 l1开启
没有灯亮 p2 l1开启
l1开启 p1 l2开启
l1开启 p2 l3开启
l2开启 p1 没有灯亮
l2开启 p2 没有灯亮
l3开启 p1 没有灯亮
l3开启 p2 没有灯亮

That’s a short and simple explanation, but maybe it makes things “click”.

这是一个简短的解释,但也许会使事情“点击”。

翻译自: https://flaviocopes.com/finite-state-machines/

有限状态机(fsm)