Generating an EMF 1.1 Model using XML Schema

This tutorial provides a step-by-step description for creating an EMF model from an XML schema and then generating a simple model editor for it. Completing this tutorial will enable you to see how easy EMF makes it to go from a simple model definition in an XML schema to a fully-functioning editor for that model.

The XML schema file we use in this tutorial can be found here: library.xsd. Save it somewhere on your workstation for later use.

The mapping rules from XML schema to ECore are:

From a modeling perspective, an XML schema is not as expressive as ECore. An XML schema cannot be used to define bi-directional references, nor can it provide the type of a reference target.

The screenshots are based on version 2.1 of the Eclipse SDK, version 1.1.0 of EMF and version 1.1.0 of XSD.

Contents

Step 0: Prerequisites
Step 1: Importing the Model from an XML Schema
Step 2: Generating the EMF Model Code
Step 3: Generating an Editor for the Model
Step 4: Running the Generated Editor


 contents

Step 0: Prerequisites

The EMF runtime package includes the EMF generator and a number of related plugins. And the XML Infoset Model is a reference library that can examine, create or modify XML Schemas. After installing the EMF runtime package and XML Infoset Model, verify that they are available in your Eclipse environment:


 contents

Step 1: Importing the Model from XML Schema

Use the XML Schema file you saved earlier: library.xsd as a sample model.

Create a new Ecore model project in the workspace:

The Ecore model "com.example.library.ecore" has the package name as its file name. The package name comes from the value of the EPackage's nsPrefix attribute is derived from the schema's target namespace (if the schema has a target namespace), or from the URI file path of the schema document if the schema does not contain a target namespace. In this sample, the "library.xsd" file includes a targetNamespace attribute that has a value of "http://www.example.com/library". Therefore, the nsPrefix attribute for the "Library" is set to "com.example.library".


 contents

Step 2: Generating the EMF Model Code

The GenModel shows a root node, representing the whole model. The model object has children that represent its packages, whose children then represent classifiers (classes and datatypes, including enumerated types). The children of classes are class attributes, references, and operations; the children of enumerated types are enum literals.

In most cases, the values of the properities need not be changed from their default values, but these options can provide a great deal of control over the code that gets generated. This topic will be explored more fully in future tutorial material; for now, select several different GenModel objects, and observe their properties.

The GenModel is also the place where you initiate the code generation. By right-clicking on an object in the model, you can generate code for it.

After generation, the class interfaces and enum class will have been created, and a new pair of interfaces will have been created for the package itself and for the factory. There will also be two new packages, with "impl" and "util" suffixes, which contain implementations of the interfaces and additional utility classes, and a "plugin.xml" manifest file for the model plugin.

If you change the XML schema file, you can regenerate the model code from it, and the corresponding changes in the model code will be merged with any hand modifications that may have been made to the code that was generated previously. You can also selectively generate a subset of the model code by right-clicking on a package, class, or enum object and selecting "Generate Model Code" from the pop-up menu.


 contents

Step 3: Generating 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: (1) an "edit" plugin, which includes adapters that provide a structured view and perform command-based editing of the model objects, and (2) an "editor" plugin, which provides the UI for the editor and a wizard.

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:


 contents

Step 4: Running the Generated Editor

In order to run plugins in a runtime workbench, a lunch configuration must first be set up:

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.

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

The root object in this editor corresponds to the "My.library" resource. Under it lies a single library, the object that was selected as the model object in the wizard.


contents