Learning goals of this step-by-step guideline and exercise:
- Learn how to generate code using EMF.
- Understand how to use containment / container and regular relations in EMF.
- Learn how to instantiate your models in EMF and by Java code.
Prerequisites
- You need Eclipse Modeling Tools. Installation guidelines.
Part #1: Modelling a library.
To get a modelling kickstart, we will start using an existing domain model from a library. This model consists of the following main concepts:
Do the following to get started with this example model in your Eclipse environment:
- Go into "File > New > Example... > Eclipse Modeling Framework > Extended Library Model Example"
- Select "org.eclipse.emf.examples.library" and press "Finish".
- Double click on the extlibrary.ecore file, and you will see an editor with a hierarchical breakdown of the domain model.
Part #2: Concepts and Relations
- Find examples of EClasses and EDataTypes
- Find examples of containment / container and ordinary relations in the model.
- Try to find features properties such as:
- multiplicity – lower and upper bound
- changeable – allows being set (using setter)
- derived – computed from other values
- transient – should not be stored in external resource
- volatile – should not need to be stored in memory
- order – not used in practice, but good to know if order is important
Part #3: Extending the model
Use the model editor to add a new BookCategory. Hint: right-click on BookCategory > New Child... > Select EEnum Literal
Fill out "Literal", "Name" and "Value" properties for our new category.
Remember to save our ecore file, after the new category is added.
Part #4: Generating Java Code
First we want to validate our ecore model.
Right click on the root element and select "Validate"
When the model validates successfully, we can generate Java code from it. Properties describing what code to generate is described in a separate .genmodel file. By double clicking on the extlibrary.genmodel file you will see the following in the Properties View:
To generate Java code for our library model, right click on the root element in the editor for the .genmodel file and select Generate Model Code.
When the code generation completes successfully, you will be able to see Java code in terms of Interfaces and Classes for our library example in the package explorer:
As we see in the package explorer, each EClass "Xyz" will (by default) become one interface Xyz and one implementation class XyzImpl.
By double clicking on the BookCategory.java file you see a enum implementation where Computer Science (the modification we made to the domain model) is added as a literal object:
Part #5 Instantiating from Java
Now, with Java code for our domain model automatically generated, it is time to learn how to use and instantiate the model.
Add a new Java Project to your workspace. Give it any name, for instance "My Library Test"
In order to make use of the generated code, we need specify a dependency to the generated code.
Add a java package under the src folder and give it any useful name, for instance "edu.ntnu.mycode".
Add a new java class to this package and give it a name, for instance "TestingEMF".
Add a main-method to your class. Within the main method you can play with the generated code and create objects instantiate your first library.
Note how you use the EXTLibraryFactory.eINSTANCE to create java objects.
Part #6 Instantiating from an Editor
With the ecore model we have for the library domain, we can also use EMF to generate an editor that we can use for instantiation.
In your workspace you will also see that we have two other projects - the edit and the editor projects. These are both generated from our extlibrary.genmodel file.
The edit project contains generic reusable classes for building an editor.
The editor project contains a eclipse plugin that allow you to instantiate a library model graphically.
As we made changes in the original ecore file we should regenerate the edit and the editor projects. We generate these by right clicking on the root element in the editor for our extlibrary.genmodel. Run both "Generate Edit Code" and "Generate Editor Code".
Now, to see what this editor looks like we can open the plugin.xml file in the editor project. Under the overview tab you will find a Launch an Eclipse application link.
By pressing this link, Eclipse will fire up a new Eclipse environment with the Library editor plugin added.
In order to get started modeling instances, we need a workspace project where we can place our model files. Go into "File > New > Project" and create a general project with a name, for instance, "My Library Project".
If you right click on your new project and select "New > Other > Example EMF Model Creation Wizards", you will find an entry labeled "EXTLibrary Model". Press this link and give your model file a name, for instance "MyFirstLibrary.extlibrary". Now you need to select what kind of object you want to model. Here you can select "Library". Keep UTF-8 as encoding and press "finish".
A new .extlibrary file appears now in your project and by opening it you will be able to add children and specify model properties. The things you are able to do in this generated editor is all constrained by the structure of your original ecore file.
Other Eclipse projects (like Sirius) allow you to customize and generate even cooler and more graphical editors for your domain.