Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Sirius/Tutorials/DomainModelTutorial

Overview

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 used 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.

Sirius tuto4 01.png


To sum up, our metamodel has 4 concepts

  • Family
  • Person
  • Man
  • Woman

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...

Sirius tuto4 02.png

Click on Add... and fill the fields

Sirius tuto4 03.png

Click on Ok.

Sirius tuto4 04.png

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...

Sirius tuto4 05.png


In Ecore Modeling Framework select Ecore Modeling Project

Sirius tuto4 06.png


Name it org.eclipse.sirius.sample.basicfamily

Sirius tuto4 07.png


Click on Finish : Ecore Tools creates a project containing an Ecore model with an empty package and a blank diagram.

Sirius tuto4 08.png


Create the elements of the metamodel

Now you can use the palette to create the elements of the metamodel described previously.

Sirius tuto4 09.png


Use the Class tool in the palette to create Family, Person, Man and Woman. Use the properties view to set Person as abstract.

Sirius tuto4 10-2.png


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.

Sirius tuto4 29.png


Use the Composition tool in the palette to create a composition relation named members between Family and Person.

Sirius tuto4 11-2.png


Use the Bi-directional Reference tool in the palette to create the relation named children and parents from Person to Person:

Sirius tuto4 12-2.png


Use the SuperType tool in the palette to create the inheritance relations from Man and Woman to Person:

Sirius tuto4 13-2.png


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.

Sirius tuto4 42.png


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 http://www.eclipse.org/sirius/sample/basicfamily

Sirius tuto4 14.png


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).

Sirius tuto4 44.png


In the Properties View, fill the property Base Package with org.eclipse.sirius.sample.

Sirius tuto4 40.png


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


Sirius tuto4 43.png


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


Sirius tuto4 41.png

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 :

 -XX:MaxPermSize=256m 

Sirius 4mtuto 03.png

Create a new Modeling Project

Right click in Model Explorer view New->Modeling Project


Sirius tuto4 30.png


Named it org.eclipse.sirius.sample.basicfamily.sample


Sirius tuto4 31.png


On the new project created, right click on New > Other...

Sirius tuto4 18.png

In Example EMF Model Creation Wizards, select Basicfamily Model.

Sirius tuto4 19.png

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.

Sirius tuto4 32.png


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.

Sirius tuto4 33.png


With the properties view you can edit the person's name and set itsParents or Children relations to other persons in the model.

Sirius tuto4 34.png

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:

Sirius tuto4 20.gif

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:

  • Man by Sirius tuto4 25.gif
  • Woman by Sirius tuto4 26.gif
  • Family by Sirius tuto4 20.gif

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 false

Sirius tuto4 35.png

Remove too the ctool16 directory in org.eclipse.sirius.sample.basicfamily.edit for EMF does not use these icons.

Sirius tuto4 36.png

Right click on BasicFamily root model and select Generate Edit Code

Relaunch the runtime with click on the Sirius tuto4 38.png in the tabbar. You will see this new icon in the new Basicfamily model creation wizard:

Sirius tuto4 21.png

With these new icons, the editor looks like this

Sirius tuto4 28.png

Rather than

Sirius tuto4 39.png

Back to the top