Previous Tutorial: Creating a GEF editor – Part 1: Defining the Model
The eclipse EMF framework provides tools for the creation of structured data models and the generation of code based on these models. In this tutorial we generate the model and edit code for the model we created on the previous tutorial.
Just a short reminder of the status of the project:
We have 5 EClasses (ObjectProcessDiagram, OPMThing, OPMObject, OPMProcess and OPMLink), some attributes and links between the EClasses. We are now ready for code generation, using the EMF Generator Model.
- Right click on the model directory on your project, select “New”->”Other…”. The new file wizard will open. Select the “EMF Generator Model” file kind and click “Next”:
- I will name my file “opm.genmodel” (note that the filename must end in .genmodel). Click next once again (and forgive me for not adding another screenshot).
- Now it’s time to select from where the model is loaded. An EMF Model Generator file is kind of a “decorator” for a model, which provides extra information to enable code generation of the model. Since we just created and Ecore model in the previous tutorial, we will be using it. You can also create a model using comment annotations in java interfaces (a good tutorial can be found here), which is actually how I started in the first place, but since the annotations are in the comments (and not real java annotations) you have to remember all the syntax, there is no error checking when editing, and after some playing with the Ecore model editor I got pretty used to it. So I’ll click on “Ecore model” in this screen and click “Next”:
- Since we have our model file in a project in the workspace, select “Browse Workspace” and find the Ecore model find you want. It should appear in the model URI textbox. You should be able to click on “Load” and nothing bad should happen. Click “Next” once again.
- Leave the next wizard page with no changes and click “Finish”. This page lets you select which packages to generate and reference from other generator models, but since I really don’t know what this means, and in any way we only have one package to model, just ignore.
- Voila! we have our EMF Generator Model:
Take some time to inspect it. It looks just like our Ecore model, but the properties window suddenly has a lot more lines! This is where all of the information for code generation is stored. We will be editing it a bit and also checking that the generated values are correct. Note that I am not really an expert on EMF, and if you want to understand all of the details you should probably read the EMF book.
- Lets define the properties of the Generation Model.
- Root properties: changed the Model Name to OPM which better matches the name I want to give my models.
- Package properties: changed the prefix (which provides a string prefix for some utility generated classes) to OPM.
This should be all we need for now. Right click on the root of the genmodel and select “Generate All” (there is no real need to generate all, but it is nice to use generated editor and test if the model works as expected).
- That is basically it. This is how my environment looks now:
If you want to test your model, you can run a model editor by clicking on the execute button (the green one with the white triangle). Another possibility if this does not work is to right-click on the “com.vainolo.phd.opm.model.edit” project, and select “Run As->Eclipse Application”. Create a new project: “File”->”New”->”Project” and select “General”->”Project”:
Click next and name your project as you like (I named mine “test”. Very original). Now we’ll create a new OPM model file! Right click on the project and select “New”->”Other…”. The wizard now also contains “OPM Model” as one possible option!
Click “Next” and give your model a nice name, for example “TheBestOPMModel.opm”. Click “Next”. You now have to select which model class to create as the root of your model’s hierarchy, so select the ObjectProcessDiagram, which is the container of all other EClasses in our model.
Next Tutorial: Creating a GEF Editor – Part 3: Basic GEF Editor