exfsm v0.1.2 ExFSM

After use ExFSM : define FSM transition handler with deftrans fromstate({action_name,params},state). A function fsm will be created returning a map of the fsm_spec describing the fsm.

Destination states are found with AST introspection, if the {:next_state,xxx,xxx} is defined outside the deftrans/2 function, you have to define them manually defining a @to attribute.

For instance :

iex> defmodule Elixir.Door do
...>   use ExFSM
...>   deftrans closed({:open_door,_params},state) do
...>     {:next_state,:opened,state}
...>   end
...>   @to [:closed]
...>   deftrans opened({:close_door,_params},state) do
...>     then = :closed
...>     {:next_state,then,state}
...>   end
...> end
...> Door.fsm
%{
  {:closed,:open_door}=>{Door,[:opened]},
  {:opened,:close_door}=>{Door,[:closed]}
}

Summary

Macros

Define a function of type transition describing a state and its transition. The function name is the state name, the transition is the first argument. A state object can be modified and is the second argument

Types

fsm_spec :: %{{state_name :: atom, event_name :: atom} => {exfsm_module :: atom, [dest_statename :: atom]}}
transition :: ({event_name :: atom, event_param :: any}, state :: any -> {:next_state, event_name :: atom, state :: any})

Macros

defbypass(signature, body_block)
deftrans(signature, body_block)

Define a function of type transition describing a state and its transition. The function name is the state name, the transition is the first argument. A state object can be modified and is the second argument.

  deftrans opened({:close_door,_params},state) do
    {:next_state,:closed,state}
  end