Framework of the week: Eclipse EMF

I’m starting a serie of blog post describing a framework each week, so enjoy reading!

The first framework i’ll speak about is Eclipse EMF.

Purpose


EMF aims to bridge the gap between raw code and modelized one (generated from a data model model representation).
It’s a very visual technology because you manipulate models (trees or diagrams) to define your future Java code (I think that my next blogposts, for example on Maven will be far less sexy).
So graphically defining a metamodel using the ecore meta-meta model (the domain object you’ll manipulate, or your classes if you prefer) you’ll be then able to round trip between Java code generation and customization.

Concepts


Model


EMF modelization is based on multiple concepts:

  • The ResourceSet: this is a set of Resources
  • Resource: a resource is a set of coherent model objects and packages: formerly an xmi (xml) file containing the definition of your model
  • Package: a set of model objects
  • Model Object: formely a Java class: can have references to other model objects, have attributes, visibility: it’s contained by a resource or an other model object
  • Containment: As EMF sees the world as an xmi, a model object MUST be contained by an other or the resource

Generated code


The generated code is done in a clean way, you’ll be able to generate 3 eclipse plugins starting from a model definition:

The model plugin

The model plugin contains your domains classes.
These classes are generated as a kind of Java beans, sending notifications each time a change is done on the object instance.
The model plugin also contains a factory to create object instances and a Package class referencing all the metamodel classes, references and attributes.

The edit plugin

This plugin contains adapters that will give the Images, getText, property source content, etc of model classes

The editor plugin

It will contain a basic Eclipse tree editor and menus to be able to manipulate your domain objects

Getting started

In order to start, you must have an eclipse ide with EMF installed.
Then, you can create a new EMF project:
new emf project context menu

new emf wizard

You can now create a new ecore file on the ‘model’ folder (your model objects definition)

ecore file wizard

ecore file context menu

Defining metaclasses

We’re going to define two or three metaclasses: a parent one, and two children ones. Don’t forget to put the containment relation from the parent to the children.
Capture d’écran 2013-12-08 à 18.50.58

So now, we’re done with our model definition, we’re are now going to define a generator model (it will use our metaclasses definition file and gen the appropriate java classe:
context menu genmodel

genmodel ecore reference

Right click on your genmodel root node, and generate model, edit and editor code.

Running

In order to test the result, we’ve to create a new Eclipse run configuration:

eclipse run configuration context menu
Eclipse run configuration

Running the configuration, you can now create an empty project and a Todo model to create your objects!
Todolist

Todo model

Useful links


vogella
EMF do’s and don’ts
EMF reference book

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *