| Eclipse RCP part 1: Designing the Model |
|
|
|
|
In this tutorial we will see how to use the Eclipse Modeling Framework in order to manage the application Model. If you have not already installed EMF and the related tools, please follow the steps illustraded here. The Library model we will use as a sample is taken from the official EMF Tutorial : it consists of two entities, Author and Book. - Author: has a String attribute name Ok, let's start Creating a New (File->New->Other...) Empty EMF Project
give it a name (e.g. com.rcpvision.rcptutorial.model) and click Finish. Select the model folder that the EMF wizard just created, right-click New->Other->Ecore Tools->Ecore Diagram
then press Next, change the value of the Domain file name field into Library.ecore
and press Finish. We now have a blank canvas where we can literally design our model.
so let's start by drag & drop an EClass object from the Palette on the blank page and rename it as Author
then move the cursor over the box and select the icon correspondent to the action "Add EAttribute" and rename the attribute as name then right-click the name attribute and select Show Properties View go to the Properties View, click the button right to the EType field type for instance "ES" in order to filter the prompted list and select EString as the attribute type
let's do the same for the entity Book and its String attribute title.
now, in order to add to the Book entity a reference attribute to Author, click on the EReference icon on the Palette
then click on the Book entity and release the mouse button over the Author entity
rename the reference as author
Please note that we just created a 0..1 author link from Book to Author. If now we save the .ecorediag file and open the .ecore file, we'll see this
You may prefer managing an EMF Model directly via this .ecore file editing (you can find an example here), it's just a matter of taste. For this purpose select the .ecore file, right-click New->Other...,
select EMF Generator Model
click Next button click Next button again and again now click the Load button, then Next
and Finish. In the end we obtained a Genmodel file:
from which we can quite "magically" generate the Model code, simply by right-clicking and selecting Generate Model Code
and ... here is the Model code, written out for you by EMF!
Take some time looking at the generated code and ask yourself how much time it would have taken to you to create it by hand (please consider that it embeds, among other features, a notification mechanism associated to each setter method, just to mention one). Now let's see how is EMF effective when it comes to maintain a Model, that is, when we need to change the Model. To do so, let's imagine we, all of a sudden, realize that a Book can have more than one Author! Oh, well, we need a Collection instead of a single flat attribute!
to have your Model fixed.
What are then the advantages with introduction of EMF ?
|































Comments