EMF State Machines: Markus Voelter
EMF State Machines: Markus Voelter
EMF State Machines: Markus Voelter
1/7
Table of Contents
INTRODUCTION............................................................................................................................... 3 INSTALLATION................................................................................................................................ 3 METAMODEL.................................................................................................................................... 3 EXAMPLE STATEMACHINE......................................................................................................... 4 RUNNING THE EXAMPLE.......................................................................................................................... 5 THE GENERATOR............................................................................................................................5 WORKFLOW.......................................................................................................................................... 5 CONSTRAINTS ....................................................................................................................................... 5 TEMPLATES........................................................................................................................................... 6 RECIPE CREATION..................................................................................................................................6
2/7
Introduction
This example shows how to implement a state machine generator using EMF and openArchitectureWare. Note that the implementation of the state machine in Java is probably the slowest and clumsiest way to implement a state machine. The focus was not on optimizing the performance of the state machien implementation. This tutorial does not explain too much it's rather a guide through the example code. We expect that you know how to work with openArchitectureWare and EMF. If that's not the case, you should read and play with the emfHelloWorld example first (the tutorial entitled Generating Code from EMF Models).
Installation
In the emf examples package, you can find the following three projects
oaw4.demo.emf.statemachine: contains the metamodel oaw4.demo.emf.statemachine.generator: contains the code generator oaw4.demo.emf.statemachine.example: contains an example state machine as well as a manually written unit test
You have to install all the projects into your workspace. You also have to (as with all other examples) set the following three Eclipse classpath variables: Variable OAW_CORE OAW_LIB OAW_RECIPE_CORE ... points to oaw4/oaw-core-4.x.x oaw4/oaw-core-4.x.x/lib oaw4/oaw-recipe-core-4.x.x
Metamodel
The metamodel looks more or less as you'd expect from a state machine metamodel. The following is the representation of the metamodel in Emfatic syntax. You can find it in the oaw4.demo.emf.statemachine/model package.
3/7
abstract class Named { attr String name; } class State extends AbstractState { val Action entryAction; val Action exitAction; } class StartState extends AbstractState { } class } class ref val ref } class } class } StopState extends AbstractState { Transition extends Named { AbstractState[1] target; Action action; Event event; Action extends Named { Event extends Named {
class CompositeEvent extends Event { val Event[*] children; } class StateMachine extends Named { val AbstractState[*] states; val Event[*] events; } abstract class AbstractState extends Named { val Transition[*] transition; }
From the .ecore file, you have to generate the implementation classes as usual with EMF.
Example Statemachine
In the oaw4.demo.emf.statemachine.example/src folder you can find an example.statemachine file that contains a simple example state machine. You can view it as an EMF tree view after generating the EMF editors.
4/7
To generate code from it, run the example.oaw workflow file right next to it. It looks as follows: <workflow> <cartridge file="workflow.oaw"> <modelFile value="example.statemachine"/> <srcGenPath value="src-gen"/> <appProject value="oaw4.demo.emf.statemachine.example"/> <srcPath value="man-src"/> </cartridge> </workflow> As you can see it only defines a number of parameters and calls another workflow file the one in the generator project. We'll take a look at it below.
The generator
Workflow
The workflow file in oaw4.demo.emf.statemachine.generator/src has four steps:
first it reads the model from the XMI file then it verifies a number of constraints then it generates the code and finally it creates a recipes file
Constraints
A number of constraints are defined. Take a look at their definition in structure.chk to learn about the constraints check language.
5/7
Templates
In the src/templates folder you can find the code generation templates.
Recipe Creation
In src/recipe you there's an SMRecipeCreator workflow component that creates recipes for the manual implementation of the state machine.Recipe Creation
Acknowledgements
Thanks to the folks at Rohde & Schwarz Bick Mobilfunk for letting us use this example.
6/7
7/7