Eclipse Modeling Framework, 2nd Edition
Example Models and Code

Last Update:­ January 7, 2008


This archive contains all of the example code and models for Eclipse Modeling Framework, Second Edition. Most of the examples in the book are based on version 2.2 of EMF. Those original examples are included in this archive, along with equivalents based on the latest release, EMF 2.4. The contents of this file can be imported into a project in your Eclipse workspace for easy access.

Contents

Installing Eclipse and EMF
Eclipse
EMF (Option 1: Eclipse Update UI)
EMF (Option 2: Manual Installation)
Importing the Examples
Using the Examples
Models
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20

Installing Eclipse and EMF

You will need to install Eclipse and EMF before you can make use of the examples. If you already know how to do this, please skip ahead to Importing the Examples.

Eclipse

You can find the latest release of Eclipse (currently 3.4.1, with 3.4.2 to be released February 25, 2009) at Eclipse Downloads. Many different packages are available; however, most of them include some portion of EMF, so we recommend downloading Eclipse Classic and then installing EMF separately as described below.


Development builds are available from the Eclipse Project Downloads page, and older releases from the Eclipse Project Archive. In either case, look for your platform's Eclipse SDK download (which is equivalent to the Eclipse Classic package) for the particular build you're interested in. Note that, even if you want to use the older version of the examples, you can still run EMF 2.2 on the latest release of the Eclipse SDK. Thus, the remainder of these instructions will assume that you are using Eclipse 3.4.


The download is simply a ZIP (such as eclipse-SDK-3.4.1-win32.zip). You can extract it anywhere and, in the resulting eclipse folder, run the eclipse or eclipse.exe (depending on your platform) executable.  When asked to select a workspace, you can accept the default or choose any other location you prefer. Eclipse should find and use your system Java Runtime Environment (JRE) by default, but if you need to specify a different one (EMF 2.3 and 2.4 require a Java 5.0 or later JRE), you can do so with the -vm command line option. For more information on the command line options supported by Eclipse, see Running Eclipse, in the Eclipse platform documentation.


Once Eclipse is installed, you have two options for installing EMF: you can use the Eclipse Update UI or manually download and unzip the EMF SDK package.

EMF (Option 1: Eclipse Update UI)

Eclipse provides a provisioning system, called p2, for managing your Eclipse install, searching for updates, and installing new functionality. This approach represents the simplest way to install EMF and keep it updated, so it recommended.

From your Eclipse workbench...


  1. Select Help > Software Updates...
  2. Switch to the Available Software tab and select the Add Site... button. Enter the following URL and select OK:
    http://download.eclipse.org/modeling/emf/updates/releases/
  3. Expand the newly added update site (it may be labeled "http://download.eclipse.org/modeling/emf/updates/releases/" or have changed to "EMF Update Site"). Select the latest 2.4 or 2.2 EMF SDK, currently "EMF SDK 2.4.1 (EMF + SDO + XSD)" or "EMF SDK 2.2.5". Older service releases of the same minor version should not appear. Click Install...
  4. When asked to confirm the items to be installed, click Next. Then, review and accept the license agreements and click Finish. Note that EMF is distributed under the terms of the Eclipse Public License, the same open source license used for all other Eclipse projects.

  5. In a few minutes (depending on the speed of your Internet connection), EMF will be installed and you will be prompted to restart Eclipse. Select Yes.

EMF (Option 2: Manual Installation)

As an alternative, EMF can be manually installed from a ZIP file. This is the traditional approach, but it is no longer recommended.

You can get EMF from the EMF Downloads page. Download the All-in-one SDK for the latest EMF 2.4 or 2.2 release (currently 2.4.1 or 2.2.5), which you'll find near the top of the page. To install, simply unzip the file into the eclipse/dropins folder in your Eclipse installation and then start Eclipse.

Note that dropins didn't exist before Eclipse 3.4. If you are using an older version of Eclipse, you'll need to unzip into the same folder as you did the Eclipse SDK ZIP. This will mix EMF's plug-ins and features in with those of the Eclipse installation, making it more difficult to uninstall.

Importing the Examples

The archive containing this readme can be imported directly into your Eclipse workspace as a project.


  1. Right-click in the Package Explorer view and select Import... from the pop-up menu.
  2. Select General > Existing Projects into Workspace and click Next.
  3. Choose Select archive file, click Browse..., locate and select the examples.zip archive, and click Open. The examples project is shown and selected. Click Finish.

The examples project is then imported into the workspace.


In addition to the two folders containing the 2.2- and 2.4-based versions of the examples, it contains a code formatter profile, emf-formatter.xml. This file describes the code formatting conventions that are used throughout the EMF code base, as well as in the included example code. It is recommended that you apply this profile so that you can easily compare the code you generate and write with the example files.


  1. Select Window > Preferences.

  2. In the Preference dialog, select Java > Code Style > Formatter. Click the Import... button.
  3. Locate and select the emf-formatter.xml file and click Open. "EMF Conventions" is shown as the active profile. Click Apply and then OK.

Using the Examples

The examples project contains two folders, emf-2.2 and emf-2.4, which contain the same example models and code, but based on EMF versions 2.2 and 2.4, respectively. In addition to incorporating the latest generator patterns, the 2.4-based version makes use of syntax introduced in Java 5.0. This is because EMF 2.3 and 2.4 require Java 5.0 or later, while previous versions run on Java 1.4.

Within each of these two folders, a models folder contains the six main purchase order models used throughout the book. The numbered folders contain example code described in the corresponding chapters of the book. In general, they include only files that need to be changed after code generation. Use them as follows:

  1. Create a project and generate code from the correct model, found under the models folder. See Chapter 4 for details on creating models and projects, and generating code.
  2. If necessary, apply any modifications to the model by copying the code found under the models directory over the generated code. See Chapter 13 for details on the required hand modifications.
  3. Apply the changes for the example by copying the code found under the numbered folder over the generated code.

Additional details and comments about the models and code follow.

Models

The models folder has the following contents.

Folder or File
 
Comments
models/

Model definitions for the main purchase order examples.

  SimplePO/


Simplest purchase order model, specified in all four forms discussed in the book. This model is used in Section 19.1 and Chapter 20.
    model/


      SimplePO.ecore

Ecore representation.
      SimplePO.mdl

Rose representation.
      SimplePO.xsd

XML Schema representation.
    src/

Annotated Java representation.
  PrimerPO/

Primer purchase order model, specified in all four forms discussed in the book. The process of using this model to generate code is detailed in Chapter 4. The model is used again in the Section 19.2.1 example.
    model/


      PrimerPO.ecore

Ecore representation.
      PrimerPO.mdl

Rose representation.
      PrimerPO.xsd

XML Schema representation.
    src/

Annotated Java representation.
  ExtendedPO/

Extended purchase order, demonstrating feature maps. Specified in all four forms discussed in the book.
    model/


      ExtendedPO.ecore

Ecore representation.
      ExtendedPO.mdl

Rose representation.
      ExtendedPO.xsd

XML Schema representation.
    src/

Annotated Java representation.
  ExtendedPO1/

First extended purchase order model, specified as a Rose model only. This model is used in the Section 19.2.2 and 19.2.3 examples.
    model/


      ExtendedPO1.mdl

Rose representation.
  ExtendedPO2/

Second extended purchase order model, specified as a Rose model only. This model is used in the examples of Section 13.6 and Chapters 14, 15, 16, and 17.
    model/


      ExtendedPO2.mdl

Rose representation.
    src/

Modifications to model code. After code is generated from the model, these source files should be copied over the corresponding generated files. See Sections 13.3-13.4 for details.
  ExtendedPO3/

Third extended purcharse order model, specified as a Rose model only. This model is used in the examples of Sections 13.7, 13.8, and 16.3.
    model/


      ExtendedPO3.mdl

Rose representation.
    src/

Modifications to model code. After code is generated from the model, these source files should be copied over the corresponding generated files.

Chapter 13

The 13 folder contains a sub-folder for each of the sections with example code.

13.6 Restricting Reference Targets

The example in Section 13.6 is based on the ExtendedPO2 model. Generate com.example.epo2 (model), com.example.epo2.edit (edit), and com.example.epo2.editor (editor) plug-ins from the ExtendedPO2.mdl Rose model. Overwrite the generated model source files with the modified versions from models/ExtendedPO2/src, as described above. Then, copy the PurchaseOrderItemProvider.java item provider implementation from within 13/13.6/com.example.epo2.edit over the generated version of the file.

13.7 Splitting the Model into Multiple Packages

The example in Section 13.7 is based on the ExtendedPO3 model. Generate the two packages of ExtendedPO3.mdl into two separate model plug-ins, com.example.epo3 and com.example.supplier, as described in Section 13.7.1, along with corresponding edit and editor plug-ins for each. Overwrite the generated model source files with the modified versions from models/ExtendedPO3/src. Then, copy the modified bundle manifest and item provider from within 13/13.7/com.example.epo3.edit over the corresponding generated files. Note that the manifest includes a dependency on the supplier model plug-in.

13.8 Editing Multiple Resources Concurrently

The example in Section 13.8 builds on the previous one, including all of the modifications made in Section 13.7. Copy the generator models for the epo3 and supplier packages, ExtendedPO3.genmodel and Supplier.genmodel, into com.example.epo3/model/ and com.example.supplier/model/, respectively, and use them to regenerate the two model plug-ins. This will enable containment proxies. Finally, replace the generated SupplierModelWizard.java file by the modified version.

Chapter 14

All of the example code in Chapter 14 is based on the ExtendedPO2 model. Generate a com.example.epo2 model plug-in from the ExtendedPO2.mdl Rose model, and overwrite the generated source files with the modified versions from models/ExtendedPO2/src. Then, copy the contents of 14/com.example.epo2/src into the generated plug-in. It contains all of the chapter's examples in a single class, which can be run stand-alone by right-clicking Metadata.java in the Package Explorer and selecting Run As > Java Application from the pop-up menu.

Chapter 15

The 15 folder includes a slightly modified version of the ExtendedPO2 model, under the model sub-folder, as well as example code for the chapter, under com.example.epo2.


The only difference in the model is that PurchaseOrder's previousOrder reference has been set to resolve proxies, as required by one of the examples in Section 15.2.5. Generate a com.example.epo2 model plug-in from this modified version of the model. Or, alternatively, if you have already generated the plug-in, you can simply modify the ExtendedPO2.ecore model to enable proxy resolution on the reference and regenerate.


Copy the contents of 15/com.example.epo2 into the generated plug-in. This includes the modifications to the model code (the same ones as in models/ExtendedPO2/src, but based on the modified version of the model used in this chapter) and two classes containing all of the example code, which can be run stand-alone. It also includes a modified bundle manifest file, META-INF/MANIFEST.MF, that adds a dependency on org.eclipse.emf.ecore.xmi, the EMF XML resource implementation plug-in, as well as a data folder containing two sample purchase order instance documents that are used in the examples.

Chapter 16

The example code in most of Chapter 16 is based on the ExtendedPO2 model, except for Section 16.3, which is based on ExtendedPO3.


Generate a com.example.epo2 model plug-in from the ExtendedPO2.mdl Rose model, and overwrite the generated source files with the modified versions from models/ExtendedPO2/src. Then, copy the contents of 16/com.example.epo2 into the generated plug-in. This includes the four classes containing the example code, a modified bundle manifest file that adds a dependency on org.eclipse.emf.ecore.xmi, and a data folder with one sample purchase order instance document used in the examples. The four example classes are for Sections 16.1 (Visitor.java), 16.2 (Adapters.java), 16.4 (CopyingObjects.java), and 16.5 (ComparingObjects.java). They can all be run stand-alone.


Generate both packages of the ExtendedPO3.mdl model into a single plug-in, com.example.epo3 (note that this is different from what was done in Chapter 13). Overwrite the generated source files with the modified versions from models/ExtendedPO3/src, and then copy the contents of 16/com.example.epo3 into the generated plug-in. This includes the CrossReferencers.java file containing the example class, which can be run stand-alone, as well as a modified bundle manifest file and two sample instance documents.

Chapter 17

All of the example code in Chapter 17 is based on the ExtendedPO2 model. Generate a com.example.epo2 model plug-in from the ExtendedPO2.mdl Rose model, and overwrite the generated source files with the modified versions from models/ExtendedPO2/src. Then, copy the contents of 17/com.example.epo2 into the generated plug-in. This includes the ChangeModel.java file containing the the example class, which can be run stand-alone, as well as a modified bundle manifest file (adding a dependcy on org.eclipse.emf.ecore.change), and the sample purchase order and change description documents that are used in the examples.

Chapter 18

The 18 folder includes modified versions of the PrimerPO model, under the model sub-folder, as well as example code for the chapter, under com.example.ppo.


The model, to which constraints and invariants have been added, is provided in three forms: Rose, XML Schema, and annotated Java. Use the Rose model, PrimerPO.mdl, to generate a com.example.ppo plug-in, along with the corresponding edit and editor plug-ins. Then, copy the code from under 18/com.example.ppo/src into the generated model plug-in. It includes the modified versions of the validator and USAddress implementation described in Section 18.2, as well as sample validation code from Section 18.3.

Chapter 19

The 19 folder contains a sub-folder for each of the chapter's sections.

19.1 Overriding Commands

The example in Section 19.1 is based on the SimplePO model. Generate a com.example.po plug-in, along with the corresponding edit and editor plug-ins, from the SimplePO.mdl Rose model. Then, copy the item provider implementation from within 19/19.1/com.example.po.edit over the generated file.

19.2.1 Suppressing Model Objects

The example in Section 19.2.1 is based on the PrimerPO model. Create a com.example.ppo model plug-in from the PrimerPO.mdl Rose model. Replace  the PrimerPO.genmodel generator model with the modified version, which disables the display of shipTo and billTo addresses as PurchaseOrder children, from 19/19.2.1/com.example.ppo/model. Generate the model, edit, and editor plug-ins, and then copy the modified source files from com.example.ppo.edit and com.example.ppo.editor over the generated versions.

19.2.2 Using List and Table Viewers

The example in Section 19.2.2 is based on the ExtendedPO1 model. Create a com.example.epo1 model plug-in from the ExtendedPO1.mdl Rose model. Replace the ExtendedPO1.genmodel generator model with the modified version, which enables notification on PurchaseOrder's customers reference, from 19/19.2.2/com.example.epo1/model. Generate the model, edit, and editor plug-ins, and then copy the modified source files from com.exaple.epo1.edit and com.example.epo1.editor over the generated versions.

19.2.3 Adding Non-Model Intermediary View Objects

The example in Section 19.2.3 builds on the previous one, including all of the modifications made in Section 19.2.2. Replace the ExtendedPO1.genmodel generator model with the modified version from 19/19.2.3/com.example.epo1/model, and regenerate the edit plug-in to make the Supplier item provider stateful. Copy the modified source files from under com.example.epo1.edit over the existing versions.

Chapter 20

The 20 folder contains just a simple Java project, SimplePO, with the stand-alone POApplication.java and a JAR of the generated model code, SimplePO.jar. Such a project can be created via File > New > Project..., by selecting Java Project on the first page of the wizard. After copying the JAR into the project, right-click SimplePO in the Package Explorer and select Properties. Choose Java Build Path and switch to the Libraries tab. Use the Add JARs... button to add SimplePO.jar, and Add External JARs... to add the three required EMF JARs from the plugins location in which they're installed. This must be done before POApplication.java will compile. Then, you can simply right-click it and select Run As > Java Application to run it. This is equivalent to the command line invocation shown in Section 20.2.1.


Good luck and happy modeling!