In this post I will explain you how to make changes to the .ecore and .genmodel files, which we created in the previous post and then generate Java classes, which are used by the code generators to read the content of a given .xml file and generate code from that content. Changes to the Ecore models include updates in the naming of the components, adding new or removing existing components and so on. In my practice I had to change the Ecore models countless times, so I believe that this post will be useful for all of you.
Updating the Ecore Model
The Ecore model is found in the model folder of the project. You can open it by double clicking. It should appear on the right side. Once you expand it, you should see the following:
If by any chance you don’t get that view and instead you see some other view or a textual representation, you should right click on the model .ecore file, select Open With and from the options choose Sample Ecore Model Editor. As you can see there are several editors for the Ecore model. You have the possibility to open it as a text document and do the changes right there. This could be helpful if you are merging the data from some other file.
In the Model Editor you have the options to add new components to the Ecore model, remove existing ones or change their properties. In the upcoming post I will explain more about the type of children, which could be added to the Ecore Model and their properties, which could be modified.
You can right click on the root node or any other child node inside the Ecore Model and add child or sibling component from the menu. There is a window under the Editor, where the properties reside. You can change them from that window.
Updating the .genmodel
Once you do a change in the Ecore model, that change should be reflected to the .genmodel as well. Usually this is done automatically but I have seen cases when the automatic updates fails. In such cases you should go ahead and update it manually.
First of all open the .genmodel file, expand all components and verify whether your changes were reflected or not. That is something I strongly recommend to do every time you update the Ecore model.
If your changes are not available in the .genmodel you need to right click on the .genmodel file and then select Reload
From the window that pops up select the Ecore model option and hit Next. Usually it will give you directly the path to your Ecore model file. In case it is not available in the field, select it and then press Next.
In the next window make sure that your Ecore model is selected and press Finish. Your .genmodel should be updated. Expand all of its component and verify that your changes are available there.
Generate Java classes
In order to generate the classes, open the .genmodel file by double clicking on it. Right click the root node and select Generate Model Code
This will generate the Java classes together with some other utilities in the src folder of the project. The first package contains the interfaces of the model. The .impl package contains the implementations of those interfaces. The .util package contains some additional implementations like factories.
That’s it. The required Java files are generated. From here on we will be using them.
You have the ability to modify the generated files and retain your changes the next time you regenerate those files. You can update the implementation of a given method. If you open one file, for example TranslationsTypeImpl.java (or any other) you will see that in the comment section of each method there is one directive – @generated. In order to retain the changes you did in a particular method, you should remove that directive. This will tell the generator that the method is implemented by the user and it will not overwrite it.
The methods that are implemented in the generated classes are used for example when reading and parsing the .xml file. You can modify the way your .xml file is parsed. For example to add an extra logic to parse a particular string differently. In my experience I had to update couple of generated methods because the .xml files I was working on had issues so parsing them was giving errors. If your .xml files are OK (as they are supposed to be) you may never have the necessity to update the generated Java classes.
In this post we saw how to make changes in the .ecore files. Changes like adding and removing child components, changing their properties and so on. The we saw how to update the .genmodel after making changes to the Ecore model and lastly generate Java classes from the .genmodel. In the next post we will introduce the elements which we can add to a .ecore model and their properties.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
Passionate developer, loving husband and caring father. As an introvert programming is my life. I work as a senior software engineer in a big international company. My hobbies include playing computer games (mostly World of Warcraft), watching TV series and hiking.