| <!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" xml:lang="en" lang="en"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| <link rel="stylesheet" href="images/../../../css/book.css" type="text/css"/> |
| <link rel="stylesheet" href="images/../../../css/emf-book.css" type="text/css"/> |
| <title>Generating an EMF Model</title> |
| </head> |
| <body lang="EN-US" xml:lang="EN-US"> |
| <h1>Generating an EMF Model</h1> |
| |
| <p>Last updated: June 29, 2004</p> |
| |
| <p>This tutorial is a step-by-step description of the process of creating an EMF |
| model and generating a simple model editor for it. Following this tutorial will |
| show how easy EMF makes it to go from a simple model definition to a fully |
| functioning editor for that model.</p> |
| |
| <p>The model we will generate looks like this in UML (see the |
| <a href="../../references/overview/EMF.html">Eclipse Modeling Framework |
| Overview</a> for a description of this model):</p> |
| |
| <img src="images/model.gif" alt="Library UML model"/> |
| |
| <p>We will show how an EMF model can be generated from either of two different |
| sources: a Rational Rose model or a set of annotated Java interfaces and |
| classes.</p> |
| |
| <p>The screenshots are based on version 3.0 of the Eclipse SDK and version 2.0 |
| of EMF.</p> |
| |
| <hr/> |
| <h2>Contents</h2> |
| |
| <table 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="#step1a">Import the Model from Rose</a> or |
| <a href="#step1b">Define the Model Using Annotated Java</a> |
| </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 2:</td> |
| <td valign="top"><a href="#step2">Generate the EMF Model Code</a> |
| </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 3:</td> |
| <td valign="top"><a href="#step3">Generate an Editor for the |
| Model</a> </td> |
| </tr> |
| |
| <tr> |
| <td valign="top">Step 4:</td> |
| <td valign="top"><a href="#step4">Run 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> |
| <h2>Step 0: Prerequisites</h2> |
| |
| <p>The EMF Runtime package includes the EMF generator and a number of related |
| plug-ins. After installing the package, verify that they are available in your |
| Eclipse environment:</p> |
| |
| <ul> |
| <li>Bring up the "Help/About Eclipse Platform" dialog.<br/> |
| <img src="images/img001.gif" alt="Help/About Eclipse Platform"/><br/> |
| </li> |
| |
| <li>Click on "Plug-in Details".<br/> |
| <img src="images/img002.gif" alt="Plug-in Details"/><br/> |
| </li> |
| |
| <li>Click the "Plug-in Id" column heading to order the plug-ins by that field. |
| Then, check that the highlighted set of plug-ins are present.<br/> |
| <img src="images/img003.gif" alt="Check EMF plug-ins"/><br/> |
| </li> |
| </ul> |
| |
| <p>Additional EMF plug-ins, which are not highlighted above, are not required |
| for this tutorial. They may or may not appear, depending on which EMF packages |
| you installed.</p> |
| |
| <hr/> |
| <p style="text-align: right"><a id="step1a" name="step1a"> </a><a href="#top">contents</a></p> |
| <h2>Step 1a: Import the Model from Rose</h2> |
| |
| <p>The Rose file for the library model can be found here: |
| <a target="_code" href="images/../library.mdl">library.mdl</a>. Save it |
| somewhere on your workstation.</p> |
| |
| <p>Create a new EMF project in the workspace:</p> |
| |
| <ul> |
| <li>Bring up the "File/New/Project..." dialog.<br/> |
| <img src="images/img101.gif" alt="File/New/Project..."/><br/> |
| </li> |
| |
| <li>Expand "Eclipse Modeling Framework" and select "EMF Project". Click the |
| "Next" button.<br/> |
| <img src="images/img102.gif" alt="EMF Project"/><br/> |
| </li> |
| |
| <li>Give the project a name, say, "library". Then, click the "Next" button.<br/> |
| <img src="images/img103.gif" alt="Name the project"/><br/> |
| </li> |
| |
| <li>Select "Load from a Rose class model" and click the "Next" button.<br/> |
| <img src="images/img104.gif" alt="Load from a Rose class model"/><br/> |
| </li> |
| |
| <li>Click on the "Browse" button and locate the Rose model file.<br/> |
| <img src="images/img105.gif" alt="Browse for Rose file"/><br/> |
| </li> |
| |
| <li>The Rose model file will be examined, and a default generator model name |
| will be suggested. You can change the name in the entry box if you wish. Then, |
| click the "Next" button.<br/> |
| <img src="images/img106.gif" alt="Generator model name"/><br/> |
| </li> |
| |
| <li>In general, a Rose model may contain more than one package. Select the |
| package for which you want to generate an EMF model -- in this case, the only |
| package, "library". Click the "Finish" button.<br/> |
| <img src="images/img107.gif" alt="Select package"/><br/> |
| </li> |
| |
| <li>A core model (library.ecore) and a generator model (library.genmodel) will |
| be created. The latter, which controls code generation for the model, is opened |
| in the main view.<br/> |
| <img src="images/img108.gif" alt="Opened generator"/><br/> |
| </li> |
| </ul> |
| |
| <hr/> |
| <p style="text-align: right"><a id="step1b" name="step1b"> </a><a href="#top">contents</a></p> |
| <h2>Step 1b: Define the Model Using Annotated Java</h2> |
| |
| <p>Instead of importing the model from a Rose diagram, we can start with a set |
| of Java interfaces and classes that correspond to the classes and enumerated |
| types, respectively, in the library model. This code is the bare minimum |
| required to illustrate the desired features. Based on it, a core model and a |
| generator model will be constructed, which will then drive generation of the |
| remaining code. The code is annotated with "@model" tags in Javadoc comments, |
| in order to specify any non-default values for the attributes and references of |
| the Ecore objects.</p> |
| |
| <blockquote> |
| <b><a target="_code" href="images/../Library.java">Library.java</a></b><a name="librarysrc"> </a><br/> |
| |
| <table border="1" frame="box"><tr><td> |
| <pre>package org.eclipse.example.library; |
| import java.util.List; |
| |
| /** |
| * @model |
| */ |
| public interface Library |
| { |
| /** |
| * @model |
| */ |
| String getName(); |
| |
| /** |
| * @model type="Writer" containment="true" |
| */ |
| List getWriters(); |
| |
| /** |
| * @model type="Book" containment="true" |
| */ |
| List getBooks(); |
| }</pre> |
| </td></tr></table> |
| </blockquote> |
| |
| <blockquote> |
| <b><a target="_code" href="images/../Book.java">Book.java</a></b><a name="booksrc"> </a><br/> |
| |
| <table border="1" frame="box"><tr><td> |
| <pre>package org.eclipse.example.library; |
| |
| /** |
| * @model |
| */ |
| public interface Book |
| { |
| /** |
| * @model |
| */ |
| String getTitle(); |
| |
| /** |
| * @model default="100" |
| */ |
| int getPages(); |
| |
| /** |
| * @model |
| */ |
| BookCategory getCategory(); |
| |
| /** |
| * @model opposite="books" |
| */ |
| Writer getAuthor(); |
| }</pre> |
| </td></tr></table> |
| </blockquote> |
| |
| <blockquote> |
| <b><a target="_code" href="images/../Writer.java">Writer.java</a></b><a name="writersrc"> </a><br/> |
| |
| |
| <table border="1" frame="box"><tr><td> |
| <pre>package org.eclipse.example.library; |
| |
| /** |
| * @model |
| */ |
| public interface Writer |
| { |
| /** |
| * @model |
| */ |
| String getName(); |
| |
| /** |
| * @model type="Book" opposite="author" |
| */ |
| java.util.List getBooks(); |
| }</pre> |
| </td></tr></table> |
| </blockquote> |
| |
| <blockquote> |
| <b><a target="_code" href="images/../BookCategory.java">BookCategory.java</a></b><a name="bookcategorysrc"> </a><br/> |
| |
| <table border="1" frame="box"><tr><td> |
| <pre>package org.eclipse.example.library; |
| |
| /** |
| * @model |
| */ |
| public class BookCategory |
| { |
| /** |
| * @model name="Mystery" |
| */ |
| public static final int MYSTERY = 0; |
| |
| /** |
| * @model name="ScienceFiction" |
| */ |
| public static final int SCIENCE_FICTION = 1; |
| |
| /** |
| * @model name="Biography" |
| */ |
| public static final int BIOGRAPHY = 2; |
| }</pre> |
| </td></tr></table> |
| </blockquote> |
| |
| <p>Create a new Java project in the workspace:</p> |
| |
| <ul> |
| <li>Bring up the "File/New/Project..." dialog.<br/> |
| <img src="images/img101.gif" alt="File/New/Project..."/><br/> |
| </li> |
| |
| <li>Expand "Java" and select "Java Project". Click the "Next" button.<br/> |
| <img src="images/img121.gif" alt="Java Project"/><br/> |
| </li> |
| |
| <li>Give the project a name, say, "library", and ensure that "Create separate |
| source and output folders" is selected. Then, click the "Next" button.<br/> |
| <img src="images/img122.gif" alt="Name the project"/><br/> |
| </li> |
| |
| <li>By default, code under the src subdirectory will be built, with the output |
| being placed in bin. Change the default output folder to "library/runtime", and |
| click the "Finish" button.<br/> |
| <img src="images/img123.gif" alt="Specify default output folder"/><br/> |
| </li> |
| |
| <li>The newly created Java project can be seen in the Package Explorer.<br/> |
| <img src="images/img124.gif" alt="Library Java project"/><br/> |
| </li> |
| </ul> |
| |
| <p>Create the first Java interface:</p> |
| |
| <ul> |
| <li>Right-click the "src" folder and select "New/Interface" from the pop-up |
| menu.<br/> |
| <img src="images/img125.gif" alt="New/Interface"/><br/> |
| </li> |
| |
| <li>Fill in the package and interface name. Click the "Finish" button.<br/> |
| <img src="images/img126.gif" alt="Create new interface"/><br/> |
| </li> |
| |
| <li>A new Java interface file is created and opened in the main view.<br/> |
| <img src="images/img127.gif" alt="Opened interface"/><br/> |
| </li> |
| |
| <li>Type (or paste) in the <a href="#librarysrc">Library.java</a> interface |
| code, as shown above. Pay special attention to the @model tags.<br/> |
| <img src="images/img128.gif" alt="Enter interface code"/><br/> |
| </li> |
| |
| <li>Select "Save" from the "File" menu to save the file.<br/> |
| <img src="images/img129.gif" alt="File/Save"/><br/> |
| </li> |
| </ul> |
| |
| <p>Create the other two interfaces (<a href="#booksrc">Book.java</a> and |
| <a href="#writersrc">Writer.java</a>) and the class |
| (<a href="#bookcategorysrc">BookCategory.java</a>) in the same way. Of course, |
| to create the class, select "New/Class" from the pop-up menu, instead of |
| "New/Interface".</p> |
| |
| <p>Create the EMF models:</p> |
| |
| <ul> |
| <li>In the Package Explorer view, right-click the "src" folder and select |
| "New/Other..." from the pop-up menu.<br/> |
| <img src="images/img130.gif" alt="New/Other..."/><br/> |
| </li> |
| |
| <li>Expand "Eclipse Modeling Framework" and select "EMF Models". Click the |
| "Next" button.<br/> |
| <img src="images/img131.gif" alt="EMF Models"/><br/> |
| </li> |
| |
| <li>Change the folder to "library/src/model".<br/> |
| <img src="images/img132.gif" alt="Change the folder"/><br/> |
| </li> |
| |
| <li>Change the file name to "library.genmodel" and click the "Next" button.<br/> |
| <img src="images/img133.gif" alt="Change the file name"/><br/> |
| </li> |
| |
| <li>Select "Load from annotated Java" and click the "Next" button.<br/> |
| <img src="images/img134.gif" alt="Load from annotated Java"/><br/> |
| </li> |
| |
| <li>Select the "library" package and click the "Finish" button.<br/> |
| <img src="images/img135.gif" alt="Check library"/><br/> |
| </li> |
| |
| <li>A core model (library.ecore) and a generator model (library.genmodel) will |
| be created. The latter, which controls code generation for the model, is opened |
| in the main view.<br/> |
| <img src="images/img136.gif" alt="Opened generator"/><br/> |
| </li> |
| </ul> |
| |
| <hr/> |
| <p style="text-align: right"><a id="step2" name="step2"> </a><a href="#top">contents</a></p> |
| <h2>Step 2: Generate the EMF Model Code</h2> |
| |
| <p>The generator model shows a root object, representing the whole model. This |
| 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 model can be expanded to see its various elements.<br/> |
| <img src="images/img201.gif" alt="Expanded generator model"/><br/> |
| </li> |
| |
| <li>There are properties associated with each object. If the Properties view |
| isn't already showing, right-click the "Library" model object and select "Show |
| Properties View" from the pop-up menu.<br/> |
| <img src="images/img202.gif" alt="Show Properties View"/><br/> |
| </li> |
| |
| <li>These properties control the behavior of the code generator.<br/> |
| <img src="images/img203.gif" alt="Generator model properties"/><br/> |
| </li> |
| </ul> |
| |
| <p>In most cases, the properites 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 generator model objects, and observe their |
| properties.</p> |
| |
| <p>The generator model 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/img204.gif" alt="Generate Model Code"/><br/> |
| </li> |
| |
| <li>Observe the generated files.<br/> |
| <img src="images/img205.gif" alt="Generated files"/><br/> |
| </li> |
| </ul> |
| |
| <p>After generation, the class interfaces and enum class will have been created |
| (if the model was imported from Rose) or completed (if the model was defined |
| using annotated Java), 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 |
| plug-in.</p> |
| |
| <p>If you defined the model by using annotated Java, you may see a warning in |
| the Problems view: "The import java.util.List is never used". This warning is |
| expected, and will not stop you from continuing on to the next step.</p> |
| |
| <p>If you change the model, you can regenerate it, and changes will be merged |
| with any hand modifications that may have been made to the code. 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> |
| <h2>Step 3: Generate an Editor for the Model</h2> |
| |
| <p>A fully functional Eclipse editor can also be generated for any model. By |
| default, it is split between two plug-ins: an "edit" plug-in includes adapters |
| that provide a structured view and perform command-based editing of the model |
| objects; an "editor" plug-in provides the UI for the editor and wizard.</p> |
| |
| <ul> |
| <li>In the generator, right-click the "Library" model object and select |
| "Generate Edit Code" from the pop-up menu.<br/> |
| <img src="images/img301.gif" alt="Generate Edit Code"/><br/> |
| </li> |
| |
| <li>Right-click the model object again and select "Generate Editor Code" from |
| the pop-up menu.<br/> |
| <img src="images/img302.gif" alt="Generate Editor Code"/><br/> |
| </li> |
| |
| <li>Observe the generated projects in the Package Explorer view, with "edit" and |
| "editor" suffixes.<br/> |
| <img src="images/img303.gif" alt="Edit and editor projects"/><br/> |
| </li> |
| </ul> |
| |
| <p>In general, if you wish to generate the model, edit, and editor plug-ins 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 "Build All" from the "Project" menu.<br/> |
| <img src="images/img304.gif" alt="Project/Build All"/><br/> |
| </li> |
| |
| <li>Observe the Problems view. If you imported the model from Rose, there should |
| be no errors in the library, library.edit, and library.editor projects.<br/> |
| <img src="images/img305.gif" alt="No errors"/><br/> |
| </li> |
| |
| <li>If you defined the model using annotated Java, there will be one warning in |
| the library project.<br/> |
| <img src="images/img306.gif" alt="One warning"/><br/> |
| </li> |
| |
| <li>Double click the warning to see the line of code that is causing it. It |
| occurs because the code generator changed the return type of Library's two |
| multiplicity-many references from "List" to "EList", but did not remove the |
| unused import. You can simply delete it, or have Eclipse do it for you, by |
| selecting "Organize Imports" from the "Source" menu.<br/> |
| <img src="images/img307.gif" alt="Source/Organize Imports"/><br/> |
| </li> |
| </ul> |
| |
| <hr/> |
| <p style="text-align: right"><a id="step4" name="step4"> </a><a href="#top">contents</a></p> |
| <h2>Step 4: Run the Generated Editor</h2> |
| |
| <p>In order to test the new plug-ins, a second instance of Eclipse, called a |
| run-time workbench must be launched. The plug-ins will run in this |
| workbench.</p> |
| |
| <ul> |
| <li>Select "Run As/Run-time Workbench" from the "Run" toolbar drop-down.<br/> |
| <img src="images/img401.gif" alt="Run/Run As/Run-time Workbench"/><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 plug-ins are there.<br/> |
| <img src="images/img402.gif" alt="Plug-in Details"/><br/> |
| </li> |
| </ul> |
| |
| <p>The Library Model wizard can now be used to create a new instance of the |
| model.</p> |
| |
| <ul> |
| <li>Bring up the "File/New/Project..." dialog.<br/> |
| <img src="images/img101.gif" alt="File/New/Project..."/><br/> |
| </li> |
| |
| <li>Expand "Simple" and select "Project". Click the "Next" button.<br/> |
| <img src="images/img403.gif" alt="Simple Project"/><br/> |
| </li> |
| |
| <li>Give the project a name and click the "Finish" button.<br/> |
| <img src="images/img404.gif" alt="Name the project"/><br/> |
| </li> |
| |
| <li>Right-click the project and select "New/Other..." from the pop-up menu.<br/> |
| <img src="images/img405.gif" alt="New/Other..."/><br/> |
| </li> |
| |
| <li>Expand "Example EMF Model Creation Wizards" and select "Library Model". |
| Click the "Next" button.<br/> |
| <img src="images/img406.gif" alt="Library Model"/><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/img407.gif" alt="Name the library model file"/><br/> |
| </li> |
| |
| <li>Select "Library" as the model object and click the "Finish" button.<br/> |
| <img src="images/img408.gif" alt="Select the model object"/><br/> |
| </li> |
| |
| <li>The newly created library model is opened in the main view.<br/> |
| <img src="images/img409.gif" alt="New library model"/><br/> |
| </li> |
| </ul> |
| |
| <p>The root object in this editor corresponds to the My.library resource. Under |
| it lies a single library, the object which 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/img410.gif" alt="Expanded resource"/><br/> |
| </li> |
| |
| <li>If the Properties view isn't already showing, right-click the "Library" |
| object and select "Show Properties View" from the pop-up menu.<br/> |
| <img src="images/img411.gif" alt="Show Properties View"/><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/img412.gif" alt="Name the library"/><br/> |
| </li> |
| |
| <li>Right-click the library and select "New Child/Writer" from the pop-up menu. |
| A new writer is added to the library.<br/> |
| <img src="images/img413.gif" alt="New Child/Writer"/><br/> |
| </li> |
| |
| <li>Enter the name of the writer in the Properties view.<br/> |
| <img src="images/img414.gif" alt="Name the writer"/><br/> |
| </li> |
| |
| <li>Similarly, a book can be added to the library.<br/> |
| <img src="images/img415.gif" alt="New Child/Book"/><br/> |
| </li> |
| |
| <li>All the book's attributes and references can edited in the Properties |
| view.<br/> |
| <img src="images/img416.gif" alt="Book properties"/><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/img417.gif" alt="Open With/Text Editor"/><br/> |
| </li> |
| </ul> |
| |
| <hr/> |
| <p style="text-align: right"><a href="#top">contents</a></p> |
| </body> |
| </html> |