This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
1.0.0
A simple state machine implementation in Elm
version 2.0.0
license BSD3
native-modules False
elm-version 0.16.0 <= v < 0.17.0
Tag 2.0.0
Committed At 2016-01-11 21:50:15 UTC
elm-lang/core 3.0.0 <= v < 4.0.0 3.0.0

Modules

README

Statey

A state machine library, written in Elm. Very much a work in progress!

View the docs on Elm Package.

The example tests aim to be a good Elm file to take as your starting point for using this library.

Usage (taken from example tests)

module ExampleTests (..) where

{-| these tests serve as a good intro to the library
-}

import Statey exposing (..)
import ElmTest exposing (..)


type alias Person =
    StateRecord { name : String }


startState =
    makeState "start"


tiredState =
    makeState "tired"


sleepState =
    makeState "sleep"


stateMachine : StateMachine Person
stateMachine =
    { states = [ startState, tiredState, sleepState ]
    , transitions =
        [ ( startState, tiredState )
        , ( tiredState, sleepState )
        , ( sleepState, startState )
        ]
    , guards =
        [ { from = tiredState, to = sleepState, fn = \person -> person.name /= "Jack" }
        ]
    }


person =
    { name = "Jack", state = startState }


tiredPerson =
    { name = "Jack", state = tiredState }


tests : Test
tests =
    suite
        "Example tests"
        [ test
            "it can tell you the state of a record"
            (assertEqual startState (getState person))
        , test
            "it can transition a person through a state"
            (assertEqual
                (Ok { person | state = tiredState })
                (transition stateMachine tiredState person)
            )
        , test
            "but only if the transition is valid"
            (assertEqual
                (Err TransitionNotDefined)
                (transition stateMachine sleepState person)
            )
        , test
            "a guard that returns False stops a transition"
            (assertEqual
                (Err GuardPreventedTansition)
                (transition stateMachine sleepState tiredPerson)
            )
        ]

Guards

You can also add guards into your state machine to protect against certain transitions:

guardedStateMachine =
    { states = [ startState, middleState, endState ]
    , transitions = [ ( startState, middleState ), ( middleState, endState ) ]
    , guards =
        [ { from = startState, to = middleState, fn = (\_ -> False) }
        ]
    }


person = { name = "Jack", state = startState }
-- invalid, guard from start -> middle returns False
case transition stateMachine middleState person of
    Ok newPerson -> ---
    Err err -> err == GuardPreventedTansition

Guard callbacks are passed the record:

-- the guard callback is given the record:

guardedStateMachine =
    { states = [ startState, middleState, endState ]
    , transitions = [ ( startState, middleState ), ( middleState, endState ) ]
    , guards =
        [ { from = startState, to = middleState, fn = (\person -> person.name /= "Jack") }
        ]
    }


person = { name = "Jack", state = startState }
-- invalid, person.name == "Jack"
case transition stateMachine middleState person of
    Ok newPerson -> ---
    Err err -> err == GuardPreventedTansition