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.

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

Types

@type state_name() :: atom() | String.t()

state names can be an atom or a string

Functions

Link to this function

current_state(entity_id_or_entity)

View Source
@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.

Link to this function

format_as_mermaid_diagram(entity_id_or_entity, title \\ "")

View Source
@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)
Link to this function

new(initial_state, states, opts \\ [])

View Source

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]]
  ])
Link to this function

project(entity_id_or_entity)

View Source
@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.

Link to this function

running?(entity_id_or_entity)

View Source
@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
Link to this function

start(entity_id_or_entity)

View Source
@spec start(Ecspanse.Entity.id() | Ecspanse.Entity.t()) ::
  :ok | {:error, :already_running} | {:error, :not_found}

Starts the state machine

Link to this function

stop(entity_id_or_entity)

View Source
@spec stop(Ecspanse.Entity.id() | Ecspanse.Entity.t()) ::
  :ok | {:error, :already_running} | {:error, :not_found}

Stops the state machine

Link to this function

transition(entity_id_or_entity, from, to, trigger \\ :request)

View Source
@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
Link to this function

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