| Java, Eclipse and Web programming Tutorials |
Version 1.3
Copyright © 2007-2010 Lars Vogel
27.01.2010
| Revision History | ||
|---|---|---|
| Revision 0.1 - 0.4 | 12.08.2007 | Lars Vogel |
| Created, reworked and published | ||
| Revision 0.5 | 30.07.2008 | Lars Vogel |
| Updated to Eclipse 3.5, simplified article, added definition of data model. | ||
| Revision 0.6 | 01.08.2008 | Lars Vogel |
| Clean-up editor description | ||
| Revision 0.7 | 02.08.2008 | Lars Vogel |
| Moved Eclipse JET (Java Emitter Template) to own article http://www.vogella.de/articles/EclipseJET/article.html | ||
| Revision 0.8 | 25.11.2009 | Lars Vogel |
| Fixed typo | ||
| Revision 0.9 | 06.12.2009 | Lars Vogel |
| Improved reading and writing EMF models via code | ||
| Revision 1.0 | 17.01.2010 | Lars Vogel |
| added ecore tools, moved EMF models based on annotations to own article | ||
| Revision 1.1 | 20.01.2010 | Lars Vogel |
| Update chapter 5 with new model | ||
| Revision 1.2 | 21.01.2010 | Lars Vogel |
| Moved persistence and notifcation to own articles | ||
| Revision 1.3 | 27.01.2010 | Lars Vogel |
| Added infos on ecore and genmodel | ||
Table of Contents
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.
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.
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.
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 .
Install EMF via the update manager of eclipse (please see Eclipse Update manager for details). Select "Modeling" and install "EMF - Eclipse Modeling Framework SDK". Also select the "Ecore Tools"; these will allow you to create UML diagrams from your model.

Create a new project "de.vogella.emf.webpage.model" via File / New / Project... / Eclipse Modeling Framework /Empty EMF project

Create a Ecore diagram via right click on the folder "model" folder and select New / Other... / Ecore Tools / Ecore Diagram

Name your "Domain File Name" "webpage.ecore".

This should open a visual editor for creating EMF models.

Open the properties view via Window -> Show View -> Other -> Properties. This view will allow you to modify the attributes of your model elements.
Click on EClass and click into the editor to create the class.
Create the classes "MyWeb", "Webpage", "Category" and "Article".

Using the EAttribute node assign to each object the atttribute "name" of String "EString".


Add the attributes "title", "description" and "keywords" to "MyWeb" and "Webpage".

Select EReferences and create an arrow similar to the following picture. Make sure the upper bound is set to "*" and that the "Is Containment" property is flagged.


Close the dialog and open the file "webpage.ecore". The result should look like the following.

Select your "webpage.ecore" -> Select File -> New -> Other -> EMF Generator model and create "webpage.genmodel" based on your "Ecore model".



Select your model and press load.


You have created two models, the Ecore and the Genmodel model.

Once your model definition is complete you can generate Java code from it. Right-click on the root node of the genmodel and select "Generate Model Code".

This will create the Java implementation of the EMF model in the current project.
The generated code will consists out of the following:
model -- Interfaces and the Factory to create the Java classes
model.impl -- Concrete implementation of the interfaces defined in model
model.util -- The AdapterFactory
The central factory has methods for creating all defined objects via createObjectName() methods.
For each attribute the generated interface and its implementation contains getter and (if allowed in the model definition) setter methods.
Each interface extends the base interface EObject. EObject is the base of every EMF class and is the EMF equivalent of java.lang.Object. EObject and its corresponding implementation class EObjectImpl provide a lightweight base class that lets the generated interfaces and classes participate in the EMF notification and persistence frameworks.
Each setter has also a generated notification to observers of the model. This mean that other object can attach them to the model and react to changes in the model.
Every generated method is tagged with @generated. If you want to manually adjust the method you want to prevent that EMF overwrites the method during the next generation run you have to remove this tag.

EMF can generate plugins which provide wizards for creating new model instances and also an editor for you which allows you to maintain your model information. For more information about Eclipse plugin development please see Eclipse Plugin Development Tutorial
Eclipse EMF allow you to create a editor for your model. Select your genmodel, right click on it and select "Create Edit Code" and then "Create Editor Code".

Two Eclipse plugin projects have been created, "de.vogella.emf.webpage.model.edit" and "de.vogella.emf.webpage.model.editor".
Select the *.editor project and run the new plugin. Selecting the "plugin.xml", select the "Overview" tab and press then "Launch an Eclipse application". See Eclipse plugin Tutorial for details.
This should start a new Eclipse runtime instance.
In new Eclipse instance create a new project "testing" and a new folder "website" ( Select this folder, right click on it, select New-> Other-> Example EMF Model Creation Wizards -> Webpage Model.

Name it "My.webpage".

Select as the Model Object "My Web" and press finish.

The generated model code looks like normal Java code and can be used like normal Java code.
The following demonstrates how you create objects based on the generated code.
Create a new plugin project "de.vogella.emf.webpage.usingmodel". Add the following dependency to your "plugin.xml".
Create the following class.
package de.vogella.emf.webpage.usingmodel;
import de.vogella.emf.webpage.model.webpage.MyWeb;
import de.vogella.emf.webpage.model.webpage.Webpage;
import de.vogella.emf.webpage.model.webpage.WebpageFactory;
import de.vogella.emf.webpage.model.webpage.impl.WebpagePackageImpl;
public class UsingEMFModel {
public static void main(String[] args) {
WebpagePackageImpl.init();
// Retrieve the default factory singleton
WebpageFactory factory = WebpageFactory .eINSTANCE;
// Create an instance of myWeb
MyWeb myWeb = factory.createMyWeb();
myWeb.setName("Hallo");
myWeb.setDescription("This is a description");
// Create a page
Webpage webpage = factory.createWebpage();
webpage.setTitle("This is a title");
// Add the page to myWeb
myWeb.getPages().add(webpage);
// and so on, and so on
// as you can see the EMF model can be (more or less) used as standard Java
}
}
Thank you for practicing with this tutorial.
Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.
I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.
http://www.vogella.de/code/codeeclipse.html Source Code of Examples
http://www.eclipse.org/modeling/emf Eclipse EMF Homepage
http://www.eclipse.org/modeling/emf/docs/ EMF Documentation
http://www.ibm.com/developerworks/opensource/library/os-ecemf1 Model with the Eclipse Modeling Framework, Part 1: Create UML models and generate code
http://www.ibm.com/developerworks/opensource/library/os-ecemf2 Model with the Eclipse Modeling Framework, Part 2: Generate code with Eclipse's Java Emitter Templates
http://www.eclipse.org/m2m/atl/ ATL allows model to model transformation for EMF