如何阅读 FSM 图

发布于 2024-11-25 13:52:04 字数 143 浏览 1 评论 0原文

FMS

我如何获取此图并将其转换为可用的程序。

我不太确定如何阅读该图。

引导我完成它,也许会展示一个代码示例以及它与图表的关系。

FMS

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-12-02 13:52:04

里面有文字的圆圈是州。文本描述了状态是什么。

虚线箭头指向起始状态。

传出箭头确定此状态可以更改的位置。箭头旁边是被线分为上半部分和下半部分的文本。下半部分是执行箭头转换时应该发生的动作。上半部分是条件。当它们为真时 - 执行此转换(以及下部)。

Lambda 符号意味着除了在转换发生时更改当前状态之外,您不应执行任何操作。

所以下面的部分有与你的功能相对应的粗略部分。上面的部分是您应该等待条件的点 - 轮询或异步等待挂起的 I/O 数据包,等等。

下面是一些类似于 C 的伪代码(我只是在这里编写了它,所以不要假设它可以工作,甚至可以编译):

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)) 无论如何; 在技术上是不正确的,因为您在返回控制之前不会检查 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):

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;
      }
   }
}

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

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) ...;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文