State Machine Compiler inspired by Uncle Bob
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:
current state, event, next state, actions
. Notice that the actions can be 0 or more values enclosed by braces, like {action1, action2, …)dry rain wet { getUmbrella, getCoat }
. That means: given the “dry” state, when the rain
event is received, turn to the wet
state and execute the getUmbrella
and getCoat
actionscurrentState
{
event, state, actions
event, state, actions
}
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:
State : Base
>action
and exit actions are denoted by <action
(state)
(the name between parentheses).