<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Heiko Behrens (Blog) &#187; Add new tag</title>
	<atom:link href="http://www.heikobehrens.net/tag/add-new-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.heikobehrens.net</link>
	<description>11:60 p.m. - my plain thoughts</description>
	<lastBuildDate>Fri, 20 Aug 2010 14:13:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generation Gap Pattern</title>
		<link>http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/</link>
		<comments>http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 19:35:51 +0000</pubDate>
		<dc:creator>Heiko Behrens</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[openArchitectureWare]]></category>
		<category><![CDATA[Add new tag]]></category>

		<guid isPermaLink="false">http://www.1160pm.net/?p=213</guid>
		<description><![CDATA[The Generation Gap Pattern is a common way to weave manually written code into generated software artifacts. While this pattern describes how to structure your classes logically it does not teach you where to place these classes physically as files into the filesystem.
Separating generated files from manually written code is best practice for many reasons. It [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.research.ibm.com/designpatterns/pubs/gg.html">Generation Gap Pattern</a> is a common way to weave manually written code into generated software artifacts. While this pattern describes how to structure your classes logically it does not teach you where to place these classes physically as files into the filesystem.</p>
<div id="attachment_452" class="wp-caption aligncenter" style="width: 461px"><img class="size-full wp-image-452" title="Figure 1" src="http://www.1160pm.net/wp-content/uploads/2009/04/generationgappattern.png" alt="Figure 1: The Generation Gap Pattern" width="451" height="230" /><p class="wp-caption-text">Figure 1: The Generation Gap Pattern</p></div>
<p>Separating generated files from manually written code is <a href="http://www.infoq.com/articles/model-driven-dev-best-practices">best practice</a> for many reasons. It enables you to use version control wisely and makes the distinction between generated and manually written code easy. The Eclipse <a href="http://wiki.eclipse.org/Modeling_Workflow_Engine_(MWE)">EMF Modeling Workflow Engine</a> together with <a href="http://wiki.eclipse.org/Xpand">M2T Xpand</a> allows you to accomplish this goal in different ways.</p>
<p>In this post, I will present three different approaches that share some common ideas. Each strategy uses a dedicated directory for generated class files whereas manually written subclasses are stored for themselves in a different directory.</p>
<p><span id="more-213"></span></p>
<h2>General Directory Layout</h2>
<p>Both sets of classes belong to the same package but reside in different source folders. Framework classes should be kept in yet another directory and might be placed in a package specific for the framework as seen in <a href="http://www.1160pm.net/wp-content/uploads/2009/04/projectgeneral.png">Figure 2</a>.</p>
<div id="attachment_435" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.1160pm.net/wp-content/uploads/2009/04/projectgeneral.png"><img class="size-medium wp-image-435" title="Figure 2" src="http://www.1160pm.net/wp-content/uploads/2009/04/projectgeneral-300x258.png" alt="Figure 2: Schematic directory layout of an Eclipse Java project" width="300" height="258" /></a><p class="wp-caption-text">Figure 2: Schematic directory layout of an Eclipse Java project</p></div>
<p>In Eclipse you can configure such a project structure by adding new source folders (<em>New</em> → <em>Source Folder&#8230;</em>) or by declaring ordinary folders as source folders in the project properties (<em>Properties</em> → <em>Java Build Path</em> → <em>Source</em>).</p>
<p>Please note that folder names such as <em>src-generatedClasses</em> are meant to be schematically and stand for a category of folders each. In real world projects you might have several source folders that contain generated artifacts and you might deal with many folders or even projects that contain manually written classes. In the following sections you will see how these categories of folders can be used in practice.</p>
<h2>Generate Once</h2>
<p><a href="http://www.peterfriese.de/how-to-distinguish-generated-code-from-non-generated-code/">Peter describes a directory layout</a> many <a href="http://www.xtext.org">Xtext users</a> are familiar with. Generated artifacts go into a folder <em>src-gen</em> whereas manually written classes will be stored in a folder <em>src-once</em>. Each time you run the generator the content of folder <em>src-gen</em> will be purged and generated again whereas the the content of <em>src-once</em> will remain untouched.</p>
<div id="attachment_283" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/generateonce.png"><img class="size-full wp-image-283" title="Figure 3" src="http://www.1160pm.net/wp-content/uploads/2009/03/generateonce.png" alt="Figure 3: src-once collects one-time generated artefacts for future editing" width="500" height="168" /></a><p class="wp-caption-text">Figure 3: Generate Once collects one-time generated artifacts for future editing</p></div>
<p>Since each model element <em>A</em> leads to two classes <em>AbstractA</em> and <em>ConcreteA</em> (<a href="http://www.research.ibm.com/designpatterns/pubs/gg.html">Generation Gap Pattern</a>) you can reduce the amount of manual work by generating the class <em>ConcreteA</em> if it does not exist, yet. During the next run of the generator the class <em>ConcreteA</em> won’t be generated again, hence the name “once”.</p>
<p>Configure an additional outlet in your EMF MWE file:</p>
<pre class="brush: xml; highlight:6">&lt;component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="src-gen"/&gt;
&lt;component class="org.eclipse.xpand2.Generator"&gt;
   &lt;metaModel idRef="mm"&gt;
   &lt;expand value="MyTamplate::main FOR model"&gt;
   &lt;outlet overwrite="true" path="src-gen"&gt;
   &lt;outlet name="MANUAL" overwrite="false" path="src-once"&gt;
&lt;/component&gt;</pre>
<p>Using M2T Expand you can than refer to the outlet <em>MANUAL</em> where appropriate</p>
<pre class="brush: plain">«DEFINE ConcreteClass FOR Element»
   «FILE this.fileName() MANUAL»
   ...
«ENDDEFINE»</pre>
<p>This approach works well in many scenarios but has its limits. As seen in <a href="http://www.1160pm.net/wp-content/uploads/2009/03/generateonce.png">Figure 3</a> problems arise whenever you delete or rename a model element. In these cases each concrete artifact at <em>src-once</em> that does not correspond to a model element anymore is abandoned and will remain with compile errors. This is desired for classes that hold manual code but is cumbersome for those that do not.</p>
<div id="attachment_288" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/projectonce.png"><img class="size-medium wp-image-288" title="Figure 4" src="http://www.1160pm.net/wp-content/uploads/2009/03/projectonce-300x256.png" alt="Figure 4" width="300" height="256" /></a><p class="wp-caption-text">Figure 4: Generate Once in package explorer</p></div>
<p>For scenarios where a huge amount of generated classes is customized only in a few cases the &#8220;Generate Once&#8221; approach leads to unwanted manual refactoring effort including file movement. Life is getting worse when you are working with a complex folder structure instead of a single flat directory. <a href="http://www.1160pm.net/wp-content/uploads/2009/03/projectonce.png">Figure 4</a> might give you an idea of how things will look for growing models.</p>
<h2>Conditional Generation 1 (with one gen-folder)</h2>
<p>The following approach plays well when only a small subset of classes has to be extended with manual code. According to the <a href="http://www.research.ibm.com/designpatterns/pubs/gg.html">Generation Gap Pattern</a> for each model element <em>A</em> there is a generated class<em> AbstractA</em> as well as an concrete class <em>ConcreteA</em>. But in contrast to the “Generate Once” approach both files will be placed in the very same folder <em>src-gen</em> and therefore deleted and regenerated for each generator run.<br />
There is one exception to this behavior. Whenever the generator finds a file <em>ConcreteA</em> in the folder <em>src-man</em> it skips the generation process for this particular file. This is how you introduce manual code.</p>
<div id="attachment_289" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/generatecond1.png"><img class="size-full wp-image-289" title="Figure 5" src="http://www.1160pm.net/wp-content/uploads/2009/03/generatecond1.png" alt="Figure 5" width="500" height="214" /></a><p class="wp-caption-text">Figure 5: Conditional Generation 1 reduces manual refactoring effort</p></div>
<p>At first glance this approach seems to be similar to the solution above. But when deleting or moving model elements this approach really pays off. Each concrete class that does not contain manual code will be deleted and regenerated. Therefore, when moving or deleting the model elements no additional manual work has to be done. Also, a quick look at <em>src-man</em> reveals any code that has been written manually.</p>
<div id="attachment_290" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/projectman.png"><img class="size-medium wp-image-290" title="Figure 6" src="http://www.1160pm.net/wp-content/uploads/2009/03/projectman-300x248.png" alt="Figure 6" width="300" height="248" /></a><p class="wp-caption-text">Figure 6: Conditional Generation 1 in package explorer</p></div>
<p>To implement the desired behavior of the generator users of MWE can configure an outlet with a special veto strategy. Please note that the outlet <em>MANUAL</em> does not need to keep files (<em>overwrite=&#8221;true&#8221;</em>), they will be purged by the directory cleaner anyway.</p>
<pre class="brush: xml; highlight:6-7">&lt;component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="src-gen"/&gt;
&lt;component class="org.eclipse.xpand2.Generator"&gt;
  &lt;metaModel idRef="mm" /&gt;
  &lt;expand value="MyTamplate::main FOR model"/&gt;
  &lt;outlet overwrite="true" path="src-gen" /&gt;
  &lt;outlet name="MANUAL" overwrite="true" path="src-gen"&gt;
    &lt;vetoStrategy class="your.package.CondGenVetoStrategy" path="src-man"/&gt;
  &lt;/outlet&gt;
&lt;/component&gt;</pre>
<p>Such a veto strategy would read like:</p>
<pre class="brush: java; collapse: true; toolbar: true;">public class CondGenVetoStrategy implements VetoStrategy, VetoStrategy2 {

	private String path;

	public void setPath(String value) {
		path = value;
	}

	public String getPath() {
		return path;
	}

	public boolean hasVeto(FileHandle fileHandle) {
		URI fileToBeGenerated = fileHandle.getTargetFile().toURI();
		URI pathToGenerateInto = new File(fileHandle.getOutlet().getPath()).getAbsoluteFile().toURI();
		URI relativeFileName = pathToGenerateInto.relativize(fileToBeGenerated);
		File potentiallyExistingFile = new File(this.getPath(), relativeFileName.toString());

		return potentiallyExistingFile.exists();
	}

	public boolean hasVetoBeforeOpen(FileHandle handle) {
		return hasVeto(handle);
	}
}</pre>
<p><strong>oAW 4.x: </strong>VetoStrategy2 has been introduced with oAW 4.3.1.</p>
<p>Unfortunately, the advantages of this approach do not come for free: Compared to &#8220;Generate Once&#8221; it is more difficult to introduce manual code since it is harder to understand the needed steps. One has to create a concrete class in the <em>src-man</em> folder manually (or move it from <em>src-gen</em> and remove all comments that mark them as generated) where no class exists, yet. This might seem to be a no-brainer but can lead into trouble when defining a process for large projects. This is especially true for the unlikely case that you check-in generated code of <em>src-gen</em>.</p>
<h2>Conditional Generation 2 (with two gen-folders)</h2>
<p>A slighty different way that basically follows the same idea introduces yet another folder <em>src-gencond</em>.</p>
<div id="attachment_291" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/generatecond2.png"><img class="size-full wp-image-291" title="Figure 7" src="http://www.1160pm.net/wp-content/uploads/2009/03/generatecond2.png" alt="Figure 7" width="500" height="217" /></a><p class="wp-caption-text">Figure 7: Conditional Genration 2 lists classes that can implemented manually</p></div>
<p>As you can see in <a href="http://www.1160pm.net/wp-content/uploads/2009/03/generatecond2.png">Figure 7</a> the generated artifacts are split into the folder <em>src-gen</em> where the abstract classes reside and the newly introduced folder <em>src-gencond</em>. The latter contains only the concrete classes that could be replaced by manually implemented classes. In contrast to the “Conditional Generation 1” approach the users are now able to find classes that can be augmented be manual implementations without any challenge. Users still have to move classes from <em>src-gencond</em> to <em>src-man</em> by hand but it is simpler to communicate the necessary steps.</p>
<div id="attachment_292" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.1160pm.net/wp-content/uploads/2009/03/projectcond.png"><img class="size-medium wp-image-292" title="Figure 8" src="http://www.1160pm.net/wp-content/uploads/2009/03/projectcond-300x232.png" alt="Figure 8" width="300" height="232" /></a><p class="wp-caption-text">Figure 8: Conditional Generation 2 in package explorer</p></div>
<p>As you can see in <a href="http://www.1160pm.net/wp-content/uploads/2009/03/projectcond.png">Figure 8</a> one can easily identify that there are two classes that could be implemented by hand. Such a folder structure can pay off when different groups of developers are dealing with a fairly large and scattered set of classes.</p>
<p>In order to realize &#8220;Conditional Generation 2&#8243; you simply reconfigure the outlet <em>MANUAL</em> to write its output to <em>src-gencond</em>. Also, a second directory cleaner removes all files from this directory before code generation starts.</p>
<pre class="brush: xml; highlight: [2, 7]">&lt;component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="src-gen"/&gt;
&lt;component class="org.eclipse.emf.mwe.utils.DirectoryCleaner" directory="src-gencond"/&gt;
&lt;component class="org.eclipse.xpand2.Generator"&gt;
  &lt;metaModel idRef="mm" /&gt;
  &lt;expand value="MyTamplate::main FOR model"/&gt;
  &lt;outlet overwrite="true" path="src-gen" /&gt;
  &lt;outlet name="MANUAL" overwrite="true" path="src-gencond"&gt;
    &lt;vetoStrategy class="your.package.CondGenVetoStrategy" path="src-man"/&gt;
  &lt;/outlet&gt;
&lt;/component&gt;</pre>
<h2>Comparison</h2>
<p>Each of these solutions do their job in real world <a href="http://en.wikipedia.org/wiki/Model_Driven_Software_Development">MDSD</a> projects. Where projects with rather stable interfaces go with the “Generate Once” approach larger projects address growing maintenance effort with the “Conditional Generatation 1/2”  solution. The move from “Conditional Generatation 1” towards “Conditional Generatation 2” is rather trival and can smoothly be taken within a running project.</p>
<p>I&#8217;d be happy to read about your experience with these or different project setups.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-mail">
			<a href="mailto:?subject=%22Generation%20Gap%20Pattern%22&amp;body=Link: http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A The%20Generation%20Gap%20Pattern%20is%20a%20common%20way%20to%20weave%20manually%20written%20code%20into%20generated%20software%20artifacts.%20While%20this%20pattern%20describes%20how%20to%20structure%20your%20classes%20logically%20it%20does%20not%20teach%20you%20where%20to%20place%20these%20classes%20physically%20as%20files%20into%20the%20filesystem.%0D%0A%0D%0A%0D%0A%0D%0ASeparating%20generated%20fi" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Generation+Gap+Pattern+-+http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;title=Generation+Gap+Pattern" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;title=Generation+Gap+Pattern" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.mister-wong.com/addurl/?bm_url=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;bm_description=Generation+Gap+Pattern&amp;plugin=sexybookmarks" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/&amp;title=Generation+Gap+Pattern" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.heikobehrens.net/2009/04/23/generation-gap-pattern/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
