Exercise 1: Code Generation
Scenario
You will be building tools to work with purchase orders. Your company has existing shipping and billing systems that
you need to integrate with. The basis for this integration will be an XML Schema for purchase orders,
po.xsd, which is already used by the shipping system.
In this exercise, you'll use EMF to quickly generate an implementation of the model and an editor from the schema.
Once the code has been generated, you will explore the editor, using it to create an instance of the model and to
save it as a conforming XML document.
The purchase order model in this exercise is an extension of the example used throughout the presentation and is
basically equivalent to the following UML class diagram.

Later, in Exercise 2, you will reuse the model to serialize and transmit purchase orders to the shipping system
according to the schema. The Supplier class is not actually relevant to that, but will provide a
container for PurchaseOrders in the editor in this exercise.
Objectives
At the end of the exercise you should be able to:
- Import a model into Eclipse using EMF
- Generate an implementation of the model and an editor based on it
- Use the generated editor to create an instance of the model
Required Software
- Java 5.0 or later
- Eclipse 3.3 or later
- EMF 2.3 or later
The screen captures in this document are based on Eclipse 3.4 M5 and EMF 2.4 M5.
Additional Resources
Though not required to complete this exercise, additional information about XML Schema is available from the W3C's
XML Schema Recommendation: The Primer provides a
relatively gentle introduction, and Part 1 and
Part 2 have all the details of the
specification.
Directions
This exercise is carried out entirely using the Eclipse Software Development Kit (SDK) with the Eclipse Modeling
Framework (EMF) SDK installed into it. These instructions refer to this product as either Eclipse or the
workbench.
These instructions are located in your workspace, in the _EMF_Tutorial project. If you look
there, you should find an additional file: po.xsd. This is the schema that
describes the purchase order model. You can open the schema in a text editor or the Sample XML Schema Editor to see
how it presents the same information as the class diagram above.
The full solution to this exercise is available in the Solutions folder. That folder contains the
three complete projects that you will generate in the exercise.
Part 1: Import the Model
There are three sources for importing a model into EMF that are supported out of the box: XML Schema (XSD), annotated
Java and Rational Rose UML. You can create an Ecore model and generate code from any of these, but in this exercise
you will use a schema.
- Switch to the Java perspective, if not already there.
- Select Window -> Open Perspective -> Java.
- In the Package Explorer view, open the _EMF_Tutorial project and select
the file po.xsd.
- Create a new EMF project called com.example.po.
- Select File -> New -> Project...
- Expand the twisty next to Eclipse Modeling Framework and select
EMF Project. Click the Next button.
- Name the project com.example.po and click Next.
- Select XML Schema. Click Next.
- Click Load to load the pre-filled schema file, which was based on the selection. Click
Next.
- Because the model is specified in one schema with a single target namespace, its Ecore representation is a
single package. Notice that this package, com.example.po, has been selected. Click
Finish.
- The generator model, po.genmodel, is opened in the EMF generator. Expand it and select
various items to see how the model was imported from the schema.

Part 2: Generate the Code
The generator model decorates the Ecore model created from the schema, adding information that is only relevant to
code generation. It provides many ways to customize the code that EMF will generate.
- Change the model name and package prefix. This will simply improve the naming of some of the artifacts you
generate.
- Select the root node, named Po, right-click, and select
Show Properties View (if this view is not already shown).
- Scroll in the Properties view to find the All twisty. Select the
Model Name property under it, and change its value to Purchase Order.
This property determines the names of the generated plug-ins. Notice also that the model node's label is
updated in the tree.
- Select the node under the root, the package, which is still named Po.
- In the Properties view, find All and select the
Prefix property from underneath it. Change its value to PO (make both
characters uppercase). This prefix is used in the names of package-level classes and interfaces, such as the
factory, pcakage, and validator. Again, the node's label is updated in the tree.
- Disable generation of tests. By default, EMF can generate scaffolding for JUnit tests, but you won't use them in
this exercise.
- Select the root node in the generator, now named Purchase Order.
- In the Properties view, find Tests and select the
Test Directory property from underneath it. Clear its value (hit backspace).
- Generate code for the purchase order model.
- Ensure that the root Purchase Order node, which represents the whole model, is selected.
Otherwise, only the artifacts relating to the selected model element will be generated. This can be helpful in
the future when regenerating just a single changed class, which is faster than regenerating everything.
- Right-click and select Generate All. Notice that Generate Test Code is
disabled because of the property you changed in step 2.

- The full implementation of the model is generated in the com.example.po project. In
addition, two new projects are created: com.example.po.edit contains edit support that is
not dependent on Eclipse, and com.example.po.editor is an integrated Eclipse editor.
- 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 by selecting
Project -> Build All.
- Save the generator model. This will retain the changes you made in case you need to regenerate the code in the
future.
Depending on your plug-in development compiler preferences, if you're using very recent builds of Eclipse and EMF,
you may see three warnings about the use of the deprecated Eclipse-LazyStart header in the
generated MANIFEST.MF files. You may use the suggested quick fix or simply ignore the
warnings.
Part 3: Create a Supplier (Run the Generated Code)
- Launch a new Eclipse workbench to try out your editor.
- Select your com.example.po project in the Package Explorer view.
- Select Run As -> Eclipse Application from the Run toolbar drop-down
or menu. Note that the Run menu is context-sensitive, so options available under
Run As will change depending on what you have selected.
- Once the second workbench opens, close or minimize the Welcome view in it.
- Select Help -> About Eclipse SDK, click Plug-in Details,
and look for the generated Purchase Order plug-ins, com.example.po,
com.example.po.edit, and com.example.po.editor. You may find them
more easily if you click on the Plug-in Id column header to re-sort.
- In the newly launched Eclipse workbench, create a project and an instance of the purchase order model.
- Select File -> New -> Project...
- Expand General and select Project. Click the Next
button.
- Give the project a name, such as po, and click Finish.
- In the Package Explorer view, right-click the project and select
New -> Other... from the pop-up menu.
- Expand Example EMF Model Creation Wizards and select PO Model. Click
Next.
- Enter a file name for the purchase order, such as supplier.po. Make sure it ends with
a .po extension. Then, click Next.
- Select Supplier as the model object and UTF-8 as the XML encoding.
Click Finish. The newly created supplier is opened in an editor.
- The root node in the editor corresponds to the supplier.po resource. Under it lies a
DocumentRoot, which acts as a container for the single Supplier object
in the document. Use the editor to initialize the supplier and create a purchase order.
- Expand the platform:/resource/po/supplier.po resource and the
Document Root, and select the Supplier object.
- If the Properties view isn't already showing, right-click the Supplier
object and select Show Properties View from the pop-up menu.
- In the Properties view, select the Name property, and provide a name
for the supplier. The label in the editor is updated.
- Right-click the supplier and select New Child -> Purchase Order from the pop-up menu.
A new order is added to the supplier.

- Validate the document.
- Right-click the Document Root and select Validate from the pop-up
menu.
- A dialog shows that the validation was unsuccessful. Click the Details button to see the
problems found.

- Initialize the purchase order by filling in its properties and creating children, so as to make the document
valid.
- You will need to create a Bill To US Address, a Credit Card, and at
least one Item, as well as initialize those objects.
- The correct synatx for an order or ship date is yyyy-mm-dd. For an expiry date,
yyyy-mm is correct.
These are standard XML date formats.
- An item's part number consists of three digits, a dash, and two uppercase letters, as defined by the regular
expression \d{3}-[A-Z]{2}.
- Ensure the document validates successfully before continuing.
- Serialize the supplier and examine the resulting document.
- Select File -> Save to save the supplier.
- In the Package Explorer view, right-click the new supplier.po file,
and select Open With -> Text Editor to view the XML serialization. Notice that the
document conforms to the original po.xsd schema.
Summary
You generated a model and editor from an XML Schema document, and then used the editor to create, validate, and save
an instance of that model. You'll use the model, the editor, and the instance document again in Exercise 2. If you
didn't have time to fully initialize the document, you can copy one from _EMF_Tutorial/supplier.po
in the first, development workbench.