State Machine Compiler inspired by Robert C. Martin, AKA "Uncle Bob", and his work.
This the State Machine Compiler (SMC) that Robert C. Martin builds in episodes 28, 29 and 30 of Clean Coders, built from scratch in C#.
It can be used to create Finite State Machines in several programming languages, like Java, C, Python, and C#, of course. In fact, it's built on C# 😄
Actions: Turnstile
FSM: TwoCoinTurnstile
Initial: Locked
{
(Base) Reset Locked Lock
Locked : Base
{
Pass Alarming -
Coin FirstCoin -
}
Alarming : Base >AlarmOn <AlarmOff
{
- - -
}
FirstCoin : Base
{
Pass Alarming -
Coin Unlocked Unlock
}
Unlocked : Base
{
Pass Locked Lock
Coin - ThankYou
}
}
The syntax is quite easy to understand:
- The first 3 lines are the header.
- After the header, there is a block enclosed with braces, this block is the definition of the transitions of the Finite State Machine (FSM)
- Each each transition consists of 4 values:
current state, event, next state, actions
. Notice that the actions can be 0 or more values enclosed by braces, like {action1, action2, ...) For instancedry rain wet { getUmbrella, getCoat }
. That means: given the "dry" state, when therain
event is received, turn to thewet
state and execute thegetUmbrella
andgetCoat
actions - However, the transitions of one state can be grouped under a block like this:
currentState { event, state, actions event, state, actions }
- The hyphen (-). It's a placeholder to indicate "nothing" or "unchanged". For instance, if a transition has its
next state
to -, it means that the next state is remains the same (the state won't change).
There are some cool features to make it easier to define the states:
- Base states. A state can derive from a base state, inheriting the transitions from the base state. It's denoted by
State : Base
- Entry and exit actions. You can define which actions happen every time a state enters or exists. Entry actions are denoted by
>action
and exit actions are denoted by<action
- Abstract states. They are states that only exists so other states can derive from it. They don't translate to a real state. They are denoted by
(state)
(the name between parentheses).
- Robert C. Martin for his wonderful lessons at Clean Coders
- Nicholas Blumhardt for the great parser combinator library Superpower