Free tutorials for Java, Eclipse and Web programming



Eclipse Modeling Framework (EMF) - Tutorial

Lars Vogel

Version 1.3

27.01.2010

Revision History
Revision 0.1 - 0.412.08.2007Lars Vogel
Created, reworked and published
Revision 0.530.07.2008Lars Vogel
Updated to Eclipse 3.5, simplified article, added definition of data model.
Revision 0.601.08.2008Lars Vogel
Clean-up editor description
Revision 0.702.08.2008Lars Vogel
Moved Eclipse JET (Java Emitter Template) to own article http://www.vogella.de/articles/EclipseJET/article.html
Revision 0.825.11.2009Lars Vogel
Fixed typo
Revision 0.906.12.2009Lars Vogel
Improved reading and writing EMF models via code
Revision 1.017.01.2010Lars Vogel
added ecore tools, moved EMF models based on annotations to own article
Revision 1.120.01.2010Lars Vogel
Update chapter 5 with new model
Revision 1.221.01.2010Lars Vogel
Moved persistence and notifcation to own articles
Revision 1.327.01.2010Lars Vogel
Added infos on ecore and genmodel

Eclipse EMF

This article describes the usage of the Eclipse EMF framework for modeling your data model and creating Java code from it.

This article is based on Eclipse 3.5.


Table of Contents

1. Models and Eclipse EMF
1.1. Eclipse EMF
1.2. Ecore and Genmodel
1.3. Advantages of using EMF
2. Installation
3. Define EMF model
3.1. Create project
3.2. Create Ecore diagram
3.3. View Ecore diagram
3.4. Create EMF Generator Model
3.5. Set the package
4. Generating the domain classes
4.1. Generating Java code
4.2. Review the generated code
5. Create EMF Editor plugins
5.1. Generating edit / editor code
5.2. Run your plugins
5.3. Create your model
5.4. Edit your model
6. Using the model code
6.1. Overview
6.2. Example
7. Next steps
8. Thank you
9. Questions and Discussion
10. Links and Literature
10.1. Source Code
10.2. EMF Resources
10.3. Other Resources

1. Models and Eclipse EMF

1. Data model

A data model, sometimes also called domain model, represents the data you want to work with. For example if you write a program which is a online booking service you might model your domain model with objects like Person, Flight, Booking etc.

The general recommendation is to model your data model independent of the application logic. This approach lets usually to classes with almost no logic and a lot of properties, e.g. Person would have the properties "firstName", "lastName", "Address", etc.

1.1. Eclipse EMF

Eclipse EMF can be used to model your domain model.

In general their is a distinction between the meta-model and the actual model. The meta-model describes the structure of possible instances of the model. A model is then the instance of this meta-model.

EMF uses XMI (XML Metadata Interchange) for the model definition.

This model definition can be defined / generated via:

  • A XMI document, using an XML or text editor

  • Java annotations

  • UML

  • XML Schema

Once the EMF model is specified you can generate the corresponding Java implementations classes from this model. EMF allows that you can edit / modify the generated code by hand. Even with hand-written code you can use EMF to re-generate the code if the model changes.

1.2. Ecore and Genmodel

EMF is based on two meta-models; the Ecore and the Genmodel model.

The Ecore metamodel contains the information about the defined classes.

  • EClass: represents a class, with attributes and references * EAttribut

  • EAttribute: represents an attribute which has a name and a type

  • EReference: represents an assoziation between two classes

  • EDataType: represents the type of the attribute

The Genmodel contains additional information for the codegeneration, e.g. the path and file information. The genmodel contains also the control parameter how the coding should be generated.

The Ecore model shows a root object representing the whole model. This model has children which represents the packages, whose children represents the classes, while the children of the classes represents the attributes of these classes. EMF's meta model Ecore (roughly) corresponds to the EMOF (Essential MOF) subset of the MOF 2.0 standard.

1.3. Advantages of using EMF

EMF allows you to explicitly model your data model enhancing there fore the visibility and extensibility for the model.

EMF automatically provides notification functionality to the model in case of changes in the model happen.

Eclipse EMF helps to program against the interfaces instead of classes.

It is possible to regenerate the code from the model at any point in time.

Using the UML2 Tools project you can create an UML2 diagram out of your model. See Creating UML 2 diagrams with Eclipse UML2 Tools .