In this tutorial, we have chosen to show you how EMF and loading works with the classical example of finite state machines.
Create a new simple project, call it, for example, MyFirstEMFSamples .
Select in the toolbar menu (on top of Eclipse window) File > New > Other > Example EMF Creation Wizards folder > Ecore Model
give a file name to your metamodel (e.g fsm.ecore), then click on Next button
choose EPackage as the Model Object (the root of your metamodel), and click on the Finish button
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)
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;
through this tab, you need to set 2 properties to your EPackage (which is displayed as null for the moment)
its Name : fsm, for example;
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
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 :
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)
a Transition (idem as Fsm class)
a State
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.
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)
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
don't forget to save your model
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. |
At this stage of the tutorial, you should have the following metamodel:
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:
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.
You can find more documentation about EMF at the following links :
More generally, you can find most of the documentation in the eclipse website.
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:
Omondo / Eclipse UML has a nice graphical editor for ecore models.You can even generate the EMF editor directly from this tool.
Kermeta: You can write your metamodel with kermeta and then translate it into ecore using the kermeta2ecore function.
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. |