Skip to content

:states is a lean Finite State Machine package for the Arturo Programming Language

License

Notifications You must be signed in to change notification settings

RickBarretto/states.art

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

:states

:states is a basic Finite State Machine (FSM) package for the Arturo Programming language

Arturo logo Arturo logo

At a Glance

Running states.art screenshot

Inspiration

This package was highly inspired by Harrington Joseph's 1 talk "When Booleans Are Not Enough... State Machines?" 2. By consequence, this is inspired by the pytransitions package.

Tip

If you want to know why you need a FSM, I recommend you to watch his talk first.

Trying :states

Defining a finite state-machine is simple like that:

import {states}!

turnstile: to :states ['locked [
    push        /  locked    ~>  locked
    push        /  unlocked  ~>  locked
    insertCoin  /  locked    ~>  unlocked
    insertCoin  /  unlocked  ~>  unlocked
]]

Explanation

  • The first parameter is the initial state,
  • The second parameter is the definition os the transitions, this must follow the following order: event, source, fate, being or not separated by symbols.
  • You can choose what symbol to use to separate them, or even choose by don't use them, making this a trilema.

Getting the current state

To get the current state, you must call the state method:

turnstile\state 
; => locked

Transitioning between states

There are two equivalent ways of transitioning between states, you can use <fsm>/<event> or <fsm>\changeFrom '<event>:

turnstile\state                   
; => locked
turnstile\push                   
; => locked
turnstile\insertCoin             
; => unlocked
turnstile\insertCoin             
; => unlocked

turnstile\changeFrom 'push       
; => locked
turnstile\changeFrom 'insertCoin 
; => unlocked

Background photo on "At a Glance" by Jack Anstey on Unsplash

Footnotes

  1. You can find him on Twitter or Github as @harph.

  2. "When Booleans Are Not Enough... State Machines?" by Harrington Joseph can be found in two places, on the Channel Next Day Video Presented on PyTexas 2019 and on the Channel SF Python presented on SF Python Meetup Feb 2019.