<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Create Eclipse Ecore (EMF) Models manually
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/adietisheim">Andre Dietisheim</a> in <i>JBoss Tools</i> - <a href="http://community.jboss.org/docs/DOC-15705">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><h1><strong>Forewords</strong></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>EMF provides a runtime and tools that allow you to create ecore object models. The starting point is a model definition. It may be created out of a XML Schema (XSD), annotated java classes, etc.&#160; but you will mostly craft one by hand. This document attempts to describe the process involved in the later. It will show you the basic steps to create an ecore model implementation and give you some more advanced hints here and there.</p><div> </div><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>There are a few tutorials available on <a class="jive-link-external-small" href="http://www.eclipse.org/modeling/emf/docs/#tutorials">eclipse.org</a>, the best one so far's the one provided by <a class="jive-link-external-small" href="http://www.vogella.de/articles/EclipseEMF/article.html">Lars Vogella</a>.</p><p>If you want to create an ecore model manually, you have different editors at hand that you may use for this matter. The most common one is the <strong>Sample Ecore Model Editor</strong>, but you may also use text based editors like <a class="jive-link-external-small" href="http://wiki.eclipse.org/Emfatic">Emfatic</a> or <a class="jive-link-external-small" href="http://www.eclipse.org/Xtext/">Xtext</a>. There are also graphical editors around like the<strong><span style="font-weight: normal;"> </span><a class="jive-link-external-small" href="http://www.eclipse.org/modeling/emft/?project=ecoretools"><span style="font-weight: normal;">Ecore Diagram Editor</span></a><span style="font-weight: normal;"> or the <a class="jive-link-external-small" href="http://www.soyatec.com/euml2/">eUML2 Editor</a>.</span></strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Create an EMF Project</h1><p>To get started, create an empty EMF project.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/4918/new-empty-emf-project.jpg"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/4918/new-empty-emf-project.jpg </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>You'll get a new project, that's set up to work with the EMF framework. The model definitions, the <strong>ecore</strong> files are put into a <strong>model</strong> folder (not a must but a standard so far). Create a new ecore model file in this folder by selecting the folder and triggering the new ecore model wizard.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-11-4891/new-ecore-file.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-11-4891/new-ecore-file.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Create the Package</h1><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>If you want to create an ecore model manually, you have different editors at hand that you may use for this matter. The most common one is the <strong>Sample Ecore Model Editor</strong>, but you may also use text based editors like <a class="jive-link-external-small" href="http://wiki.eclipse.org/Emfatic">Emfatic</a> or <a class="jive-link-external-small" href="http://www.eclipse.org/Xtext/">Xtext</a>. There are also graphical editors around like the<strong><span style="font-weight: normal;"> </span><a class="jive-link-external-small" href="http://www.eclipse.org/modeling/emft/?project=ecoretools"><span style="font-weight: normal;">Ecore Diagram Editor</span></a><span style="font-weight: normal;"> or the <a class="jive-link-external-small" href="http://www.soyatec.com/euml2/">eUML2 Editor</a>.</span></strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>We created the empty ecore model so far, so the next step is to create a package for your model. You set its name, Ns Prefix and Ns URI.<br/>The good practice (or at least what most modelers do) is to set them to:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><ul><li><strong>name</strong>: a simple term (not required to be unique)</li><li><strong>Ns prefix</strong>: ~shoretened 'java package' name (not required to be unique)</li><li><strong>Ns URI</strong>: some real (or bogus) unique URI where the scheme might be found.</li></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h4>example</h4><p>I have a plugin/module in cdo called <strong>org.eclipse.emf.cdo.ui.defs</strong><br/>The ecore model for it has the following definitions:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>name</strong>: defs<br/><strong>Ns prefix</strong>: cdo.ui.defs<br/><strong>Ns URI</strong>: <a class="jive-link-external-small" href="http://www.eclipse.org/emf/CDO/ui/defs/1.0.0" target="_blank">http://www.eclipse.org/emf/CDO/ui/defs/1.0.0</a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Model Your Classes</h1><p>You can now add classes to your package. There are usually several ways to get to the desired result, most likely the best way to find out is to get the book or error and trial. A rule of thumb is to have all classes available in your model. This is evident for ecore classes. But if your ecore classes use references to plain java types (that are not part of your ecore model in the strict sense) you'll at least need to declare those java types in the ecore model. In other words, the ecore model needs to know about all types (ecore or plain java) that are part of your model.</p><h4>example1</h4><p>Let's say that my modeled class CDOEditorDefs has a method execute() that throws an ExecutionException. I could add that method by hand but as a matter of taste I prefer to declare that method in my model.</p><p>My model does not know anything about this exception so far, so there's no way to get the correct signature generated out of the box . I'll therefore have&#160; to declare this exception in model. I create a DataType <strong>ExecutionException</strong>.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>create an EDataType:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/4911/declare-datatype.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/4911/declare-datatype.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="text-align: left; ">Give it an instance type name so it won't be generated but is referencable:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/4913/declare-executionexception.jpg"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/4913/declare-executionexception.jpg </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Set the execute method (operation) to throw the ExecutionException:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/4914/declare-throws-executionexception.jpg"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/4914/declare-throws-executionexception.jpg </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>The generated method now throws the given Exception:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/4915/generated-throws-executionexception.jpg"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/4915/generated-throws-executionexception.jpg </span></a></p><h4>example2</h4><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>A very common problem is to have modeled (ecore-) classes extend Java Interfaces. For instance this could be java.lang.Comparable<br/>The best way to achieve that is to model a class Comparable. Do not model its operation as this is just a mirror of the real java interface in the ecore-world.</p><p>Interfaces are modeled as supertypes in ecore. You therefore cannot use EDataTypes here&#160; as they cannot be supertypes of ecore classes. Nevertheless you can achieve that in a slight different manner: You model a (real) ecore class, but you set its instance type name: <strong>java.lang.Comparable</strong>. You can now add the Comparable class to the super type of each ecore-class that shall implement Comparable. The generator will not generate an ecore class that's called Comparable but it will include java.lang.Comparable in the interface that your ecore-classes implement.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em><strong>Not sure I follow this example and what we're trying to illustrate.&#160; Would example code help?</strong></em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Create a Genmodel</h1><p>This is mostly straight forward. Select the ecore file and create a genmodel for it.&#160; Select your ecore file and start a new <strong>EMF Generator Model </strong>wizard. The wizard will allow you to create a so called Generator Model that holds all settings which are important to the code generation process.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-11-4847/generate-models.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-11-4847/generate-models.png </span></a></p><div> </div><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>There are 2 settings that might be of interest to you:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>'<strong>All</strong>' (property group when the package is selected):<br/>- <strong>Base Package</strong>: the base package all ecore classes get generated to<br/>- <strong>Prefix</strong>: Prefix that the factory- and package-class get</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-11-4848/genmodel-all-properties.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-11-4848/genmodel-all-properties.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>example (still using the cdo.ui.defs example):</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>Base Package</strong>:&#160; org.eclipse.emf.cdo.ui<br/><strong>Prefix</strong>: CDOUIDefs</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Package-class gets CDOUIDefsPackage, Factory gets CDOUIDefsFactory, etc. All classes get generated to the package org.eclipse.emf.cdo.ui</p><p><br/>Further modifications you might be interested in are the suffixes of the sub-packages (the defaults creates an 'impl' package where it puts all implementation classes). It can be modified by selecting the '<strong>Package Suffixes</strong>' and choosing the properties '<strong>Implementation</strong>' and '<strong>Interfaces</strong>'.<br/>The naming of the implementation- and interface-classes may be changed, too. You find those settings if you select the root-node of the tree in the genmodel-editor and choose the '<strong>Model</strong>' property group. You'll find 'Class Name Pattern' and 'Interface Name Pattern' among the available properties. The explanations for the values show up in the statusbar (default is '<strong>{0}impl</strong>' and '<strong>{0}</strong>').</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-11-4849/genmodel-classname-pattern.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-11-4849/genmodel-classname-pattern.png </span></a></p><p>Once you're done defining your generator model, you simply need to generate the implementation classes. Select the package you want to generate, right click and select the implementation you want to create. You may choose among the models, the editor, the tests.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/102-15697-11-4850/genmodel-generate.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/102-15697-11-4850/genmodel-generate.png </span></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h1>Modify the Generated Classes</h1><p>Ecore is built to be modified, the basic usage-pattern is to code and generate hand-in-hand. To tell the generator not to override your modifications you need to set the javadoc-annotation to anything different than '@generated'. Good practice says that you should set it to '@generated NOT'. Good practice also tells you to annotate any manually added method by '@ADDED', but its optional though.<br/>There is another handy that allows you to modify and get the generated code. If you want to have your code instead of the generated one, you just annotate accordingly and the generator will preserve your code. If you want the generated code, too, you'd need to create a method that has the original name + a suffix 'Gen'</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>example:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>/**<br/>*<br/>* @generated NOT<br/>*/<br/>public void setName(String name) {<br/>&#160;&#160;&#160; YOUR OWN CODE<br/>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>/**<br/>*<br/>* @generated<br/>*/<br/>public void setNameGen(String name) {<br/>&#160;&#160;&#160; GENERATOR provides the generated code in here<br/>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>After making your modifications, you simply need to re-generate the ecore classes. (<em><strong>How?</strong></em>)</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-15705">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss Tools at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2128">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>