如何阅读FSM图 [英] How to read a FSM diagram

查看:316
本文介绍了如何阅读FSM图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用此图并将其转换为可用的程序.我不太确定如何阅读此图.如果有人可以带我走一下,也许给我看一个代码示例以及它与图表的关系,那就太好了.

How do i take this diagram and translate it into a useable program. I'm not really sure how to read this diagram. If someone could just kind of walk me through, maybe show an example of code and how it relates to the diagram, that be great.

谢谢!

推荐答案

内部带有文本的圆圈为状态.文字描述了状态.

Circles with text inside are the states. Text describes what the state is.

虚线箭头指向起始状态.

Dashed arrow points to starting state.

输出箭头确定此状态可能在何处更改.箭头旁边是文本,由该线分为上半部分和下半部分.下部是执行箭头转换时应执行的动作.上部是条件.当它们为true时-执行此转换(因此执行下半部分).

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符号表示您什么都不做,只能在发生过渡时更改当前状态.

Lambda symbol means you should do nothing except changing current state when transition takes place.

因此,较低的部分与您的功能相对应.上部是您应该等待条件的地方-轮询或异步等待未决的I/O数据包.

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.

这里有一些类似于C的伪代码(我在这里写过,所以不要以为它可以工作甚至编译):

Here is some pseudo-code similar to C (I've written it just here so do not assume it works or even compiles):

enum State { WaitFor0Call, WaitForAck0, WaitForCall1, WaitForAck1 }

int main() {
   State state = WaitFor0Call;
   while (1) {
      switch (state) {
         case WaitFor0Call:
            if (rdt_rcv(rcvpkt)) continue;
            if (rdt_send(data)) {
               state = WaitForAck0;
               sndpkt = make_pkt(0, data, checksum);
               udt_send(sndpkt);
               start_timer();
            }
            break;
         case WaitForAck0:
            // ...similar code...
            break;
         case WaitForCall1:
            // ...similar code...
            break;
         case WaitForAck1:
            // ...similar code...
            break;
      }
   }
}

您还应该考虑到接收和发送功能可能被阻塞,因此代码if (rdt_rcv(rcvpkt)) whatever;在技术上是不正确的,因为在返回控制之前,您不检查rdt_send.因此,FSM仅传达逻辑流程,而不传达其应如何组织的技术方面,线程管理等.我的代码也未显示此方面,因为它可能会根据您的需求而相当复杂,并且您没有提供足够的详细信息在这类事情上提供明智的建议:)

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 for rdt_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 :)

我唯一的猜测是您将有某种双向流(分别用于输入和输出),并且条件类似于if (there_is_ready_for_consuming_packet_in_the_input_queue) continue;if (data_was_put_to_outgoing_stream_successfully) ...;

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; and if (data_was_put_to_outgoing_stream_successfully) ...;

这篇关于如何阅读FSM图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆