900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 状态模式 有限状态机_有限状态机解释

状态模式 有限状态机_有限状态机解释

时间:2024-07-04 06:50:04

相关推荐

状态模式 有限状态机_有限状态机解释

状态模式 有限状态机

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 itsstates, itsinitial stateand thetransitions.

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

The power of FSM comes from the ability to clearly define differentbehaviorsin 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.Deterministicrefers to the uniqueness of the computation. A Finite State Machine is called a DFA if it obeys the following rules:

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

Each of its transitions isuniquelydetermined by its source state and input symbol

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

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中找不到?

An NFA can have anempty stringtransition (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的特定状态时,机器可以过渡到下一个状态而无需读取输入符号

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中的对的唯一目标相反 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.每对状态和转换符号对为其可能的目的地中的每一个产生一个“分支”计算,从而创建某种多线程系统。 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.

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

翻译自: /news/finite-state-machines/

状态模式 有限状态机

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。