Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a FSM diagram

FMS

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.

Thanks!

like image 301
Matt Avatar asked Jul 20 '11 22:07

Matt


People also ask

What is FSM diagram?

A finite state machine (fsm) diagram, also called a statechart diagram, is a directed graph. The nodes represent internal states of some abstract machine. The arrows represent state transitions. A state transition is usually triggered by some event, such as receiving a signal, or timing out.

What is lambda in FSM?

FSM - Formal Definition. An input alphabet, (“sigma”), which is a set of symbols, one for each distinct possible input (e.g. buttons, coin slots, ...) An output alphabet, ¡ (“lambda”), which is another set of symbols, one for each distinct possible output (e.g. lights, can dispensed, ...)

What FSM explain the components of it briefly?

Finite State Machine Components Finite State Machines are comprised of these components: Set of Known States: as the name implies, there must be a finite amount of states our system can be in. Only one state can be active at a time.


1 Answers

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

like image 185
Ivan Danilov Avatar answered Sep 18 '22 05:09

Ivan Danilov