GK
Size: a a a
GK
ŹR
GK
GK
GK
ŹR
states = [:stop, :drive]
transitions = %{
stop: %{
gas: &gas_action/1
},
drive: %{
brake: &stop_action/1
}
}
AB
ŹR
AB
ŹR
GK
IA
IA
ŹR
defmodule FSM do
use GenServer
defstruct [
:states,
:transitions
]
def init(struct) do
{:ok, {Enum.at(struct.states, 0), struct}}
end
def handle_info({:transition, transition}, {current_state, struct}) do
case Map.get(struct.transitions, current_state) do
nil -> {:noreply, {current_state, struct}}
transition_map -> case Map.get(transition_map, transition) do
nil -> {:noreply, {current_state, struct}}
{action_name, next_state} ->
ActionPerformer.perform_action(action_name)
{:noreply, {next_state, struct}}
end
end
end
end
#######
states = [:stop, :drive]
transitions = %{
stop: %{
gas: {"gas_action", :drive}
},
drive: %{
brake: {"stop_action", :stop}
}
}
ŹR
AB
AB
GK
GK