View Source EcspanseStateMachine (ECSpanse State Machine v0.3.3)
ECSpanse State Machine Api
Summary
Types
state names can be an atom or a string
Functions
Retrieves the current state of the state machine in the given entity if it is running.
Generates the source for a Mermaid State Diagram
Creates a component_spec for a State Machine
Returns a map of the state_machine to use in your projections.
Returns true/false if the state machine is running
Registers the state machine systems with the ECSpanse manager.
Starts the state machine
Stops the state machine
Triggers a state change
Triggers a state change to the default exit
Types
state names can be an atom or a string
Functions
@spec current_state(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: {:ok, state_name()} | {:error, :not_found} | {:error, :not_running}
Retrieves the current state of the state machine in the given entity if it is running.
@spec format_as_mermaid_diagram( Ecspanse.Entity.id() | Ecspanse.Entity.t(), String.t() ) :: {:ok, String.t()} | {:error, :not_found}
Generates the source for a Mermaid State Diagram
Parameters
- title: The diagram will have this title (optional)
@spec new(state_name(), [Keyword.t()], Keyword.t()) :: Ecspanse.Component.component_spec()
Creates a component_spec for a State Machine
Options
auto_start: boolean - if true, the state machine will start automatically
Examples
state_machine =
EcspanseStateMachine.new(:red, [
[name: :red, exits: [:green, :flashing_red], timeout: 30_000],
[name: :flashing_red, exits: [:red]],
[name: :green, exits: [:yellow], timeout: 10_000, default_exit: :yellow],
[name: :yellow, exits: [:red]]
])
@spec project(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: {:ok, map()} | {:error, :not_found}
Returns a map of the state_machine to use in your projections.
@spec running?(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: {:ok, boolean()} | {:error, :not_found}
Returns true/false if the state machine is running
@spec setup(Ecspanse.Data.t()) :: Ecspanse.Data.t()
Registers the state machine systems with the ECSpanse manager.
Call this from your setup(data) function. See ECSpanse Setup
Examples
def setup(data) do
data
|> EcspanseStateMachine.setup()
#
# Your registrations here
#
# Be sure to setup the Ecspanse.System.Timer if you have any 73s
|> Ecspanse.add_frame_end_system(Ecspanse.System.Timer)
end
@spec start(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: :ok | {:error, :already_running} | {:error, :not_found}
Starts the state machine
@spec stop(Ecspanse.Entity.id() | Ecspanse.Entity.t()) :: :ok | {:error, :already_running} | {:error, :not_found}
Stops the state machine
@spec transition( Ecspanse.Entity.id() | Ecspanse.Entity.t(), state_name(), state_name(), any() ) :: :ok | {:error, :not_found}
Triggers a state change
- the state machine is needs to be running
- the from state must be the current state
- the to state must be in the current state's exits
Parameters
- from: the state to transition from
- to: the state to transition to
- trigger: the reason for the transition
transition_to_default_exit(entity_id_or_entity, from, trigger \\ :request)
View Source@spec transition_to_default_exit( Ecspanse.Entity.id() | Ecspanse.Entity.t(), state_name(), any() ) :: :ok | {:error, :not_found}
Triggers a state change to the default exit
- the state machine is needs to be running
- the from state must be the current state
- the from state must have an exit
Parameters
- from: the state to transition from
- trigger: the reason for the transition