vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

Create EMF Models based on annotated interfaces

Lars Vogel

Version 0.1

17.01.2010

Revision History
Revision 0.1 12.08.2007Lars Vogel
Split from EMF tutorial

Create Eclipse EMF models based on annotated interfaces

This article describes how you can create Eclipse EMF models based on annotated Java interfaces.

This article is based on Eclipse 3.5.


Table of Contents

1. Models and Eclipse EMF
2. Define EMF models via annotated interfaces
2.1. Overview
2.2. Annotations Tags
2.3. Data Model
2.4. Create EMF Meta Model
3. Thank you
4. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. EMF Resources
5.3. vogella Resources

1. Models and Eclipse EMF

For a general introduction into Eclipse EMF please see Eclipse EMF Tutorial

This article focus on the creation of EMF models based on Java interfaces.

2. Define EMF models via annotated interfaces

2.1. Overview

EMF allows to use annotated Java interfaces to define the data model. The annotations used are Javadoc annotations. Each attribute or class that is a part of the EMF model must have a @model tag and may have an optional list of additional attributes.

The @model tag may be followed by additional details about the model element. For example, if an attribute should be read-only (no generation of a set method),the following annotation needs to be added: changeable="false".

2.2. Annotations Tags

The following tags can be used in the @model definition:

Table 1. Java Annotations

AnnotationDescription
abstract="true"Creates a abstract class
containment="true"Express UML2 composition relationships (links with black diamonds). These objects references are saved with the object.
default=ValueDefault value, can be a constant, enumeration.
changeable="false"No setter will be generated

Tip

If a list is flagged with Containment="true" then elements of a list A are automatically removed from list A if the added to another list B. This is very powerful but important to know.

2.3. Data Model

We will model a webpage structure with EMF.

Create a new Java project called "de.vogella.emf.webpage". Create a new folder called "metamodel" which will contain the EMF model.

Create the following interfaces.

				
package de.vogella.emf.webpage.model;
/**
 * @model
 */
public interface Article {
	/**
	 * @model default=""
	 */
	public String getName();

}

			

				
package de.vogella.emf.webpage.model;

import java.util.List;

/**
 * @model
 */
public interface Category {
	/**
	 * @model default=""
	 */
	public String getName();

	/**
	 * @model containment="true"
	 */
	public List<Article> getArticles();
}

			

				
package de.vogella.emf.webpage.model;
import java.util.List;

/**
 * @model
 */
public interface Webpage {
	/**
	 * @model default=""
	 */
	public String getName();

	/**
	 * @model default=""
	 */
	public String getTitle();

	/**
	 * @model default=""
	 */
	public String getDescription();

	/**
	 * @model default=""
	 */
	public String getKeywords();

	/**
	 * @model containment="true"
	 */
	public List<Category> getCategories();
}


			

				
package de.vogella.emf.webpage.model;

import java.util.List;

/**
 * @model
 */
public interface MyWeb {
	/**
	 * @model default=""
	 */
	public String getName();
	/**
	 * @model default=""
	 */
	public String getTitle();
	/**
	 * @model default=""
	 */
	public String getDescription();

	/**
	 * @model default=""
	 */
	public String getKeywords();
	/**
	 * @model containment="true"
	 */
	public List<Webpage> getPages();
}

	
			

2.4. Create EMF Meta Model

Create a new EMF meta model "website.genmodel" based on these interfaces in folder "metamodel".

Select your metamodel folder, right click and select Other. Select Eclipse Modeling Framework -> EMF Generator Model

Press next and select the following.

Press next and you should be able to select your package. Change the name of the ecore model to "website.ecore".

Press finish. You have created your EMF model! We will discuss this model in the next chapter.

To use your model in an plugin environment later. Add the util package to your exported packages in MANIFEST.MF on tab Runtime.

3. 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 using flattr or donating or by recommending this tutorial to other people.

Flattr this

4. Questions and Discussion

Before posting questions, please see the vogella FAQ . If you have questions or find an error in this article please use the www.vogella.de Google Group . I have created a short list how to create good questions which might also help you. .

5. Links and Literature

5.1. Source Code

Source Code of Examples

5.2. EMF Resources

http://www.eclipse.org/modeling/emf Eclipse EMF Homepage

http://www.eclipse.org/modeling/emf/docs/ EMF Documentation

http://www.vogella.de/articles/EclipseEMF/article.html Eclipse EMF Tutorial

http://www.eclipse.org/m2m/atl/ ATL allows model to model transformation for EMF