FSMC is a scala application that generates a finite state machine from a file with a specific syntax and outputs it as a nested switch case statement in C#
json4s was used to build the json of the syntax data structure
The repository has a compiler folder with fsmcompiler.jar
and a sample car.fsm
file showing the syntax of fsmc.
In the same directory run the command java -jar fsmcompiler.jar <language> <.fsm file path>
<language>
is only C#<.fsm file path>
is one or multiple paths to the .fsm files- Additional command
gen=<generated code directory>
which by default is the current directory - Additional command
json=<syntax structure directory>
to output the data structure of the finite state machine
Actions : Key
FSM : Car
Initial : Locked
{
Locked {
Open Unlocked unlock
Force Alarming {lock notify}
}
Alarming >alarmOn <alarmOff {
Reset Locked -
}
Unlocked {
Close Locked lock
Reset Locked lock
}
}
Actions
will generated an enum containing all the actionsFSM
will be the file name and also part of the enum containing the fsm statesInitial
is the initial state of the finite state machine
Locked >entryFunction <exitFunction {
Open Unlocked unlock
Force Alarming {lock notify}
}
Locked
is the state of the fsm to execute all the subtransitions>entryFunction
is the function that will be executed when transitioning in the state, this is optional<exitFunction
is the function that will be executed when transitioning out of the state, this is optional
{ action nextState - }
no function will be called{ action nextState function}
{ action nextState {function1 function2} }
- Lexer converts the stream of data into a stream of tokens used by the Parser as events
- Parser is a finite state machine that transfers a sequence of events to the builder
- Syntax Builder creates the language data structure
- Generator converts the syntax data structure into a syntax node which is easier to parse from different visitors
- Language Visitors generate the finite state machine in different programming languages
Implement the Language Visitor interface just like the CSharpVisistor.scala update the visitors map in the Config.scala file
The idea comes from the fsm compiler of Uncle Bob with a different implementation