Chapter 2. Create a meta-model for Kermeta programs

In this tutorial, we have chosen to show you how EMF and loading works with the classical example of finite state machines.

2.1.  Step by step

  1. Create a new simple project, call it, for example, MyFirstEMFSamples .

  2. Select in the toolbar menu (on top of Eclipse window) File > New > Other > Example EMF Creation Wizards folder > Ecore Model

    Selection of the model object (root element)

    Figure 2.1. Selection of the model object (root element)

  3. give a file name to your metamodel (e.g fsm.ecore), then click on Next button

  4. choose EPackage as the Model Object (the root of your metamodel), and click on the Finish button

    Selection of the model object (root element)

    Figure 2.2. Selection of the model object (root element)

  5. If you have Omondo installed, you can create a meta-model using the graphical editor, but in this tutorial, we advise you to use the Sample Ecore Editor (if omondo editor was opened by default, i.e if you see the graphical editor, close it, and reopen your ecore model while right-clicking on it, then select the Open with > Sample Ecore Editor)

  6. if you don't see a tab called Properties, at the bottom (or left, or right) part of your eclipse, open it using the toolbar menuWindow > Show View > Other > Basic folder > Properties;

  7. New empty ecore model

    Figure 2.3. New empty ecore model

    How to show properties view

    Figure 2.4. How to show properties view

  1. through this tab, you need to set 2 properties to your EPackage (which is displayed as null for the moment)

    1. its Name : fsm, for example;

    2. its Ns URI : the namespace URI of the ecore model is mandatory to let Kermeta be able to load correctly its potential instances, as well as for the Dynamic creation of instances tool (see section 3.1 p.11). It is stronlgy recommended to set an absolute Eclipse URI (this kind of URI is actually relative to your Eclipse project), e.g platform:/resource/MyFirstEMFSamples/metamodels/fsm.ecore

  1. you can now add children (e.g a class (EClass), a package (EPackage), a datatype (EDataType) in your root (which is fsm EPackage in our example) by right-clicking on it and choosing the New child item. In our example, we will add three classes, which one of them will be the “root class” (model object!); this should not be mandatory, but EMF works best this way. In our example, this Fsm is the root class. So let's create :

New child on EPackage root node

Figure 2.5. New child on EPackage root node

    1. a Fsm, for which we will set the Name in the properties window (you don't need to consider the other properties for the moment)

    2. a Transition (idem as Fsm class)

    3. a State

  1. to add operations, attributes, or references on your new classes it is the same philosophy as for adding classes to an EPackage, i.e using New child on each created element.

  2. still through the Properties tab, you will have to set the EContainment property to true on each reference for which you will want to add instances. For example, in our automaton, transition and state references are containments of Fsm class. Thus, we will be able to create, in an EMF model of it, a collection of transitions and states. It will be easier to understand it once you are at the EMF model creation step (section 3, p.11)

    Setting the properties of an EReference

    Figure 2.6. Setting the properties of an EReference

  3. to add properties to the attributes, operations, and references, like the upper and lower bounds, their types (EType), you still have to use the Properties tab. The main properties to consider are : ESuperType, EType, Name, Upper Bound, Lower Bound, Containment (for the diamond-ed associations), Ordered, Unique, and EOpposite (opposite property). For the tutorial goals, you can ignore the others.

    Lower and upper bound properties : 0, 1, -1 (stands for *) are allowed

  4. don't forget to save your model

[Note]Note
For the primitive types case, like the String, Integer, Boolean, you have to create EAttribute, but not EReferences. For the other types, you must create a EReference.

2.2.  Resulting metamodel

At this stage of the tutorial, you should have the following metamodel:

a simple fsm metamodel

Figure 2.7. a simple fsm metamodel

2.3. EMF Metamodel creation tips

Creating a good metamodel is some time difficult due to limitation of the tools you use. You'll have the best experience with EMF tools and Kermeta if your metamodels follow those rules:

  1. Create an element that will contain directly or indirectly all the other elements.

    The reflexive editor and the editor generated by EMF allow to create only one root element and then, from this element, create contained element.

    This problem occurs only for model element creation from the editors. The editors correctly display models from metamodels that doesn't follow this rule if you are able to create such models by another mean. Kermeta is not affected by this constraint.

2.4.  External documentation

You can find more documentation about EMF at the following links :

More generally, you can find most of the documentation in the eclipse website.

2.5.  Alternative ways to create a metamodel

EMF tools are not the only way to create ecore metamodels. Any tool that can manipulate ecore can do the same. Here is a small list of tools that can be used to create your metamodel:

  1. Omondo / Eclipse UML has a nice graphical editor for ecore models.You can even generate the EMF editor directly from this tool.

  2. Kermeta: You can write your metamodel with kermeta and then translate it into ecore using the kermeta2ecore function.

Generate ecore metamodel from kermeta

Figure 2.8. Generate ecore metamodel from kermeta

[Tip]Tip
You can use this ecore import/export function and the Omondo editor in order to display graphically your kermeta classes. It will act as a basic manual roundtrip editor.