Examples

  1. A simple state machine: the door
  2. The TCP state machine
  3. A more complicated state machine, the life

A very simple state machine: the door

# Control a regular door

"Close it", "Open it", "Blow it out" : MESSAGE;

OPEN, CLOSED, GONE : STATE;

OPEN : 
   "Close it" -> CLOSED;

CLOSED :
   "Open it" -> OPEN;

OPEN, CLOSED :
   "Blow it out" -> GONE;

and the ASCII-art version, produced by Shadok via Graph::Easy.


             +--------------+
  +--------> |    CLOSED    | -+
  |          +--------------+  |
  |            |               |
  | Close it   | Open it       |
  |            v               |
  |          +--------------+  |
  +--------- |     OPEN     |  | Blow it out
             +--------------+  |
               |               |
               | Blow it out   |
               v               |
             +--------------+  |
             |     GONE     | <+
             +--------------+


and the PNG version, produced by Shadok via Graphviz. (Not displayed, see the ASCII-art version).

TCP state machine

TCP (Transmission Control Protocol) is described in RFC 793. Its state machine is more complicated

# The TCP state machine. RFC 793 3.2 "Terminology"

Title = "Transmission Control Protocol";

SYN-RCVD, SYN-SENT, FIN-WAIT-1, FIN-WAIT-2, ESTAB, 
      CLOSING, TIME-WAIT, CLOSED, CLOSE-WAIT, 
      LISTEN, LAST-ACK : STATE;

CLOSE, passive-OPEN, active-OPEN , 
      rcv-SYN, rcv-ACK-of-FIN, rcv-ACK-of-SYN, rcv-SYN-ACK, 
      rcv-FIN, SEND, Timeout : MESSAGE;

LISTEN : CLOSE -> CLOSED : Delete-TCB ;
# ALTERNATIVE syntax:
# CLOSE: LISTEN -> CLOSED : Delete-TCB ;
# ALTERNATIVE syntax:
# (LISTEN, CLOSE) -> CLOSED : Delete-TCB ;

CLOSED : passive-OPEN  -> LISTEN : Create-TCB;

LISTEN : rcv-SYN -> SYN-RCVD;

SYN-RCVD : CLOSE -> FIN-WAIT-1;

FIN-WAIT-1: rcv-ACK-of-FIN -> FIN-WAIT-2;

FIN-WAIT-1: rcv-FIN -> CLOSING;

FIN-WAIT-2 : rcv-FIN -> TIME-WAIT;

CLOSING : rcv-ACK-of-FIN -> TIME-WAIT;

CLOSED : active-OPEN -> SYN-SENT;

LISTEN : SEND -> SYN-SENT;

SYN-SENT : rcv-SYN -> SYN-RCVD;

SYN-RCVD : rcv-ACK-of-SYN -> ESTAB;

SYN-SENT : rcv-SYN-ACK-> ESTAB; 

ESTAB : CLOSE -> FIN-WAIT-1;

ESTAB : rcv-FIN -> CLOSE-WAIT;

CLOSE-WAIT : CLOSE -> LAST-ACK;

TIME-WAIT : Timeout -> CLOSED;

LAST-ACK : rcv-ACK-of-FIN -> CLOSED;

And the ASCII-art output


                                                             +-----------------+  rcv-FIN
                                                             |   CLOSE-WAIT    | <---------------------+
                                                             +-----------------+                       |
                                                               |                                       |
                                                               | CLOSE                                 |
                                                               v                                       |
                                                             +-----------------+                       |
                                                             |    LAST-ACK     |                       |
                                                             +-----------------+                       |
                                                               |                                       |
                                                               | rcv-ACK-of-FIN                        |
                                                               v                                       |
                                                             +--------------------------------------+  |
                                               +-----------> |                CLOSED                | <+-----------+
                                               |             +--------------------------------------+  |           |
                                               |               |                  |                    |           |
                                               | CLOSE         | passive-OPEN     |                    |           |
                                               |               v                  |                    |           |
                                               |             +-----------------+  |                    |           |
                                               +------------ |     LISTEN      | -+---------------+    |           |
                                                             +-----------------+  |               |    |           |
                                                               |                  |               |    |           |
                                                               | SEND             | active-OPEN   |    |           |
                                                               v                  |               |    |           |
                                                             +-----------------+  |               |    |           |
                                               +------------ |    SYN-SENT     | <+               |    |           |
                                               |             +-----------------+                  |    |           |
                                               |               |                                  |    |           |
                                               |               | rcv-SYN                          |    |           |
                                               |               v                                  |    |           |
                                               |             +-----------------+  rcv-SYN         |    |           |
                                         +-----+------------ |    SYN-RCVD     | <----------------+    |           |
                                         |     |             +-----------------+                       |           |
                                         |     |               |                                       |           |
                                         |     | rcv-SYN-ACK   | rcv-ACK-of-SYN                        |           |
                                         |     |               v                                       |           |
                                         |     |             +-----------------+                       |           |
                                         |     +-----------> |      ESTAB      | ----------------------+           |
                                         |                   +-----------------+                                   |
                                         |                     |                                                   |
                                         |                     | CLOSE                                 +-----------+
                                         |                     v                                       |
                   +------------+        |    CLOSE          +-----------------+                       |
  +--------------> | FIN-WAIT-2 |        +-----------------> |   FIN-WAIT-1    | -----------------+    |
  |                +------------+                            +-----------------+                  |    |
  |                  |                                         |                                  |    |
  |                  |                                         | rcv-FIN                          |    |
  |                  |                                         v                                  |    |
  |                  |                                       +-----------------+                  |    |
  |                  |                                       |     CLOSING     |                  |    |
  |                  |                                       +-----------------+                  |    |
  | rcv-ACK-of-FIN   |                                         |                                  |    |
  |                  |                                         | rcv-ACK-of-FIN                   |    |
  |                  |                                         v                                  |    |
  |                  |            rcv-FIN                    +-----------------+                  |    |
  |                  +-------------------------------------> |    TIME-WAIT    | -----------------+----+
  |                                                          +-----------------+                  |
  |                                                                                               |
  +-----------------------------------------------------------------------------------------------+


And the more graphical output (Not displayed, see the ASCII-art version).

Life state machine

# Just a test, do not take it seriously.

 Title = "Complicated state machine, to harass the processor";

Initial = 
       Limbo;
Final = Dead;

 Limbo : 
   birth -> Childhood;

 Limbo : RU486 -> Limbo;

Childhood : school -> 
   Manhood;

Manhood: wedding ->Honorability;
Honorability: divorce->
  Manhood;

Childhood: "Reading of Eric Raymond's books" -> Geekness;

Geekness: "Using Microsoft software", aging -> Manhood;

Manhood, Honorability: retirment->Oldness;

Childhood, Manhood, Honorability, Oldness: death -> Dead;

RU486, birth, school, wedding, "Reading of Eric Raymond's books", 
   "Using Microsoft software", divorce,   
   aging, retirment, death : MESSAGE;

Limbo, Childhood, Manhood, Honorability, Geekness, 
  Oldness, Dead: STATE;

And the ASCII-art output


                      +----------------------------------+   RU486
                      |                                  | ----------+
                      |              Limbo               |           |
                      |                                  | <---------+
                      +----------------------------------+
                        |
                        | birth
                        v
                      +----------------------------------+
              +------ |            Childhood             | ---------------+
              |       +----------------------------------+                |
              |         |                                                 |
              |         | Reading of Eric Raymond's books                 |
              |         v                                                 |
              |       +----------------------------------+                |
              |       |             Geekness             | -+             |
              |       +----------------------------------+  |             |
              |         |                                   |             |
              |         | Using Microsoft software          | aging       | school
              |         v                                   v             v
              |       +-----------------------------------------------------------+
  +-----------+-----> |                          Manhood                          |
  |           |       +-----------------------------------------------------------+
  |           |         |                                   |             |
  | divorce   |         | wedding                           |             |
  |           |         v                                   |             |
  |           |       +----------------------------------+  |             |
  +-----------+------ |           Honorability           | -+-------------+----------+
              |       +----------------------------------+  |             |          |
              |         |                                   |             |          |
              |         | retirment                         | retirment   |          |
              |         v                                   |             |          |
              |       +----------------------------------+  |             |          |
              | death |             Oldness              | <+             |          |
              |       +----------------------------------+                |          |
              |         |                                                 |          |
              |         | death                                           |          |
              |         v                                                 |          |
              |       +----------------------------------+  death         |          |
              +-----> |               Dead               | <--------------+          |
                      +----------------------------------+                           |
                        ^                                  death                     |
                        +------------------------------------------------------------+

And the more graphical output (Not displayed, see the ASCII-art version).