如何阅读 FSM 图
我如何获取此图并将其转换为可用的程序。
我不太确定如何阅读该图。
引导我完成它,也许会展示一个代码示例以及它与图表的关系。
How do I take this diagram and translate it into a useable program.
I'm not really sure how to read this diagram.
Walk me through it, maybe show an example of code and how it relates to the diagram.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
里面有文字的圆圈是州。文本描述了状态是什么。
虚线箭头指向起始状态。
传出箭头确定此状态可以更改的位置。箭头旁边是被线分为上半部分和下半部分的文本。下半部分是执行箭头转换时应该发生的动作。上半部分是条件。当它们为真时 - 执行此转换(以及下部)。
Lambda 符号意味着除了在转换发生时更改当前状态之外,您不应执行任何操作。
所以下面的部分有与你的功能相对应的粗略部分。上面的部分是您应该等待条件的点 - 轮询或异步等待挂起的 I/O 数据包,等等。
下面是一些类似于 C 的伪代码(我只是在这里编写了它,所以不要假设它可以工作,甚至可以编译):
您还应该考虑到接收和发送函数可能会阻塞,因此代码
if ( rdt_rcv(rcvpkt)) 无论如何;
在技术上是不正确的,因为您在返回控制之前不会检查rdt_send
。因此,FSM 仅传达逻辑流,而不传达如何组织、线程管理等技术方面的信息。我的代码也没有显示这些方面,因为根据您的需求,它可能会相当复杂,而且您没有提供足够的详细信息对此类事情提出明智的建议:)我唯一的猜测是您将拥有某种双向流(分别用于输入和输出)并且条件类似于
if (there_is_ready_for_consuming_packet_in_the_input_queue) continue;
和if (data_was_put_to_outgoing_stream_successively) ...;
Circles with text inside are the states. Text describes what the state is.
Dashed arrow points to starting state.
Outgoing arrows determine where this state could change. Beside of the arrow is the text divided by the line into upper part and lower part. Lower part is actions that should take place when arrow transition is executed. Upper part is conditions. When they are true - this transition is executed (and so lower part).
Lambda symbol means you should do nothing except changing current state when transition takes place.
So lower parts have coarse corresponding to your functions. And upper parts are the points where you should wait for conditions - polling or async waiting for pending I/O packets, whatever.
Here is some pseudo-code similar to C (I've written it just here so do not assume it works or even compiles):
You should also take into account that receive and send functions could be blocking, so code
if (rdt_rcv(rcvpkt)) whatever;
is technically incorrect as you don't check forrdt_send
until it returns control. So FSM communicates only logical flow, not technical aspects of how it should be organized, thread management etc. And my code doesn't show this aspects also because it could be decently complicated depending on your needs and because you didn't give enough details to make informed advices on these sort of things :)My only guess is that you would have some sort of bi-directed stream (for input and output respectively) and conditions would be like
if (there_is_ready_for_consuming_packet_in_the_input_queue) continue;
andif (data_was_put_to_outgoing_stream_successfully) ...;