Skip to content

A finite state machine library.

Notifications You must be signed in to change notification settings

catchcake/ex_state

Repository files navigation

ExState

Elixir finite state machines and statecharts library.

Quick start

Install

def deps do
  [
    {:ex_state, "~> 2.0.1"}
  ]
end

Usage

  iex> definition = %{
  iex>   id: "toggle",
  iex>   initial: :inactive,
  iex>   states: %{
  iex>     inactive: %{
  iex>       on: %{
  iex>         TOGGLE: %{
  iex>           target: :active
  iex>         }
  iex>       }
  iex>     },
  iex>     active: %{
  iex>       on: %{
  iex>         TOGGLE: %{
  iex>           target: :inactive
  iex>         }
  iex>       }
  iex>     }
  iex>   }
  iex> }
  iex> machine = ExState.machine(definition)  # and now create state machine
  iex> ExState.transition(machine, :TOGGLE)  # and transit to new state
  %ExState.Machine{
    context: nil,
    id: "toggle",
    initial: :inactive,
    options: %ExState.Options{actions: nil, guards: nil},
    state: %ExState.State{
      context: nil,
      event: %ExState.Event{data: nil, type: :TOGGLE},
      value: :active
    },
    states: %{
      active: %ExState.NodeType.Atomic{
        on: %{
          TOGGLE: %ExState.Transition{actions: [], cond: nil, target: :inactive}
        }
      },
      inactive: %ExState.NodeType.Atomic{
        on: %{
          TOGGLE: %ExState.Transition{actions: [], cond: nil, target: :active}
        }
      }
    }
  }

Statecharts?

Statecharts are a formalism for modeling stateful, reactive systems. This is useful for declaratively describing the behavior of your application, from the individual components to the overall application logic.

License

TODO: Add license