All Projects → bluzky → as_fsm

bluzky / as_fsm

Licence: other
A finite state machine implementation for elixir

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to as fsm

FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (+221.43%)
Mutual labels:  fsm, finite-state-machine
Microwf
A simple finite state machine (FSM) with workflow character where you define your workflows in code.
Stars: ✭ 122 (+771.43%)
Mutual labels:  fsm, finite-state-machine
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (+528.57%)
Mutual labels:  fsm, finite-state-machine
raider
OWASP Raider: a novel framework for manipulating the HTTP processes of persistent sessions
Stars: ✭ 88 (+528.57%)
Mutual labels:  fsm, finite-state-machine
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+13457.14%)
Mutual labels:  fsm, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (+500%)
Mutual labels:  fsm, finite-state-machine
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (+707.14%)
Mutual labels:  fsm, finite-state-machine
Libfsm
DFA regular expression library & friends
Stars: ✭ 512 (+3557.14%)
Mutual labels:  fsm, finite-state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (+78.57%)
Mutual labels:  fsm, finite-state-machine
Statelin
A finite state machine for Kotlin and Android
Stars: ✭ 134 (+857.14%)
Mutual labels:  fsm, finite-state-machine
visual-automata
Visual Automata is a Python 3 library built as a wrapper for the Automata library to add more visualization features.
Stars: ✭ 55 (+292.86%)
Mutual labels:  fsm, finite-state-machine
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (+1292.86%)
Mutual labels:  fsm, finite-state-machine
Floatsidebar.js
Lightweight (2kb gzipped), zero-dependency javascript library for making float sidebars based on the finite state machine
Stars: ✭ 56 (+300%)
Mutual labels:  fsm, finite-state-machine
Fsm
Finite State Machine for Go
Stars: ✭ 1,269 (+8964.29%)
Mutual labels:  fsm, finite-state-machine
Stateless4j
Lightweight Java State Machine
Stars: ✭ 658 (+4600%)
Mutual labels:  fsm, finite-state-machine
Alexafsm
With alexafsm, developers can model dialog agents with first-class concepts such as states, attributes, transition, and actions. alexafsm also provides visualization and other tools to help understand, test, debug, and maintain complex FSM conversations.
Stars: ✭ 103 (+635.71%)
Mutual labels:  fsm, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+1792.86%)
Mutual labels:  fsm, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+3085.71%)
Mutual labels:  fsm, finite-state-machine
Automata
A Python library for simulating finite automata, pushdown automata, and Turing machines
Stars: ✭ 121 (+764.29%)
Mutual labels:  fsm, finite-state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (+978.57%)
Mutual labels:  fsm, finite-state-machine

Elixir Finite state machine

This package is inspired by ecto_fsm package

This package allows to use finite state machine pattern in elixir.

I have rewritten this library to make code simple and easier to use Install

def deps do
  [
    {:as_fsm, "~> 2.0.0"}
  ]
end

Usage

First you to define FSM module

defmodule TaskFsm do
  use AsFsm, repo: MyApp.Repo
  # by default state is check from column `state` of struct
  # you can specify your own with
  # use AsFsm, repo: MyApp.Repo, column: :status

  # define your event
  defevent(:start, from: :idle, to: :running)
  defevent(:pause, from: :running, to: :paused)
  defevent(:stop, from: [:running, :paused], to: :idle)

  # you can define some hook
  # it is automatically invoked if defined

  def before_start(context) do
    # do something then return context
    context
  end

  def on_start(context) do
    # do something then return context
    context
  end
end

All appropriate event function will be generated. In this example we have

def start(context), do: ....
def paus(context), do: ....
def stop(context), do: ....

Then use it

  • Trigger an even transition
my_task
|> TaskFsm.new_context(other_params)
|> TaskFsm.start()
  • Or trigger by name
my_task
|> TaskFsm.new_context(other_params)
|> TaskFsm.trigger(:start)

Understand the context

@type :: %Context{
  struct: struct(),
  state: any(),
  valid?: boolean(),
  error: String.t() | nil,
  multi: Ecto.Multi.t() | nil
}
  • struct is your data
  • state any data you want to pass to transition, it could be parameter from client
  • valid? if it is true, then data will be persisted
  • error error message in case valid? is false
  • multi is an Ecto.Multi you can pass a multi to new_context(), it make sure all action you do in a transaction

Event hook

For each event you can define 2 hook

  • before_hook you can define this hook to check for some condition before doing transation
  • on_hook this is your hook to do some logic on transaction

These 2 hooks must return a context. If you want to stop this transition, set valid? to false and return the context.

Custom persist struct

You can define your own function to persist struct state. This function is run within Multi so that it must return {:ok, data} | {:error, reason}

def persist(struct, new_state, _context) do
  # do your update logic
  # or write log here
end

Options

  • Custom property name by default property name is :state
defmodule ExampleFsm do

  use AsFsm,
    column: :status,
end
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].