RSS

Search Engine

Showing posts with label Eclipse Modeling Framework (EMF) - Tutorial. Show all posts
Showing posts with label Eclipse Modeling Framework (EMF) - Tutorial. Show all posts

Thursday, July 22, 2010

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 uses XMI (XML Metadata Interchange) to persists the model definition.

This EMF meta-model definition can be defined based on:

  • A XMI document, using an XML or text editor

  • Java annotations

  • UML

  • XML Schema

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

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

Installation

Install EMF via the Eclipse update manager . 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.

Define EMF model

3.1. Create project

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

3.2. Create Ecore diagram

Select the folder "model". 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.

3.3. View Ecore diagram

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

Tip

Right click on any element and select "Show Properties View" to see the additional attributes of the node.

3.4. Create EMF Generator Model

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.

3.5. Set the package

Open "webpage.genmodel" and select the node "Webpage". Set the "base package" property to "sawan.modi.emf.webpage.model".

Generating the domain classes

4.1. Generating Java code

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.

4.2. Review the generated code

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.

Create EMF Editor plugins

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

5.1. Generating edit / editor code

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, "sawan.modi.emf.webpage.model.edit" and "sawan.modi.emf.webpage.model.editor".

5.2. Run your plugins

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 Tutorials for details.

This should start a new Eclipse runtime instance.

5.3. Create your model

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.

5.4. Edit your model

You should now see a editor for your website.model.

Right click on "My Web" and create a new elements. To edit the elements use the "Properties View" which can be found under Window -> Show View -> Properties.

Save your created model.

Using the model code

6.1. Overview

The generated model code is standard Java code and can be used as such. The following demonstrates how you create objects based on the generated code.

6.2. Example

Create a new plugin project "sawan.modi.emf.webpage.usingmodel". Add the following dependency to your "plugin.xml".

  • org.eclipse.emf.ecore
  • sawan.modi.emf.webpage.model

Create the following class.

   
package sawan.modi.emf.webpage.usingmodel;

import sawan.modi.emf.webpage.model.webpage.MyWeb;
import sawan.modi.emf.webpage.model.webpage.Webpage;
import sawan.modi.emf.webpage.model.webpage.WebpageFactory;
import sawan.modi.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


}
}

Extending an EMF Ecore model (inheritance)

7.1. Overview

EMF allows to define base models and extend them. The following will demonstrate this. It will also demonstrate how to work with EMF ecore models directly without using the ecore tools.

7.2. Example

The New Project of EMF.

Create a new EMF project "sawan.modi.emf.inheritance". Create a new model by selecting File -> New -> "Eclipse Modeling Framework" -> "Ecore Model". Name the model "base.ecore".

Select "EPackage" as the basis and maintain the following properties for this package.

Right click on the package and select New Child -> EClass. Maintain the class "MyBaseClass" with two "EAttributes" of type "EString".

Create a new "Ecore" model "extendedmodel.ecore". Maintain "extended" as the package name. Right-click your model and select "Load resource".

Create a new class "MyExtendedClass" and press "ESuperType".

Add your "MyBaseClass".

Maintain a new EAtribute "detailedField" on "MyExtendedClass".

Create a new genmodel "extended.genmodel" based on extended.ecore. Generated Java code and you will see that the "MyExtendedClass" has extended "MyBaseClass".

Thank you

Thank you for practicing with this tutorial.

I maintain this tutorial in my private time. If you like the information please help me by donating or by recommending this tutorial to other people.