状态模式 有限状态机

The finite state machine (FSM) is a software design pattern where a given model transitions to other behavioral states through external input.

有限状态机(FSM)是一种软件设计模式,其中给定的模型通过外部输入转换为其他行为状态。

了解有限状态机 (Understanding the Finite State Machine)

A FSM is defined by its states, its initial state and the transitions.

FSM由其状态 ,其初始状态过渡定义

The power of FSM comes from the ability to clearly define different behaviors in different conditions. Usually FSM is used with looping behavioral scripts which constantly evaluate the current situation in a loop or with events.

FSM的能力来自能够清楚定义不同条件下不同行为的能力。 通常,FSM与循环行为脚本一起使用,这些脚本不断评估循环中的当前情况或事件。

To help form an image of how this might be applied, a coffee machine will be used as an example of a finite state machine. We will also cover a state diagram to visualise the FSM and provide coding examples.

为了帮助形成如何应用的图像,将使用咖啡机作为有限状态机的示例。 我们还将覆盖状态图以可视化FSM并提供编码示例。

状态图 (State Diagram)

This diagram shows three possible states for the coffee machine:

此图显示了咖啡机的三种可能状态:

  • Open打开
  • ReadyToBuy准备购买
  • PoweredOff断电

The lines between these states show which transitions are possible between states and in which direction. These transitions have conditions for when the FSM needs to change between states.

这些状态之间的线表示状态之间和方向上可能发生哪些转换。 这些过渡为何时需要在状态之间更改FSM提供了条件。

  • StartUpMachine From the PoweredOff state to the Open state the machine has to start up. This is done manually in this case.StartUpMachine从PoweredOff状态到Open状态,机器必须启动。 在这种情况下,这是手动完成的。
  • deposit >= cost of coffee The FSM evaluates the amount of deposited cash either in a loop or when the amount changes (recommended in this case) If you deposit enough cash into the coffee machine, the FSM will go from ‘Open’ to ‘ReadyToBuy’.存款> =咖啡成本FSM会循环评估或当金额发生变化时评估存入的现金量(在这种情况下,建议使用)如果您将足够的现金存入咖啡机,FSM将从“打开”变为“ ReadyToBuy” '。
  • ShutdownMachine The machine will automatically go from Open to PoweredOff through the ShutDownMachine method if the condition ‘no more coffees left’ is met.ShutdownMachine如果满足“不再剩下咖啡”的条件,则机器将通过ShutDownMachine方法自动从“打开”变为“关闭”。
  • DispenseCoffee In the ReadyToBuy state the user can buy a coffee whereafter it will be brewed and dispensed. The condition is when the BuyCoffee event (!Link to observer pattern!) fires. (not shown in diagram)DispenseCoffee在ReadyToBuy状态下,用户可以购买咖啡,然后将其冲泡并分配。 条件是发生BuyCoffee事件(!链接到观察者模式!)时。 (图中未显示)
  • CancelCoffee If the user opts to cancel, the machine will go from ReadyToBuy to Open.CancelCoffee如果用户选择取消,则计算机将从ReadyToBuy变为Open。
  • ShutDownMachine The machine will go to PoweredOff stateShutDownMachine机器将进入PoweredOff状态

状态 (States)

In every state there is defined behavior which will only be executed when the object is in that state. For instance, during PoweredOff the coffee machine won’t brew coffee before it’s powered on, during the Open state it will wait either until there’s enough cash inserted, until the power down command is given, or until it runs out of coffee. During this Open state it can do routines such as cleaning which won’t happen in other states.

在每种状态下都有定义的行为,仅当对象处于该状态时才执行。 例如,在PoweredOff期间,咖啡机在开机之前不会煮咖啡;在Open(打开)状态下,咖啡机将等待直到插入足够的现金,发出断电命令或咖啡用完为止。 在此Open状态期间,它可以执行诸如清洗之类的例程,而在其他状态下则不会发生。

初始状态 (Initial State)

Every FSM has an initial state, this means which state it starts in when it is created and has to be defined when constructed or instantiated. Of course it’s possible to directly change state if conditions are met.

每个FSM都有一个初始状态,这意味着它创建时从哪个状态开始,并且在构造或实例化时必须定义。 当然,如果满足条件,则可以直接更改状态。

转场 (Transitions)

Every state either constantly evaluates if it should transition to another state or will transition to another state based on a triggered event.

每个状态要么持续评估它是否应该转换为另一个状态,要么根据触发的事件转换为另一个状态。

DFA和NFA (DFA and NFA)

There are two types of finite automaton, Deterministic (DFA) and Nondeterministic (NFA). Both of them accept regular languages and operate more or less in the same way described above however with some differences.

有限自动机有两种类型,确定性(DFA)和不确定性(NFA)。 它们都接受常规语言,并以与上述相同的方式或多或少地进行操作,但是有一些区别。

A DFA accepts or rejects a string of symbols and only produces one unique computation or automaton for each input string. Deterministic refers to the uniqueness of the computation. A Finite State Machine is called a DFA if it obeys the following rules:

DFA接受或拒绝一串符号,并且每个输入字符串仅产生一个唯一的计算或自动机。 确定性是指计算的唯一性。 如果有限状态机遵循以下规则,则称为DFA:

  1. Each of its transitions is uniquely determined by its source state and input symbol

    每个过渡都由其源状态和输入符号唯一地确定

  2. Reading an input symbol is required for each state transition.每个状态转换都需要读取输入符号。

An NFA does not need to obey these restrictions, meaning that each DFA is also an NFA. And since they both only recognize regular languages, each NFA can be converted into an equivalent DFA using the powerset construction algorithm.

NFA不需要遵守这些限制,这意味着每个DFA也是NFA。 并且由于它们都只能识别常规语言,因此可以使用powerset构造算法将每个NFA转换为等效的DFA。

So what sort of rules can we expect to find in NFAs but not DFAs ?

那么,我们期望在NFA中找到什么样的规则,而在DFA中找不到?

  1. An NFA can have an empty string transition (generally denoted by an epsilon). Meaning that when at a certain state with an epsilon for a transition rule, the machine can transition to the next state without reading an input symbol

    NFA可以具有空字符串过渡(通常由epsilon表示)。 这意味着当处于带有过渡规则的epsilon的特定状态时,机器可以过渡到下一个状态而无需读取输入符号

  2. In an NFA, each pair of state and transition symbol can have multiple destination states as opposed to the unique destinations of pairs in DFAs在NFA中,每对状态和转换符号对可以具有多个目标状态,这与DFA中的对的唯一目标相反
  3. Each pair of state and transition symbol produces a ‘branch’ of computation for each of its possible destination creating some sort of a multithreaded system.每对状态和转换符号对为其可能的目的地中的每一个产生一个“分支”计算,从而创建某种多线程系统。
  4. A DFA will reject the input string if it lands on any state other than the acceptance state. In an NFA, we only need one ‘branch’ to land on an acceptance state in order to accept the string.如果DFA落入接受状态以外的任何状态,则DFA将拒绝输入字符串。 在NFA中,我们只需要一个“分支”即可进入接受状态以接受字符串。

If you want to learn more, here's a great in-depth guide on state machines.

如果您想了解更多信息,请参阅以下有关状态机的详尽指南 。

翻译自: https://www.freecodecamp.org/news/finite-state-machines/

状态模式 有限状态机

状态模式 有限状态机_有限状态机解释相关推荐

  1. 状态模式 - Unity(有限状态机)

    文章目录 状态模式(有限状态机) 结构 实现(有限状态机) 应用场景 优缺点 与其他模式的关系 状态模式(有限状态机) 状态模式是一种对象型模式,他将复杂的逻辑判断提取到不同状态对象中,允许状态对象在 ...

  2. 状态模式 设计模式_设计模式:状态

    状态模式 设计模式 本文将介绍状态设计模式 . 它是行为设计模式之一 . 您无需了解许多理论即可了解模式的主要概念. 该文章将分为几个部分,在其中我将提供有关需要应用该模式的情况,它所具有的利弊以及用 ...

  3. 设计模式之美 - 64状态模式

    状态模式一般用来实现状态机,主要应用在游戏.工作流引擎中. 状态机实现方式:分支逻辑法.查表法.状态模式 什么是有限状态机? 状态机有 3 个组成部分:状态(State).事件(Event).动作(A ...

  4. 有限状态机(使用状态模式C++实现)

    最近在研究怪物的AI,由于怪物的AI是使用有限状态机来实现的.所以就查看关于有限状态机的东西.根据这几天的查看资料,知道了有限状态机是计算机科学中一个很重要的概念.而且有限状态机是一个抽象的概念,具体 ...

  5. 【游戏设计模式】之三 状态模式、有限状态机 Unity版本实现

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接: http://blog.csdn.net/poem_qianmo/article/details/52824776 作者:毛星云(浅 ...

  6. 【游戏设计模式】之三 状态模式 有限状态机 Unity版本实现

     本系列文章由@浅墨_毛星云 出品,转载请注明出处.    文章链接: http://blog.csdn.net/poem_qianmo/article/details/52824776  作者:毛星 ...

  7. 【游戏设计模式】之三 状态模式、有限状态机

    转载自:https://blog.csdn.net/poem_qianmo/article/details/52824776 游戏开发过程中,各种游戏状态的切换无处不在.但很多时候,简单粗暴的if e ...

  8. 状态模式、有限状态机 Unity版本实现

    游戏开发过程中,各种游戏状态的切换无处不在.但很多时候,简单粗暴的if else加标志位的方式并不能很地道地解决状态复杂变换的问题,这时,就可以运用到状态模式以及状态机来高效地完成任务.状态模式与状态 ...

  9. Java 有限状态机 (设计模式——状态模式)

    Java 有限状态机 (设计模式--状态模式) 编写代码的时候,有时会遇见较为复杂的swith...case...和if...else...语句.这一刻有时会想到状态机,用有限状态机替换swith.. ...

最新文章

  1. Gentoo 安装日记 15 (配置内核 :固件驱动..文件系统以及其他)
  2. RavenDb学习(十)附件,存储大对象
  3. 如何查找SAP Fiori UI上某个字段对应的底层数据库表
  4. 团队任务3:每日立会(2018-10-25)
  5. 第六节:反射(几种写法、好处和弊端、利用反射实现IOC)
  6. 兼容FF/IE的添加收藏夹的代码
  7. Prototype源码浅析——Function.prototype部分(一)
  8. 06旋转数组的最小数字
  9. linux 访问samba共享文件夹权限,请教:linux下 利用samba网络共享文件夹修改权限的问题...
  10. 一个数组中,除两个元素其余都出现了两次,找出这两个元素
  11. HTML5本地存储与会话存储
  12. Express框架的请求处理~非常详细
  13. android qq图片分享到朋友圈,微信分享 QQ分享直接展示视频或图片 分享图片 分享视频...
  14. RDP报表设置Tomcat服务自动启动详细教程
  15. Rational Rose安装使用教程
  16. python爬虫设计图片大全_PYTHON爬虫——必应图片关键词爬取
  17. wed简介及html简单标签(1)
  18. Linux 编jpeg函数,JPEG图形库:libjpeg,在LINUX下如何将jpg转换成bmp
  19. android ibeacon sdk,如何通过Android上的SDK更改iBeacon参数(UUID,Major,Minor,TxPower)的值...
  20. optaplanner学习笔记(一)案例Cloud balance

热门文章

  1. 【今日CV 计算机视觉论文速览】Mon, 4 Mar 2019
  2. Kubernetes-基本介绍/核心功能/相关术语(一)
  3. 0407 背景相关的作业
  4. StringBuilder对象的常用方法 c#1614651638
  5. 包的实际操作 java
  6. Laravel-admin 使用表单动态地保存一个关联模型(源码探究到功能实现)
  7. PHP设计模式之抽象工厂模式
  8. binlog2sql使用总结
  9. 分享平时工作中那些给力的shell命令
  10. oracle报表范例1 (转载)