State Machines in DRAKON-Erlang

Home

By Stepan Mitkin

About state machines

State machines are similar to objects from object-oriented programming. They also react to messages and keep information inside them. The difference is that a state machine can switch between different modes of functioning. The switching is done explicitly. And this is a great advantage of state machines as an abstraction.

DRAKON offers a convenient way to draw state machines. Both the algorithm of the machine and the state switching logic are visible on the same DRAKON diagram. Read more...

Standalone and OTP state machines

DRAKON Editor can generate Erlang source code from DRAKON state machine diagrams. Two flavours of generated code are available:

  1. State machines based on gen_fsm behaviour. The generated state machine will live in a separate process. Benefits:

    • The machine can be a full member of the supervision tree, enjoying all benefits of OTP.
    • Timeouts can be used.
    • Process dictionary can be used.
  2. Standalone state machine. The generated state machine will be just a tuple in the memory. Benefits:

    • Very lightweight.
    • No need to think about termination.

How to create a state machine

  1. Create a new diagram. Select the DRAKON Silhouette diagram type.

    Create new

  2. The name of the diagram must be 'state machine'.

  3. The branches of the silhouette designate states. The state names must be valid Erlang atom names.

  4. The "address" icons at the bottom point to next states.

  5. The leftmost branch is the initial state.

  6. The rightmost branch is the final state. Do not place any other icons on the final state. It is possible to switch to the final state. But sending an event to a machine in the final state will throw an exception.

  7. The first icon in a branch must be a "Select" icon with text "receive".

  8. The "Case" icons under the "receive" designate the accepted event types. An empty "Case" icon mean "all other event types".

  9. The last icon in a branch must contain the next data content of the machine. For gen_fsm-based state machines, the last icon may also contain the timeout value in milliseconds. The timeout value is separated by a comma from the next data content.

  10. The variable 'State' is always available. It is the previous data content of the machine.

Generated code for gen_fsm state machines

For gen_fsm-based state machines, DRAKON Editor generates a start_link/2 function:

{ok, Pid} = start_link(InitialData, Options)
  • InitialData is the initial data content of the state machine.
  • Options are passed to gen_fsm:start_link as is.

In order to send an event to the state machine, use the gen_fsm:send_event function like this:

gen_fsm:send_event(MachinePid, MyEvent)

The state machine is terminated as a usual gen_fsm state machine process. See the details here: http://www.erlang.org/doc/design_principles/fsm.html

The user can define his own init/1 function that gen_fsm expects. If the user does not provide his own init/1, DRAKON Editor will generate one.

Generated code for standalone state machines

In order to tell DRAKON Editor to generate a standalone state machine, put 'standalone' in the "Formal parameters" icon like this:

Standalone marker

Generated functions:

Machine = create(InitialData)
NewMachine = send_event(OldMachine, Event)
CurrentStateName = get_state(Machine)
CurrentData = get_data(Machine)

No specific code is required for terminating a standalone machine. It is just a tuple.


2 February 2014.

Contact: drakon.editor@gmail.com