View Source EcspanseStateMachine (ECSpanse State Machine v0.3.0)

ECSpanse State Machine Api

Summary

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.

Registers the state machine systems with the ECSpanse manager.

Starts the state machine

Stops a state machine

Triggers a state change to the default exit

Types

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

Functions

Link to this function

current_state(entity_id)

View Source
@spec current_state(Ecspanse.Entity.id()) ::
  {: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, title \\ "")

View Source
@spec format_as_mermaid_diagram(Ecspanse.Entity.id(), 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]]
  ])

Returns a map of the state_machine to use in your projections.

@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()) ::
  :ok | {:error, :already_running} | {:error, :not_found}

Starts the state machine

@spec stop(Ecspanse.Entity.id()) ::
  :ok | {:error, :not_running} | {:error, :not_found}

Stops a state machine

Link to this function

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

View Source
@spec transition(Ecspanse.Entity.id(), 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, from, trigger \\ :request)

View Source
@spec transition_to_default_exit(Ecspanse.Entity.id(), 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