Skip to content

NaoMod/FSM-with-MontiCore

Repository files navigation

FSM with MontiCore

This project shows an implementation of a Domain Specific Language for Finite State Machines with the language workbench MontiCore.

It consists of Java classes generated by Monticore from the grammar described in grammars/FSM.mc4. These source files have then been extended with handwritten code, such as in :

  • the fsm._cocos.hwc package

  • the fsm._visitor.hwc package

  • the fsm.interpreter package

  • the fsm.FSMCLI class

Using the DSL

Models can be created in any file (.fsm by convention). They must comply to the grammar defined in the file grammars/FSM.mc4.

With Gradle

Open a terminal and go to the FSM folder.

To run the example FSM, execute : gradle run --args="example/test.fsm"

More broadly, to run any FSM, execute : gradle run --args="<path-to-your-fsm-file>"

If you also want to check the context conditions before running the FSM, add the option -c (or --check) in the arguments : gradle run --args="<path-to-your-fsm-file> -c"

To regenerate the source files from the FSM grammar, run : gradle generateFSMsources. The sources become available in target/generated-sources.

With Eclipse

Open the FSM project : File > Open Projects from File System > Directory > FSM.

Then, create a new Run configuration : Run > Run Configurations > Java Application. In the Main tab, thhe Project is FSM and the Main class is fsm.FSMCLI.

Run configuration main

In the Arguments tab, you must specify the path to your FSM. If If you also want to check the context conditions before running the FSM, add the option -c (or --check).

Run configuration arguments

You can now apply the changes and run the configuration.

To regenerate the source files from the FSM grammar, you must download the Buildship Gradle Integration plugin from the Eclipse Marketplace (Help > Eclipse Marketplace). Then, open the Gradle Tasks (Window > Show View > Other > Gradle > Gradle Tasks). Double-click on the task FSM > build > build. When the Gradle task is done, refresh the FSM project (Right-click > Refresh). The sources become available in target/generated-sources.

Exploring the Definition

The grammar is formerly described in the file grammars/FSM.mc4.

FSM grammar

Some non-literals are introduced to describe the grammar of our language. Most importantly, the State non-literal is defined as a symbol. This means each occurrence can be referenced by its Name. References to States are made with Name@State.

Source code was generated from this grammar by MontiCore. This code was manually completed to define additional behavior. This code is available in the src/ folder, and manually added code includes :

  • Class fsm.FSMCLI : the entry point to use the rest of the classes in the infrastructure. Must be passed the path to a file containing the definition of an FSM as an argument.

  • Packages fsm.interpreter and fsm._visitor.hwc : contains the necessary classes to define the operationnal semantics of the FSM.

  • Package fsm._cocos.hwc : contains handwritten context conditions that define static semantics for the FSM. These conditions can optionally be checked when running _fsm.FSMCLI_ by passing the -c option in the arguments.