[jboss-user] [JBoss Tools] - Manually Creating Eclipse EMF Ecore Models

Andre Dietisheim do-not-reply at jboss.com
Tue Aug 10 15:18:49 EDT 2010

Andre Dietisheim [http://community.jboss.org/people/adietisheim] modified the document:

"Manually Creating Eclipse EMF Ecore Models"

To view the document, visit: http://community.jboss.org/docs/DOC-15697

EMF provides tools for creating an EMF object model from an XML Schema (XSD).  It also provides tools for hand-crafting a model.  This document attempts to describe the process involved in the later.

+Note: All Content provided by André Dietisheim.  Thanks André !!+

Usually you model your stuff in an editor (default ecore tree editor or emfatic text editor).

h1. Create The Package
The first step is to create a package for your model. You set its name, Ns Prefix and Ns URI.
The good practice (or at least what most modelers do) is to

- *name*: a simple term (not required to be unique)
- *Ns prefix*: ~shoretened 'java package' name (not required to be unique)
- *Ns URI*: some real (or bogus) unique URI where the scheme might be found.
Usually the version is included in the uri to make sure different versions have an unique URI. The URI is needed so that the ecore runtime finds its metal-models (the classes, the type system).

I have a plugin/module in cdo called *org.eclipse.emf.cdo.ui.defs*
The ecore model for it has the following definitions:

*name*: defs
*Ns prefix*: cdo.ui.defs
*Ns URI*:  http://www.eclipse.org/emf/CDO/ui/defs/1.0.0 http://www.eclipse.org/emf/CDO/ui/defs/1.0.0

h1. Model Your Classes
You can now add classes to your package. There are usually several ways to get to the desired result, most likely the best way is to get the book or error and trial.

A very common problem is to have modeled (ecore-) classes extend Java Interfaces. For instance this could be java.lang.Comparable
The best way to achieve that is to model a class Comparable. Do not model its operation as this is just a mirror of the real java interface in the ecore-world. What differs to real ecore-classes is that you set its instance type name: java.lang.Comparable. You can now add the Comparable class to the super type of each ecore-class you want to. Modeled this way, the generator will not generate an ecore class that's called Comparable but it will include java.lang.Comparable in the interface that your ecore-classes implement.

+*Not sure I follow this example and what we're trying to illustrate.  Would example code help?*+

h1. Create a genmodel
This is mostly straight forward. Select the ecore file and create a genmodel for it.  Select your ecore file and start a new *EMF Generator Model* wizard. The wizard will allow you to create a so called Generator Model that holds all settings which are important to the code generation process.

 http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-3-4847/generate-models.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-3-4847/450-234/generate-models.png 

There are 2 settings that might be of interest to you:

'*All*' (property group when the package is selected):
- *Base Package*: the base package all ecore classes get generated to
- *Prefix*: Prefix that the factory- and package-class get

 http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-3-4848/genmodel-all-properties.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-3-4848/450-137/genmodel-all-properties.png 

example (still using the cdo.ui.defs example):

*Base Package*:  org.eclipse.emf.cdo.ui
*Prefix*: CDOUIDefs

Package-class gets CDOUIDefsPackage, Factory gets CDOUIDefsFactory, etc. All classes get generated to the package org.eclipse.emf.cdo.ui

Further modifications you might be interested in are the suffixes of the sub-packages (the defaults creates an 'impl' package where it puts all implementation classes). It can be modified by selecting the '*Package Suffixes*' and choosing the properties '*Implementation*' and '*Interfaces*'.
The naming of the implementation- and interface-classes may be changed, too. You find those settings if you select the root-node of the tree in the genmodel-editor and choose the '*Model*' property group. You'll find 'Class Name Pattern' and 'Interface Name Pattern' among the available properties. The explanations for the values show up in the statusbar (default is '*{0}impl*' and '*{0}*').

 http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-3-4849/genmodel-classname-pattern.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-3-4849/450-238/genmodel-classname-pattern.png 
Once you're done defining your generator model, you simply need to generate the implementation classes. Select the package you want to generate, right click and select the implementation you want to create. You may choose among the models, the editor, the tests.

 http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-3-4850/genmodel-generate.png  http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-3-4850/450-139/genmodel-generate.png 

h1. Modify the Generated Classes
Ecore is built to be modified, the basic usage-pattern is to code and generate hand-in-hand. To tell the generator not to override your modifications you need to set the javadoc-annotation to anything different than '@generated'. Good practice says that you should set it to '@generated NOT'. Good practice also tells you to annotate any manually added method by '@ADDED', but its optional though.
There is another handy that allows you to modify and get the generated code. If you want to have your code instead of the generated one, you just annotate accordingly and the generator will preserve your code. If you want the generated code, too, you'd need to create a method that has the original name + a suffix 'Gen'


* @generated NOT
public void setName(String name) {

* @generated
public void setNameGen(String name) {
    GENERATOR provides the generated code in here

After making your modifications, you simply need to re-generate the ecore classes. (+*How?*+)


Comment by going to Community

Create a new document in JBoss Tools at Community
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20100810/7b85738c/attachment.html 

More information about the jboss-user mailing list