Code: Finite state machines in Jennifer
Finite state machines in Jennifer: the code
The programs behind the slides, for Jennifer 0.27.0. flags.j and turnstile.j need nothing but the interpreter; every other program uses the @deckyard/fsm deck listed in deck.toml, so run jvc install once in this folder to fill vendor/. No server and no network are needed after that. Run everything from this folder: load.j reads order.toml by relative path.
flags.j: a connection kept in three boolean flags, and a combination that makes no sense ("Flags and nested ifs"). Run:jennifer run flags.jturnstile.j: the turnstile by hand, with an enum for the states, one for the events and a nestedmatch("By hand: two enums and a match"). Run:jennifer run turnstile.jturnstile_fsm.j: the same turnstile with the deck, and the events allowed in the last state ("The turnstile with the deck"). Run:jennifer run turnstile_fsm.jrefused.j: an event with no transition, an unknown state, a misspelt state and a transition that could never be taken ("Refused when built, refused when fired"). Run:jennifer run refused.jorders.j: the module with the order life cycle (build), its guardsisManagerandisPaid, the enter hookstamp, andfunctionsforfsm.load("A real one", "Guards", "Hooks"). Imported by the programs below.order.j: one order walked from draft to shipped, withevents,canand a guard that refuses ("Walking an order"). Run:jennifer run order.jcheck.j: a draft machine with a dead end, a final state with an exit and an unreachable state, found byfsm.check("check: flaws in the definition"). Run:jennifer run check.jorder.toml: the same order machine as a TOML document.load.j: loadsorder.toml, compares it withorders.build(), shows a misspelt key being refused and prints the Mermaid diagram ("The machine as data", "Drawing it"). Run:jennifer run load.jstore.j: states read back from storage, checked withisStateandisFinal("Stored states"). Run:jennifer run store.jlifecycle_test.j: tests for the definition, paths through it, the events each role sees and refused events ("Testing a machine"). Run:jennifer test lifecycle_test.j
| File | Size | |
|---|---|---|
| LICENSE | 899 B | raw |
| README.md | 2.2 KiB | raw |
| check.j | 590 B | raw |
| deck.toml | 247 B | raw |
| flags.j | 598 B | raw |
| lifecycle_test.j | 1.7 KiB | raw |
| load.j | 575 B | raw |
| order.j | 808 B | raw |
| order.toml | 677 B | raw |
| orders.j | 1.4 KiB | raw |
| refused.j | 825 B | raw |
| store.j | 746 B | raw |
| turnstile.j | 674 B | raw |
| turnstile_fsm.j | 528 B | raw |