| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head> |
| <meta name="generator" content="HTML Tidy, see www.w3.org" /> |
| <meta http-equiv="Content-Type" |
| content="text/html; charset=utf-8" /> |
| <title>Generating an EMF 1.1 Model using an XML Schema</title> |
| </head> |
| <body> |
| <h2>Generating an EMF 1.1 Model using XML Schema</h2> |
| |
| <p>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.</p> |
| |
| <p>The XML schema file we use in this tutorial can be found here: |
| <a target="_code" |
| href="images/../library.xsd">library.xsd</a>. Save |
| it somewhere on your workstation for later use.</p> |
| |
| <p>The mapping rules from XML schema to ECore are:</p> |
| |
| <ul> |
| <li>Each target namespace of a schema maps to an EPackage.</li> |
| |
| <li>A complex type definition or a top level element declaration |
| maps to an EClass.</li> |
| |
| <li>A simple type definition maps to an EDataType, except for the |
| types anyURI, QName, IDREF, or IDREFS, which instead map to the |
| EClass for EObject.</li> |
| |
| <li>An attribute declaration or a nested element declaration maps |
| to an EAttribute if its type maps to an EDataType, or to an |
| EReference if its type maps to an EClass.</li> |
| </ul> |
| |
| 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. |
| <br /> |
| <br /> |
| |
| |
| <p>The screenshots are based on version 2.1 of the Eclipse SDK, |
| version 1.1.0 of EMF and <a |
| href="http://www.eclipse.org/emf/downloads/dl-xsd1x.html"> |
| version 1.1.0 of XSD.</a></p> |
| |
| <h3>Contents</h3> |
| |
| <table summary="" border="0" cellspacing="2" cellpadding="2" |
| width="100%"> |
| <tr> |
| <td valign="top" width="60">Step 0:</td> |
| <td valign="top"><a href="#step0">Prerequisites</a></td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 1:</td> |
| <td valign="top"><a href="#step1">Importing the Model from an XML |
| Schema</a> </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 2:</td> |
| <td valign="top"><a href="#step2">Generating the EMF Model Code</a> |
| </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 3:</td> |
| <td valign="top"><a href="#step3">Generating an Editor for the |
| Model</a> </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 4:</td> |
| <td valign="top"><a href="#step4">Running the Generated Editor</a> |
| </td> |
| </tr> |
| </table> |
| |
| <br /> |
| <hr /> |
| <p style="text-align: right"><a id="step0" |
| name="step0"> </a><a href="#top">contents</a></p> |
| |
| <h3>Step 0: Prerequisites</h3> |
| |
| <p>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 <a |
| href="http://www.eclipse.org/emf/downloads/dl-xsd1x.html"> |
| XML Infoset Model</a>, verify that they are available in your |
| Eclipse environment:</p> |
| |
| <ul> |
| <li>Bring up the "Help/About Eclipse Platform" dialog.<br /> |
| <img src="images/../../clibmod/images/cg001.gif" |
| alt="Help/About Eclipse Platform" height="123" width="168" /><br /> |
| </li> |
| |
| <li>Click on "Plug-in Details".<br /> |
| <img src="images/../../clibmod/images/cg002.gif" |
| alt="Plug-in Details" height="332" width="464" /><br /> |
| </li> |
| |
| <li>Check that the highlighted plugins are present.<br /> |
| <img src="images/../../clibmod/images/cg003.gif" |
| alt="Check EMF plugins" height="437" width="595" /><br /> |
| <br /> |
| <img src="images/xl001.gif" alt="Check XSD plugins" height="437" |
| width="595" /><br /> |
| </li> |
| </ul> |
| |
| <hr /> |
| <p style="text-align: right"><a id="step1" |
| name="step1"> </a><a href="#top">contents</a></p> |
| |
| <h3>Step 1: Importing the Model from XML Schema</h3> |
| |
| <p>Use the XML Schema file you saved earlier: <a target="_code" |
| href="images/../library.xsd">library.xsd</a> as a |
| sample model.</p> |
| |
| <p>Create a new Ecore model project in the workspace:</p> |
| |
| <ul> |
| <li>Bring up the "File/New/Project..." dialog.<br /> |
| <img src="images/../../clibmod/images/cg101.gif" |
| alt="File/New/Project..." height="55" width="341" /><br /> |
| </li> |
| |
| <li>Select "Eclipse Modeling Framework" and "EMF Project". Click |
| the "Next" button.<br /> |
| <img src="images/../../clibmod/images/cg132.gif" alt="EMF Project" |
| height="150" width="394" /><br /> |
| </li> |
| |
| <li>Give the project a name, say, "library". Then, click the "Next" |
| button.<br /> |
| <img src="images/../../clibmod/images/cg133.gif" |
| alt="Name the project" height="203" width="294" /><br /> |
| </li> |
| |
| <li>Select "Load from an XML Schema" and click the "Next" |
| button.<br /> |
| <img src="images/xl101.gif" alt="Load from an XML Schema" |
| height="200" width="255" /><br /> |
| </li> |
| |
| <li>Click on the "Browse File System..." or "Browse Workspace..." |
| button to locate and load the XML schema file on your system. |
| Alternatively, you can enter the XML schema file's location |
| directly in the "XML Schema URI" entry field and then click on the |
| "Load..." button to load it. (Note: If you locate the schema using |
| either "Browse File System..." button or "Brose Workspace..." |
| button, you do not need to click on the "Load..." button.)<br /> |
| <img src="images/xl102.gif" alt="Browse for XML Schema file" |
| height="200" width="500" /><br /> |
| </li> |
| |
| <li>After the XML schema file is loaded, a default GenModel name |
| will be suggested. You can change the name in the "Generator model |
| name" entry field if you wish. You can also select "Create XML |
| Schema to Ecore Map" to generate a mapping file that maps from an |
| XML Schema to Ecore, with an extension of ".xsd2ecore". If you do |
| not select this option, the mapping file is not generated. Click |
| the "Next" button.<br /> |
| <img src="images/xl103.gif" alt="Gen Model Name" height="500" |
| width="500" /><br /> |
| </li> |
| |
| <li>Select the package for which you want to generate an EMF model |
| - In general, there can be many packages within a single schema. |
| However, in this case, there is only one package, "library". Click |
| the "Finish" button.<br /> |
| <img src="images/xl104.gif" alt="Select package" height="500" |
| width="500" /><br /> |
| </li> |
| |
| <li>Three files will be created: (1) A mapping file that maps from |
| an XML Schema to Ecore, with an extension of ".xsd2ecore", (2) an |
| Ecore model, with an extension of ".ecore", and (3) a GenModel, |
| with an extension of ".genmodel". The GenModel, which controls code |
| generation for the model appears in the main view.<br /> |
| <img src="images/xl105.gif" alt="Opened GenModel" height="167" |
| width="498" /><br /> |
| </li> |
| </ul> |
| |
| <p>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".</p> |
| |
| <hr /> |
| <p style="text-align: right"><a id="step2" |
| name="step2"> </a><a href="#top">contents</a></p> |
| |
| <h3>Step 2: Generating the EMF Model Code</h3> |
| |
| <p>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.</p> |
| |
| <ul> |
| <li>The GenModel can be expanded to see its various elements.<br /> |
| <img src="images/xl201.gif" alt="Expanded GenModel" height="200" |
| width="320" /><br /> |
| </li> |
| |
| <li>There are properties associated with each node. If the |
| Properties view isn't already showing, bring up "Window/Show |
| View/Other..."<br /> |
| <img src="images/../../clibmod/images/cg202.gif" |
| alt="Window/Show View/Other..." height="257" width="378" /><br /> |
| </li> |
| |
| <li>Expand the "Basic" set of views and choose "Properties".<br /> |
| <img src="images/../../clibmod/images/cg203.gif" |
| alt="Chose Properties" height="164" width="153" /><br /> |
| </li> |
| |
| <li>These properties control the behavior of the code |
| generator.<br /> |
| <img src="images/xl202.gif" alt="GenModel properties" height="523" |
| width="518" /><br /> |
| </li> |
| </ul> |
| |
| <p>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.</p> |
| |
| <p>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.</p> |
| |
| <ul> |
| <li>Right-click the "library" model object and select "Generate |
| Model Code" from the pop-up menu.<br /> |
| <img src="images/../../clibmod/images/cg207.gif" |
| alt="Generate Model" height="238" width="213" /><br /> |
| </li> |
| |
| <li>Observe the generated files.<br /> |
| <img src="images/xl203.gif" alt="Generated model code" |
| height="340" width="246" /><br /> |
| <br /> |
| </li> |
| </ul> |
| |
| <p>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.</p> |
| |
| <p>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.</p> |
| |
| <hr /> |
| <p style="text-align: right"><a id="step3" |
| name="step3"> </a><a href="#top">contents</a></p> |
| |
| <h3>Step 3: Generating an Editor for the Model</h3> |
| |
| <p>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.</p> |
| |
| <ul> |
| <li>In the GenModel, right-click the "Library" model object and |
| select "Generate Edit Code" from the pop-up menu.<br /> |
| <img src="images/../../clibmod/images/cg301.gif" |
| alt="Generate Edit Code" height="240" width="220" /><br /> |
| </li> |
| |
| <li>Right-click the model object and select "Generate Editor Code" |
| from the pop-up menu.<br /> |
| <img src="images/../../clibmod/images/cg304.gif" |
| alt="Generate Editor Code" height="233" width="212" /><br /> |
| </li> |
| |
| <li>Observe the generated projects, with "edit" and "editor" |
| suffixes.<br /> |
| <img src="images/../../clibmod/images/cg302.gif" |
| alt="Edit and editor projects" height="37" width="118" /><br /> |
| </li> |
| </ul> |
| |
| <p>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.</p> |
| |
| <p>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:</p> |
| |
| <ul> |
| <li>Select "Rebuild All" from the "Project" menu.<br /> |
| <img src="images/../../clibmod/images/cg303.gif" |
| alt="Project/Rebuild All" height="110" width="377" /><br /> |
| </li> |
| |
| <li>There should no errors in the "library", "library.edit", and |
| "library.editor" projects.<br /> |
| <img src="images/../../clibmod/images/cg305.gif" alt="No errors" |
| height="72" width="247" /><br /> |
| </li> |
| </ul> |
| |
| <hr /> |
| <p style="text-align: right"><a id="step4" |
| name="step4"> </a><a href="#top">contents</a></p> |
| |
| <h3>Step 4: Running the Generated Editor</h3> |
| |
| <p>In order to run plugins in a runtime workbench, a lunch |
| configuration must first be set up:</p> |
| |
| <ul> |
| <li>Select "Run..." from the "Run" menu.<br /> |
| <img src="images/xl401.gif" alt="Run/Run..." height="155" |
| width="493" /><br /> |
| </li> |
| |
| <li>Select "Run-time Workbench" and click the "New" button.<br /> |
| <img src="images/../../clibmod/images/cg402.gif" |
| alt="New Run-time Workbench" height="329" width="615" /><br /> |
| </li> |
| |
| <li>You may want to change the configuration's name to something |
| more recognizable. Then, switch to the "Plug-ins and Fragments" |
| tab.<br /> |
| <img src="images/../../clibmod/images/cg403.gif" |
| alt="Name the configuration" height="245" width="575" /><br /> |
| </li> |
| |
| <li>Select "Choose plugin-ins and fragments to launch from the |
| list", and then select the top-level "External Plug-ins".<br /> |
| <img src="images/../../clibmod/images/cg404.gif" |
| alt="Select all External Plugins" height="297" width="457" /><br /> |
| </li> |
| |
| <li>Switch to the "Common" tab, select "Run" from the "Display in |
| favorites menu" section of the dialog, and the click the "Apply" |
| button.<br /> |
| <img src="images/../../clibmod/images/cg405.gif" |
| alt="Display in favorites menu: Run" height="363" |
| width="470" /><br /> |
| </li> |
| </ul> |
| |
| <p>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.</p> |
| |
| <ul> |
| <li>Select the new launch configuration from the "Run" button |
| drop-down.<br /> |
| <img src="images/../../clibmod/images/cg406.gif" |
| alt="Library Configuration" height="124" width="355" /><br /> |
| </li> |
| |
| <li>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.<br /> |
| <img src="images/../../clibmod/images/cg407.gif" |
| alt="Plug-in Details" height="437" width="595" /><br /> |
| </li> |
| </ul> |
| |
| <p>The library model wizard can now be used to create a new |
| instance of the model.</p> |
| |
| <ul> |
| <li>Working in the Resource perspective, Bring up the |
| "File/New/Project..." dialog.<br /> |
| <img src="images/../../clibmod/images/cg101.gif" |
| alt="File/New/Project..." height="55" width="341" /><br /> |
| </li> |
| |
| <li>Select "Simple" and "Project". Click the "Next" button.<br /> |
| <img src="images/../../clibmod/images/cg408.gif" |
| alt="Simple Project" height="91" width="333" /><br /> |
| </li> |
| |
| <li>Give the project a name and click the "Finish" button.<br /> |
| <img src="images/../../clibmod/images/cg409.gif" |
| alt="Name the project" height="126" width="213" /><br /> |
| </li> |
| |
| <li>Right-click the project and select "New/Other..." from the |
| pop-up menu.<br /> |
| <img src="images/../../clibmod/images/cg410.gif" |
| alt="New/Other..." height="178" width="387" /><br /> |
| </li> |
| |
| <li>Select "Example EMF Model Creation Wizards" and "Library |
| Model". Click the "Next" button.<br /> |
| <img src="images/../../clibmod/images/cg411.gif" |
| alt="Library Model" height="500" width="500" /><br /> |
| </li> |
| |
| <li>Enter a file name for the library model. Make sure it ends with |
| a ".library" extension. Then click the "Next" button.<br /> |
| <img src="images/../../clibmod/images/cg412.gif" |
| alt="Name the library model file" height="46" width="146" /><br /> |
| </li> |
| |
| <li>Select "Library" as the model object, and click the "Finish" |
| button.<br /> |
| <img src="images/../../clibmod/images/cg413.gif" |
| alt="Select the Model Object" height="238" width="498" /><br /> |
| </li> |
| |
| <li>The newly created library model is opened in the main |
| view.<br /> |
| <img src="images/../../clibmod/images/cg414.gif" |
| alt="New library model" height="118" width="482" /><br /> |
| </li> |
| </ul> |
| |
| <p>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.</p> |
| |
| <ul> |
| <li>Expand the "platform:/resource/librarytest/My.library" resource |
| to see the library object. Select it.<br /> |
| <img src="images/../../clibmod/images/cg415.gif" |
| alt="Expanded resource" height="242" width="592" /><br /> |
| </li> |
| |
| <li>In the Properties view, click on the "Value" column of the |
| "Name" property, and give a name to the library. The label in the |
| main view will be updated when you hit Enter.<br /> |
| <img src="images/../../clibmod/images/cg416.gif" |
| alt="Name the library" height="105" width="646" /><br /> |
| </li> |
| |
| <li>Right-click on the library and select "New Child/Writer" from |
| the pop-up menu to add a new writer to the library.<br /> |
| <img src="images/../../clibmod/images/cg417.gif" |
| alt="New Child/Writer" height="240" width="332" /><br /> |
| </li> |
| |
| <li>Enter the name of the writer in the Properties view.<br /> |
| <img src="images/../../clibmod/images/cg418.gif" |
| alt="Name the writer" height="260" width="470" /><br /> |
| </li> |
| |
| <li>Similarly, a book can be added to the library.<br /> |
| <img src="images/../../clibmod/images/cg419.gif" |
| alt="New Child/Book" height="238" width="325" /><br /> |
| </li> |
| |
| <li>All the book's attributes and references can be edited in the |
| Properties view.<br /> |
| <img src="images/../../clibmod/images/cg420.gif" |
| alt="Book properties" height="365" width="568" /><br /> |
| </li> |
| |
| <li>You can save, close, and then re-open the model using the text |
| editor if you wish to see the saved model in XMI format.<br /> |
| <img src="images/../../clibmod/images/cg421.gif" |
| alt="Open With/Text Editor" height="213" width="379" /><br /> |
| </li> |
| |
| <li>The XMI format file of "My.library" contains a Library object |
| called "Skills Center", a Book object with the value of title as |
| "Fellowinship of the Ring", and a Writer object that "JRR Tolkien" |
| is the writer's name.<br /> |
| <img src="images/xl402.gif" alt="XMI format" height="330" |
| width="564" /><br /> |
| </li> |
| </ul> |
| |
| <hr /> |
| <p style="text-align: right"><a href="#top">contents</a></p> |
| </body> |
| </html> |
| |