blob: 3d4e2a16333307d943441c574a984cf05c3a7a00 [file] [log] [blame]
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Code Generation tutorial</title>
<link href="book.css" rel="stylesheet" type="text/css">
<meta content="DocBook XSL Stylesheets V1.75.1" name="generator">
<link rel="home" href="index.html" title="OCL Documentation">
<link rel="up" href="Tutorials.html" title="Tutorials">
<link rel="prev" href="SafeNavigationTutorial.html" title="Safe navigation tutorial">
<link rel="next" href="DebuggerTutorial.html" title="Debugger tutorial">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<h1 xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0">Code Generation tutorial</h1>
<div class="section" title="Code Generation tutorial">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both">
<a name="CodeGenerationTutorial"></a>Code Generation tutorial</h2>
</div>
</div>
</div>
<p>This tutorial has been updated for Eclipse Luna: Eclipse 4.4, EMF 2.10, OCL 5.0.</p>
<div class="itemizedlist">
<ul class="itemizedlist" type="disc">
<li class="listitem">
<p>Some screenshots may be slightly out of date.</p>
</li>
</ul>
</div>
<p>The direct OCL to Java Code generator was a very experimental functionality for the Juno release. It has been substantially rewritten for Kepler. Some optimisations have been activated for Luna and Mars. </p>
<p>In this tutorial we will continue the OCLinEcore tutorial and show how to get a direct Java representation of the Ecore model avoiding the need for run-time compilation.</p>
<div class="section" title="Load OCLinEcore Tutorial Example Project">
<div class="titlepage">
<div>
<div>
<h3 class="title">
<a name="LoadOCLinEcoreTutorialExampleProject"></a>Load OCLinEcore Tutorial Example Project</h3>
</div>
</div>
</div>
<p>All the material for this tutorial is available as part of the OCLinEcore Example project that you
may load by selecting
<span class="bold"><strong>New</strong></span> then
<span class="bold"><strong>Example...</strong></span> using the right button context menu of the Project Explorer. This
should give the
<span class="bold"><strong>New Example</strong></span> dialog in which you can select the
<span class="bold"><strong>OCL (OCL Constraint Language) Plugins</strong></span> and the
<span class="bold"><strong>OCLinEcore Tutorial</strong></span>.
</p>
</div>
<div class="section" title="Direct code">
<div class="titlepage">
<div>
<div>
<h3 class="title">
<a name="Directcode"></a>Direct code</h3>
</div>
</div>
</div>
<p>In
<a class="link" href="Tutorials.html#OCLinEcoreTutorial-genmodel" title="Generating Java Code">Generating Java Code</a> we saw how to create a genmodel and how to generate code from it that realizes OCL as text strings in the Java implementation files. These text string are lazily compiled at run-time.
</p>
<p>Whether to generate OCL as text strings for interpretation or to convert directly to Java is determined by the Code Generation Mode. This may be configured using the project property or workspace preference as described in
<a class="link" href="UserInterface.html#CodeGenerationMode" title="Code Generation Mode">Code Generation Mode</a>. So use
<span class="bold"><strong>Window-&gt;Preferences-&gt;OCL</strong></span> to change the setting to
<span class="bold"><strong>Generate Java code in xxxBodies classes</strong></span>.
</p>
<p>Now open
<span class="bold"><strong>Tutorial.genmodel</strong></span>, select the root resource and invoke
<span class="bold"><strong>Generate Model Code</strong></span> to (re)generate the Java code. This will take somewhat longer as additional work items show that the OCL is being compiled and that Xtend templates are generating additional Java code.
</p>
<div class="blockquote">
<blockquote class="blockquote">
<p>
Note that you must close
<span class="bold"><strong>Tutorial.genmodel</strong></span> while changing the Code Generation Mode.
</p>
<p>You may also need to delete the autogenerated *Impl files if you change from one mode of generation to another.</p>
</blockquote>
</div>
<p></p>
<p>That is all there is to it. Your model code is now 100% Java; no OCL parsing is needed at run-time. </p>
</div>
<div class="section" title="Using a GenAnnotation">
<div class="titlepage">
<div>
<div>
<h3 class="title">
<a name="UsingaGenAnnotation"></a>Using a GenAnnotation</h3>
</div>
</div>
</div>
<p>Changing the default genmodel setting is a little dangerous since the change will affect any other genmodel activities you perform. It is therefore advisable to reset the workspace preference setting to its default and use a GenAnnotation to embed the setting in the genmodel.</p>
<p>The easiest way to create the GenAnnotation that ensure direct code generation regardless of workspace or project preferences, is to paste the following three lines into your genmodel just above the
<code class="code">foreignModel</code> or
<code class="code">genpackages</code> element.
</p>
<div class="literallayout">
<p>
<code class="code">&nbsp;&nbsp;&lt;genAnnotations&nbsp;source="http://www.eclipse.org/OCL/GenModel"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;details&nbsp;key="Use&nbsp;Delegates"&nbsp;value="false"/&gt;<br>
&nbsp;&nbsp;&lt;/genAnnotations&gt;<br>
</code>
</p>
</div>
<p></p>
<p>Of course, if you want to enforce delegation you should set the
<code class="code">value</code> to
<code class="code">true</code>.
</p>
<p>If you don&rsquo;t like cutting and pasting into XMI files, you can achieve the same effect with the GenModel editor by:</p>
<div class="itemizedlist">
<ul class="itemizedlist" type="disc">
<li class="listitem">
<p>Enable annotation display using
<span class="bold"><strong>Generator-&gt;Show Annotations</strong></span>
</p>
</li>
<li class="listitem">
<p>Invoke
<span class="bold"><strong>Annotate</strong></span> from the right button context menu of the genmodel root element
</p>
</li>
<li class="listitem">
<p>Use the
<span class="bold"><strong>Properties View</strong></span> to set the GenAnnotation source to
<code class="code">http://www.eclipse.org/OCL/GenModel</code>
</p>
</li>
<li class="listitem">
<p>Invoke
<span class="bold"><strong>Add Detail</strong></span> from the right button context menu of the GenAnnotation
</p>
</li>
<li class="listitem">
<p>Use the
<span class="bold"><strong>Properties View</strong></span> to set the Detail key to
<code class="code">Use Delegates</code>
</p>
</li>
<li class="listitem">
<p>Use the
<span class="bold"><strong>Properties View</strong></span> to set the Detail value to
<code class="code">false</code>
</p>
</li>
</ul>
</div>
<p>
</p>
<div class="mediaobject">
<img src="images/4400-genannotation.png"></div>
<p>
</p>
<p>A further &ldquo;Use Null Annotations&rdquo; GenAnnotation may be used to control whether @NonNull and @Nullable annotations are emitted in the generated code.</p>
<div class="literallayout">
<p>
<code class="code">&nbsp;&nbsp;&lt;genAnnotations&nbsp;source="http://www.eclipse.org/OCL/GenModel"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;details&nbsp;key="Use&nbsp;Delegates"&nbsp;value="false"/&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;details&nbsp;key="Use&nbsp;Null&nbsp;Annotations"&nbsp;value="true"/&gt;<br>
&nbsp;&nbsp;&lt;/genAnnotations&gt;<br>
</code>
</p>
</div>
<p></p>
</div>
</div>
</body>
</html>