FSM状态的实现技术
您如何实现 FSM(编辑:有限状态机)状态? 我通常将 FSM 视为一组函数, 调度员, 和一个线程来指示当前的运行状态。 意思是,我阻止对代表的函数/函子的调用 州。
刚才我用一种不同的风格实现了一个, 我仍然用函数(对象)表示状态,但是线程 只是调用一个 state->step()
方法,该方法尝试返回 尽快。如果状态已经完成并且 应该发生转变,它相应地表明了这一点。 我将其称为“轮询”风格,因为函数大多看起来 就像:
void step()
{
if(!HaveReachedGoal)
{
doWhateverNecessary();
return; // get out as fast as possible
}
// ... test perhaps some more subgoals
indicateTransition();
}
我知道它是 FSM 中的 FSM。
感觉比较简单,但是有一定的优点。 当线程被阻塞或处于某种状态时 while (!CanGoForward)checkGoForward();
循环可能会很麻烦且难以操作, 轮询感觉更容易调试。 这是因为 FSM
对象在之后重新获得控制权 每一步,输出一些调试信息都是轻而易举的事。
好吧,我偏离了我的问题: 你们如何实现 FSM 的状态?
How do you go about implementing FSM(EDIT:Finite State Machine) states?
I usually think about an FSM like a set of functions,
a dispatcher,
and a thread as to indicate the current running state.
Meaning, I do blocking calls to functions/functors representing
states.
Just now I have implemented one in a different style,
where I still represent states with function(object)s, but the thread
just calls a state->step()
method, which tries to return
as quickly as possible. In case the state has finished and a
transition should take place, it indicates that accordingly.
I would call this the 'polling' style since the functions mostly look
like:
void step()
{
if(!HaveReachedGoal)
{
doWhateverNecessary();
return; // get out as fast as possible
}
// ... test perhaps some more subgoals
indicateTransition();
}
I am aware that it is an FSM within an FSM.
It feels rather simplistic, but it has certain advantages.
While a thread being blocked, or held in some kind ofwhile (!CanGoForward)checkGoForward();
loop can be cumbersome and unwieldy,
the polling felt much easier to debug.
That's because the FSM
object regains control after
every step, and putting out some debug info is a breeze.
Well I am deviating from my question:
How do you implement states of FSMs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
状态设计模式是实现 FSM 的一种有趣方式:
http://en.wikipedia.org/wiki /State_pattern
这是实现 FSM 的一种非常干净的方式,但它可能会很混乱,具体取决于 FSM 的复杂性(而不是状态的数量)。然而,优点是:
此站点上有 Java 和 C++ 实现:
http://www.vincehuston.org/dp /state.html
The state Design Pattern is an interesting way of implementing a FSM:
http://en.wikipedia.org/wiki/State_pattern
It's a very clean way of implementing the FSM but it can be messy depending on the complexity of your FSM (but not the amount of states). However, the advantages are that:
There is a Java and C++ implementation at this site:
http://www.vincehuston.org/dp/state.html
总有一种我称之为“飞面面条怪”的 FSM 实现风格(FSM 风格的 FSM):使用大量的 goto。例如:
非常好的意大利面条代码:-)
There’s always what I call the Flying Spaghetti Monster’s style of implementing FSMs (FSM-style FSMs): using lotsa
goto
s. For example:Very nice spaghetti code :-)
我把它做成一个表,内存中的平面数组,每个单元格都是一个状态。请查看废弃的 DFA 项目的 cvs 源。对于示例:
但这是为了一个非常具体的需求(匹配正则表达式)
I did it as a table, a flat array in the memory, each cell is a state. Please have a look at the cvs source of the abandoned DFA project. For example:
But this was for a very specific need (matching regular expressions)
我记得我的第一个 FSM 程序。我用 C 语言编写了一个非常简单的 switch 语句。从一种状态切换到另一种状态或进入下一种状态似乎很自然。
然后我开始使用表查找方法。我能够使用这种方法编写一些非常通用的编码风格。然而,当需求发生变化并且我必须支持一些额外的活动时,我遇到了几次麻烦。
我最近没有写任何FSM。我写的最后一个是针对 C++ 中的通信模块,其中我将“状态设计模式”与“命令模式”(操作)结合使用。
I remember my first FSM program. I wrote it in C with a very simple switch statement. Switching from one state to another or following through to the next state seemed natural.
Then I progressed to use a table lookup approach. I was able to write some very generic coding style using this approach. However, I was caught out a couple of times when the requirements changed and I have to support some extra events.
I have not written any FSMs lately. The last one I wrote was for a comms module in C++ where I used a "state design pattern" in conjunction with a "command pattern" (action).
如果您要创建复杂的状态机,那么您可能需要查看 SMC - 状态机编译器。它采用状态机的文本表示并将其编译为您选择的语言 - 它支持 Java、C、C++、C#、Python、Ruby、Scala 等。
If you are creating a complex state machine then you may want to check out SMC - the State Machine Compiler. This takes a textual representation of a state machine and compiles it into the language of your choice - it supports Java, C, C++, C#, Python, Ruby, Scala and many others.