Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Sirius/Tutorials/DomainModelTutorial

< Sirius‎ | Tutorials
Revision as of 09:56, 8 August 2016 by Julien.dupont.obeo.fr (Talk | contribs) (Create a new Modeling Project)

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

Sirius tuto4 01.png


To sum up, our metamodel have 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

You should obtain this diagram:

Sirius tuto4 13-2.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, open the basicfamily.ecore and select the tab Model in the properties view.

For this tutorial, we have set the Ns URI with http://www.eclipse.org/sirius/sample/basicfamily

Sirius tuto4 14.png


Open the basicfamily.genmodel. This model allows EMF to generate the Java code corresponding to the metamodel.

Sirius tuto4 15.png


Right click on first model element and select Generate All

Sirius tuto4 16.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
  • The project org.eclipse.sirius.sample.basicfamily.tests


Sirius tuto4 17.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 24.png


Note: The icons for Man, Woman and Family are defined in /org.eclipse.sirisus.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

With these new icons, the editor looks like this

Sirius tuto4 28.png

Rather than

Sirius tuto4 27.png

Back to the top