exfsm v0.1.1 ExFSM.Machine
Module to simply use FSMs defined with ExFSM :
ExFSM.Machine.fsm/1
merge fsm from multiple handlers (seeExFSM
to see how to define one).ExFSM.Machine.event_bypasses/1
merge bypasses from multiple handlers (seeExFSM
to see how to define one).ExFSM.Machine.event/2
allows you to execute the correct handler from a state and action
Define a structure implementing ExFSM.Machine.State
in order to
define how to extract handlers and state_name from state, and how
to apply state_name change. Then use ExFSM.Machine.event/2
in order
to execute transition.
iex> defmodule Elixir.Door1 do
...> use ExFSM
...> deftrans closed({:open_door,_},s) do {:next_state,:opened,s} end
...> end
...> defmodule Elixir.Door2 do
...> use ExFSM
...> defbypass close_door(_,s), do: {:keep_state,Map.put(s,:doubleclosed,true)}
...> deftrans opened({:close_door,_},s) do {:next_state,:closed,s} end
...> end
...> ExFSM.Machine.fsm([Door1,Door2])
%{
{:closed,:open_door}=>{Door1,[:opened]},
{:opened,:close_door}=>{Door2,[:closed]}
}
iex> ExFSM.Machine.event_bypasses([Door1,Door2])
%{close_door: Door2}
iex> defmodule Elixir.DoorState do defstruct(handlers: [Door1,Door2], state: nil, doubleclosed: false) end
...> defimpl ExFSM.Machine.State, for: DoorState do
...> def handlers(d) do d.handlers end
...> def state_name(d) do d.state end
...> def set_state_name(d,name) do %{d|state: name} end
...> end
...> struct(DoorState, state: :closed) |> ExFSM.Machine.event({:open_door,nil})
{:next_state,%{__struct__: DoorState, handlers: [Door1,Door2],state: :opened, doubleclosed: false}}
...> struct(DoorState, state: :closed) |> ExFSM.Machine.event({:close_door,nil})
{:bypass,%{__struct__: DoorState, handlers: [Door1,Door2],state: :closed, doubleclosed: true}}
Summary
Functions
Meta application of the transition function, using find_handler/2
to find the module implementing it
same as find_handler/2
but using a ‘meta’ state implementing ExFSM.Machine.State
find the ExFSM Module from the list handlers
implementing the event action
from state_name
return the FSM as a map of transitions %{{state,action}=>{handler,[dest_states]}} based on handlers
Types
meta_event_reply ::
{:next_state, ExFSM.Machine.State.t} |
{:next_state, ExFSM.Machine.State.t, timeout :: integer} |
{:error, :illegal_action}
Functions
Specs
action_available?(ExFSM.Machine.State.t, action_name :: atom) :: boolean
Specs
event(ExFSM.Machine.State.t, {event_name :: atom, event_params :: any}) :: meta_event_reply
Meta application of the transition function, using find_handler/2
to find the module implementing it.
same as find_handler/2
but using a ‘meta’ state implementing ExFSM.Machine.State
Specs
find_handler({state_name :: atom, event_name :: atom}, [exfsm_module :: atom]) :: exfsm_module :: atom
find the ExFSM Module from the list handlers
implementing the event action
from state_name
Specs
fsm([exfsm_module :: atom]) :: ExFSM.fsm_spec
return the FSM as a map of transitions %{{state,action}=>{handler,[dest_states]}} based on handlers