有限状态机是否应该具有“嵌套”状态机?有限状态机?
您可以阅读我提出的这个问题关于机器应用程序的最佳架构的一些背景故事,尽管这对于帮助我解决这个问题并不是完全必要的。
我对有限状态机的理解(特别是对于实现)有点年轻,可能还缺乏一点,但我正在将此应用程序作为一个实现,并且我有一个需要嵌套 FSM 的地方。基本上,机器有一些高级状态(冷[又名刚刚启动]、归位、设置、准备运行、运行、报告、重置),但是当机器运行时,它需要有自己的小 FSM 实现(装载镜头、定位边缘、测量楔块、测量圆度和完成[可能还有更多内容])。
我的问题是:我是否应该建立具有“嵌套状态”的功能,其中状态可以具有子状态列表,并且系统可以进入这些子状态并且这些子状态可以返回到父状态?或者我应该将 FSM 实现放在运行状态中,并将它们保留为两个不同的 FSM?或者你认为我正在做或想一些愚蠢的事情并且应该重新考虑?
欢迎提出想法、建议、批评和建议。
You can read this question where I ask about the best architecture for a machine application for a little back story, although it's not entirely necessary for helping me with this question.
My understanding (especially for implementation) of Finite State Machine's is a little young and may be lacking a bit, but I am implementing this application as one, and I've got a place where I kind of need to have a nested FSM. Basically the machine has a few high level states (Cold [a.k.a just started], Homing In, Setup, Ready to Run, Running, Reporting, Reseting) but when the machine is running it kind of needs to have it's own little FSM implementation for (Loading Lense, Locating Edge, Measuring Wedge, Measuring Roundness, and Complete [may be some more in there]).
My question is this: Should I build in the capability of having "nested states" where a state can have a list of sub states and the system can enter those sub states and those sub states can return out to parent states? Or should I just put a FSM implementation inside of the Running state, and keep them as two distinct FSMs? Or do you think I'm doing or thinking something dumb and should rethink it?
Thoughts, suggestions, criticisms, and advice are all welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嵌套状态机是 UML 中的标准概念,因此这并不一定是愚蠢的。 更多详细信息请参见此处。
Nested state machines are a standard notion in UML, so this is not necessarily dumb. More details here.
相反。能够嵌套 FSM 是一件好事。
人们不应该仅仅为了嵌套而嵌套 FSM,但有时 FSM 会变得相当大。如此大的绘图破坏了 FSM 模型的目的,因为它无法让您很好地了解内部工作原理。它变成了一个包含许多细节的巨大图表。
我认为你可以将它与课程进行比较。如果将所有内容都放入一个类中(更糟糕的是,将所有内容都静态化),那么拥有一个类的目的和优势就会消失。
FSM 也是如此。
举个例子,我的一位大学生使用 FSM 模拟了一只非常“现实”的狗的行为。他有一个巨大的模型,通过嵌套 FSM,我能够在短短几分钟内理解该模型。
所以,如果使用得当的话,这绝对是一件好事。
In contrary. Having the possibility to nest FSMs is a good thing.
One should not nest FSMs just to do nesting, but sometimes FSMs get quite large. Having such a large drawing undermines the purpose of an FSM-model since it does not give you a nice view of the inner working. It becomes just a huge diagram with way to many details.
I think you can compare it with classes. If you stick everything into one class (and even worse, make everything static) the purpose and advantages of having a class will disappear.
Same goes for FSMs.
To give you an example, a collegeau of mine has model quite "realistic" the behavior of a dog using FSMs. He has a huge model and by having nested FSMs i was able to understand the model in just a few mins.
So it is definitely a good thing, if it is used properly.
我只是想补充一点,嵌套状态(在 UML FSM 中)与将单独的 FSM“放入”运行状态中不同。
在真实的分层 FSM 中,事件首先被发布到当前的嵌套状态。如果该状态不处理它们,它们将被发布到父状态,依此类推。这允许人们“重构”从嵌套状态到父状态的常见状态转换。
I just want to add that nested states (in UML FSM) are not the same as "putting" a separate FSM inside a running state.
In real hierarchical FSMs, events are first posted to the current nested state. If that state doesn't handle them, they will be posted to the parent state, and so on. This allows one to "refactor" common state transitions from nested states into a parent state.
我通过使用代表状态的枚举来解决这个问题。例如
兔子有生育状态。
ProcreationState 有一个枚举,
在状态更新方法中,我只是检查它所处的状态并采取相应的操作。
我想这限制了该系统的整体能力。我经验不足,所以我尝试一下。对此方法的任何反馈都值得赞赏。
I solve this by having an enum representing states of the state. For example
Bunny has a Procreation state.
ProcreationState has an enum
In the states update method i just check what state its in and act acordingly.
I guess this limits the overall capability of this system. Im not so experienced so im trying this out. Any feedback on this approach is apreciated.
嵌套状态机是状态机理论的一个非常关键的组成部分,状态机不流行的原因,特别是在 Java 世界中,是状态爆炸问题,正如所讨论的 此处。
随着您的业务案例不断发展并变得复杂,状态数量增加,这将成为维护的噩梦。
嵌套状态机是具有分而治之模式的工具,可以防止这种困境,在我看来,它是成功状态机设计的必要组成部分。
如果您想查看实现示例,可以在我的博客中找到这些示例 blog1 博客2。
Nested State Machines are a very critical component of the State Machine theory, the reason State Machines are not popular, specially in Java world, is the State Explosion problem, as discussed here.
As your Business Case evolves and gets complicated, number of the states increases, it becomes a maintenance nightmare.
Nested State Machines are tools, with Divide and Conquer pattern, to prevent this dilemma and in my opinion a necessary part of a succesul State Machine design.
If you want to see implementations examples, you can find those in my blogs blog1 blog2.