Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Eclipse Modeling Framework (EMF) - Tutorial

Lars Vogel

Version 2.0

01.01.2011

Revision History
Revision 0.1 - 0.412.08.2007 - 13.09.2007Lars Vogel
Created
Revision 0.5 - 1.930.07.2008 - 01.01.2011Lars Vogel
bug fixes and enhancements

Eclipse EMF

This tutorial describes the usage of the Eclipse EMF framework for modeling your data model and creating Java code from it. This tutorial is based on Eclipse 3.6 (Helios).


Table of Contents

1. Models and Eclipse EMF
1.1. Data model
1.2. Eclipse EMF
1.3. Meta Models - Ecore and Genmodel
1.4. 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. Creating JavaDoc
8. Generating methods
9. Extending an EMF Ecore model (inheritance)
9.1. Overview
9.2. Example
10. Setting the empty string as default value
11. Next steps
12. Thank you
13. Questions and Discussion
14. Links and Literature
14.1. Source Code
14.2. EMF Resources
14.3. vogella Resources

1. Models and Eclipse EMF

1.1. Data model

A data model, sometimes also called domain model, represents the data you want to work with. For example if you develop an online flight booking application 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 leads to classes with almost no logic and a lot of properties, e.g. Person would have the properties "firstName", "lastName", "Address", etc.

1.2. Eclipse EMF

Eclipse EMF can be used to model your domain model. EMF has a distinction between the meta-model and the actual model. The meta-model describes the structure of the model. A model is then the instance of this meta-model. EMF provides a plugable framework to store the model information, the default uses XMI (XML Metadata Interchange) to persists the model definition.

EMF allows to create the meta-model via different means, e.g. XMI, Java annotations, UML or a XML Schema. The following description will use the EMF tools directly to create a EMF model.

Once the EMF meta-model is specified you can generate the corresponding Java implementations classes from this model. EMF provides the possibility that the generated code can be safely extended by hand.

1.3. Meta Models - Ecore and Genmodel

We said earlier that EMF has a meta-model. Actually EMF is based on two meta-models; the Ecore and the Genmodel model. The Ecore metamodel contains the information about the defined classes. 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 allows to define different elements.

  • EClass : represents a class, with zero or more attributes and zero or more references.

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

  • EReference : represents one end of an association between two classes. It has flag to indicate if it represent a containment and a reference class to which it points.

  • EDataType : represents the type of an attribute, e.g. int, float or java.util.Date

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.

1.4. Advantages of using EMF

With EMF you make your domain model explicit which helps to provide clear visibility of the model. EMF also provides change notification functionality to the model in case of model changes. EMF will generate interfaces and factory to create your objects; therefore it helps you to keep your application clean from the individual implementaiton classes.

Another advantages is that you can regenerate the Java code from the model at any point in time.