- 1 Overview
- 2 Create the Metamodel
- 3 Generate the source code of the Metamodel
- 4 Test the metamodel
This tutorial explains how to create your first EMF Metamodel. It can be seen as the early step before the Starter Tutorial.
The instructions describe the creation of the BasicFamily metamodel. This metamodel defines 4 concepts that can be use to describe a family:
- The first concept is Family. A family has a name of type Estring.
- A family contains members of type Person. A person has a name of type EString.
- A person has 2 parents and many or no children.
- Two concepts herited from Person are Man and Woman.
- A person can have one father of type Man and/or one mother of type Woman.
To sum up, our metamodel have 4 concepts
and 6 relations
- members from Family to Person
- children from Person to Person, with cardinality 0..*
- parents from Person to Person with cardinality 0..2
- father from Person to Man with cardinality 0..1
- mother from Person to Woman with cardinality 0..1
- an extend from Person for Man and Woman
Create the Metamodel
The easiest way to define a metamodel is to use Ecore Tools (based on EMF and Sirius), a graphical editor for Ecore models.
Install Ecore Tools
If you are using Obeo Designer or Eclipse Modeling packages, Ecore Tools is already installed.
Otherwise, you can find it here: EcoreTools site
To install it, click on Help/Install New Software...
Click on Add... and fill the fields
- Name: Ecore Tools
- Location: One of the update sites listed here EcoreTools UpdateSite
Click on Ok.
Finally, Click on Next > and follow the instructions to complete the installation.
Create an Ecore Modeling Project
In the Model Explorer View, right click on New->Other...
In Ecore Modeling Framework select Ecore Modeling Project
Click on Finish : Ecore Tools creates a project containing an Ecore model with an empty package and a blank diagram.
Create the elements of the metamodel
Now you can use the palette to create the elements of the metamodel described previously.
Use the Class tool in the palette to create Family, Person, Man and Woman. Use the properties view to set Person as abstract.
Note that you can edit the name of a model element (class, attribute, relation) directly from the diagram: just click on a selected element to activate the label edition (let some time before the selection and the click to avoid triggering a double-clik).
Use the Attribute tool in the palette, or the popup, to create an attribute named name on Family and Person.
Use the Composition tool in the palette to create a composition relation named members between Family and Person.
Use the Bi-directional Reference tool in the palette to create the relation named children and parents from Person to Person:
Use the SuperType tool in the palette to create the inheritance relations from Man and Woman to Person:
Use the Reference tool in the palette to create the two relations named father and mother from Person to Man and Woman. Check the derived property of these references in the Properties View.
More information about Ecore Tools modeler
Best practices on how to create a metamodel with Ecore:
Generate the source code of the Metamodel
By default, the Ns URI of the ecore file is set with
http://www.example.org/basicfamily. This value is used to reference the metamodel in your Eclipse environment.
To change this value, click on the diagram's background (it will show the properties of the package basicfamily) and set the Ns URI with
In the Model Explorer, open the file basicfamily.genmodel and select the package basicfamily (this genmodel file contains a model that allows EMF to generate the Java code corresponding to the metamodel).
In the Properties View, fill the property Base Package with
This value forces the generation of the source code into a package named org.eclipse.sirius.sample.basicfamily.
Right-click on the class diagram and select Generate > All
The "Generate All" produces these elements :
- The folder src in org.eclipse.sirius.sample.basicfamily
- The file MANIFEST.MF in the META-INF directory
- The project org.eclipse.sirius.sample.basicfamily.edit
- The project org.eclipse.sirius.sample.basicfamily.editor
- The project org.eclipse.sirius.sample.basicfamily.tests
Test the metamodel
Launch a runtime to use the Basicfamily concepts.
Launch a new runtime from your Eclipse
To launch a new eclipse application click on Run / Run Configurations and double click on Eclipse Application to get a New_configuration.
If you are not running with java 8, in order to comfortably run Sirius in your new runtime, you should add this option in your VM arguments :
Create a new Modeling Project
Right click in Model Explorer view New->Modeling Project
On the new project created, right click on New > Other...
In Example EMF Model Creation Wizards, select Basicfamily Model.
Click on Next> button to create a Basicfamily model.
Select Family as the type of the root object in your new model and click on Finish.
EMF has created a new model and automatically opened the default tree editor.
To add elements to this model, right click on the Family element and select Man or Woman menu.
With the properties view you can edit the person's name and set itsParents or Children relations to other persons in the model.
Change default EMF icons
Note: As you can see, the Basicfamily Model tool in the tree has the default EMF icon (star on a blank file).
To change this icon, just replace BasicfamilyModelFile.gif in /org.eclipse.sirius.sample.basicfamily.editor/icons/full/obj16/ by your own specific icon:
Note: The icons for Man, Woman and Family are defined in /org.eclipse.sirius.sample.basicfamily.edit/icons/full/obj16.
We have replaced the default ones generated by EMF:
To have the same icons for the elements creation (man or woman), you should modify the basicfamily.genmodel.
Open it, in properties view, update the Creation Icons value to
Remove too the ctool16 directory in org.eclipse.sirius.sample.basicfamily.edit for EMF does not use these icons.
Right click on BasicFamily root model and select Generate Edit Code
With these new icons, the editor looks like this