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.

PO Model

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:

Required Software

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.

  1. Switch to the Java perspective, if not already there.
  2. In the Package Explorer view, open the _EMF_Tutorial project and select the file po.xsd.
  3. Create a new EMF project called com.example.po.
  4. 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.

    po.genmodel

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.

  1. Change the model name and package prefix. This will simply improve the naming of some of the artifacts you generate.
  2. Disable generation of tests. By default, EMF can generate scaffolding for JUnit tests, but you won't use them in this exercise.
  3. Generate code for the purchase order model.
  4. 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.
  5. 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)

  1. Launch a new Eclipse workbench to try out your editor.
  2. In the newly launched Eclipse workbench, create a project and an instance of the purchase order model.
  3. 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.
  4. Validate the document.
  5. Initialize the purchase order by filling in its properties and creating children, so as to make the document valid.
  6. Serialize the supplier and examine the resulting document.

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.