:states is a basic
Finite State Machine (FSM)
package for the
Arturo Programming language
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.
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
-
"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. ↩