By Stepan Mitkin
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...
DRAKON Editor can generate Erlang source code from DRAKON state machine diagrams. Two flavours of generated code are available:
State machines based on gen_fsm behaviour. The generated state machine will live in a separate process. Benefits:
Standalone state machine. The generated state machine will be just a tuple in the memory. Benefits:
Create a new diagram. Select the DRAKON Silhouette diagram type.
The name of the diagram must be 'state machine'.
The branches of the silhouette designate states. The state names must be valid Erlang atom names.
The "address" icons at the bottom point to next states.
The leftmost branch is the initial state.
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.
The first icon in a branch must be a "Select" icon with text "receive".
The "Case" icons under the "receive" designate the accepted event types. An empty "Case" icon mean "all other event types".
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.
The variable 'State' is always available. It is the previous data content of the machine.
For gen_fsm-based state machines, DRAKON Editor generates a start_link/2 function:
{ok, Pid} = start_link(InitialData, Options)
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.
In order to tell DRAKON Editor to generate a standalone state machine, put 'standalone' in the "Formal parameters" icon like this:
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