EMF-OCL Documents
Eclipse Modeling Framework & Objects Constraint Language Documents
|
Generating an EMF 1.1 Model
Generating an EMF Model with Object Constraint Language
This tutorial is a step-by-step description of the process of
creating an EMF model with additional constraints and generating a simple model editor for it.
Following this tutorial will show how easy EMF makes it to go from
a simple model definition to a fully functioning editor for that
model.
The screenshots are based on version 2.1 of the Eclipse SDK and
version 1.0 of EMF-OCL plugin
The model we will generate looks like this in UML :
|
context Book inv: pages>0
context Book inv: library.writers->includes(author)
|
We're going to show how an EMF-OCL model can be generated from an UML diagram (+ constraints)
Contents
contents
Step 0: Prerequisites
After installing the plugin package, verify that it
is available in your Eclipse environment:
- Bring up the "Help/About Eclipse Platform" dialog.

- Click on "Plug-in Details".

- Check that the highlighted set of plugins are present.

contents
Step 1a: Make the Model
We're going to build the Ecore model using the eclipse interface. (library.ecore)
- Bring up the "File/New/Project..." dialog.

- Select "Eclipse Modeling Framework", choose "EMF Project" and click on "Next"

- Enter the Project name and click on "Next"

- Choose "Create empty project" and click on "Finish"

contents
Step 1b: Create an Ecore Model
Right-click on "Library/src" and Bring up the "New/Other..." dialog

Select "Example EMF Model Creation Wizards", choose "Ecore Model" and click on "Next"

Enter the File name and click on "Next"

Choose the Model Object "EPackage" and click on "Finish"

Give a name to your EPackage. Here it is "Library".

Add a class to your model. Here it is "Library".

Change the name of the class. (use the "help/Show View/Other/Basic/Properties" dialog if you don't have the window
with properties)

Do the same for other classes.
You should have the following classes: (Library,Book and Writer)

Add an EEnum for the different types of books and give a name


Add an EEnum Literal by type of book, give a name and a value by EEnum Literal


Global view of Library.Ecore at this step:

Create the EData Type (Int, String for example) and put the correct OCLtype


Add EAttributes and change their properties (name, bounds, Etype, ...)


Add the different EReferences and change their properties:


contents
Step 1c: Add the OCL Constraints
- Add EConstraints :

- Add the different properties and clic on the expression button

- Enter the OCL expression and click on the validate button

return on the Constraint tab and click on OK
- Do the same for the second constraint

contents
Step 2: Generate the EMF Model Code
Create the GenModel:
- In the Package Explorer view, right-click the "src" folder and
select "New/Other..." from the pop-up menu.

- Select "Eclipse Modeling Framework" and "EMF Models". Click the
"Next" button.

- Change the folder to "library/src/model", Change the file name to "library.genmodel" and click the "Next"
button.

- Select "Load from EMF core model" and click the "Next"
button.

- Click on "Browse File System", select your model (library.ecore), click the "Load" button and click on "Next".

- Select library package and click the "Finish" button.

- An Ecore model and a GenModel will be created. The GenModel,
which controls code generation for the model, is opened in the main
view.

contents
Step 3: Generate an Editor for the Model
A fully functional Eclipse editor can also be generated for any
model. By default, it is split between two plugins: an "edit"
plugin includes adapters that provide a structured view and perform
command-based editing of the model objects; an "editor" plugin
provides the UI for the editor and wizard.
- In the GenModel, right-click the "Library" model object and
select "Generate Edit Code" from the pop-up menu.

- Right-click the model object and select "Generate Editor Code"
from the pop-up menu.

- Observe the generated projects, with "edit" and "editor"
suffixes.

In general, if you wish to generate the model, edit, and editor
plugins in a single step, you can do so by selecting "Generate All"
from the pop-up menu.
The code should be compiled automatically as it is generated,
and should recompile whenever it is changed. If you have disabled
automatic building in the workbench preferences, you can initiate
compilation manually:
- Select "Rebuild All" from the "Project" menu.

- There should no errors in the "library", "library.edit", and
"library.editor" projects if you import a correct core model.

contents
Step 4: Run the Generated Editor
In order to run plugins in a runtime workbench, a lunch
configuration must first be set up:
- Select "Run..." from the "Run" menu.

- Select "Run-time Workbench" and click the "New" button.

- You may want to change the configuration's name to something
more recognizable. Then, switch to the "Plug-ins and Fragments"
tab.

- Pick "Choose plugin-ins and fragments to launch from the list",
and then select the top-level "External Plug-ins".

- Switch to the "Common" tab, select "Display in favorites menu:
Run", and click the "Apply" button.

A runtime workbench can be launched from the "Launch
Configurations" dialog by clicking on the "Run" button. Once the
launch configuration has been set up, it can also be launched from
the "Run" button on the toolbar.
- Select the new launch configuration from the "Run" button
drop-down.

- Wait for a second instance of the Eclipse platform to come up.
Bring up the "Help/About Eclipse Platform" dialog, click on the
"Plug-in Details" button, and verify that the generated plugins are
there.

The library model wizard can now be used to create a new
instance of the model.