<?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>Peter Friese &#187; DSLs</title>
	<atom:link href="http://www.peterfriese.de/category/dsls/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.peterfriese.de</link>
	<description>Xtext, DSLs, Eclipse, iPhone</description>
	<lastBuildDate>Tue, 27 Jul 2010 21:36:19 +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>Xtext Tricks #1: Enhancing Completion Proposals</title>
		<link>http://www.peterfriese.de/xtext-tricks-1-enhancing-completion-proposals/</link>
		<comments>http://www.peterfriese.de/xtext-tricks-1-enhancing-completion-proposals/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 14:55:43 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[MDSD]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=337</guid>
		<description><![CDATA[Those of you who follow me on Twitter might have noticed I am working on an Xtext based DSL for Behaviour Driven Development. Part of the DSL allows the DSL user to define actors and the verbs these actors can execute. Actors can have a hierarchy (much like a class hierarchy), meaning an actor will[...]]]></description>
			<content:encoded><![CDATA[<p>Those of you who <a href="htp://twitter.com/peterfriese">follow me on Twitter</a> might have noticed <a href="http://twitter.com/peterfriese/status/4889552469">I am working on an Xtext based DSL</a> for <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">Behaviour Driven Development</a>. Part of the DSL allows the DSL user to define actors and the verbs these actors can execute. Actors can have a hierarchy (much like a class hierarchy), meaning an actor will inherit all verbs of it's super actors. As the list of verbs can grow quite a bit, the content assist drop down menu becomes a bit overwhelming.</p>
<p><a href="http://www.flickr.com/photos/81029262@N00/4029808851" title="View 'XtextProposalProviderBefore' on Flickr.com">
<div style="text-align:center;"><img src="http://farm3.static.flickr.com/2581/4029808851_22fd514946.jpg" alt="XtextProposalProviderBefore" border="0" width="459" height="339" /></div>
<p></a></p>
<p>To alleviate  this situation, I decided to display the "owner" of a verb along with the name of the verb in the content assist drop down box. Here is my first try:<br />
<span id="more-337"></span></p>
<pre>
public class StoryDslProposalProvider extends AbstractStoryDslProposalProvider {
  @Override
  protected ICompletionProposal createCompletionProposal(EObject element, String proposal, String displayString, ContentAssistContext contentAssistContext)
  {
    if (element instanceof Verb) {
      Verb verb = (Verb) element;
      Subject owner = ((Subject)verb.eContainer());
      String myDisplayString = verb.getName() + ": " + owner.getName();
      return super.createCompletionProposal(element, proposal, myDisplayString, contentAssistContext);
    }
    return super.createCompletionProposal(element, proposal,  displayString, contentAssistContext);
  }
}
</pre>
<p>With this piece of code in place, the content assist proposals for verbs will now display the name of the "owner" to the right of the replacement text:</p>
<p><a href="http://www.flickr.com/photos/81029262@N00/4030575068" title="View 'XtextContentAssistAfter' on Flickr.com">
<div style="text-align:center;"><img src="http://farm4.static.flickr.com/3526/4030575068_2358208f22.jpg" alt="XtextContentAssistAfter" border="0" width="456" height="329" /></div>
<p></a></p>
<p><a href="http://zarnekow.blogspot.com/">Sebastian</a> pointed out that there is a <code>getDisplayString()</code> in <code>AbstractContentProposalProvider</code> that you should override in order to create a custom display string. It turned out, however, that this method will not be called as expected. I <a href="http://bugs.eclipse.org/bugs/show_bug.cgi?id=293380">filed a bug</a>, so expect to see a fix in one of the next milestones of the Xtext 0.8.0 stream.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/xtext-tricks-1-enhancing-completion-proposals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Everything You Always Wanted To Know About Modeling (But Were Afraid To Ask)</title>
		<link>http://www.peterfriese.de/everything-you-always-wanted-to-know-about-modeling-but-were-afraid-to-ask/</link>
		<comments>http://www.peterfriese.de/everything-you-always-wanted-to-know-about-modeling-but-were-afraid-to-ask/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 20:18:52 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[Canada]]></category>
		<category><![CDATA[DSLs]]></category>
		<category><![CDATA[EMF]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[MDSD]]></category>
		<category><![CDATA[USA]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=333</guid>
		<description><![CDATA[
Does Model Driven Software Development actually work?
Are some developers more productive than others?
What happens during code generation?
Do diagrams and text go together well?
What happens during model transformation?
What are DSLs? And can I create them with Eclipse?
Does modeling scale in the enterprise?

You guessed it, these are not the questions from a well-know Woody Allen movie, but[...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Does Model Driven Software Development actually work?</li>
<li>Are some developers more productive than others?</li>
<li>What happens during code generation?</li>
<li>Do diagrams and text go together well?</li>
<li>What happens during model transformation?</li>
<li>What are DSLs? And can I create them with Eclipse?</li>
<li>Does modeling scale in the enterprise?</li>
</ol>
<p>You guessed it, these are not the questions from a <a href="http://en.wikipedia.org/wiki/Everything_You_Always_Wanted_to_Know_About_Sex*_(*But_Were_Afraid_to_Ask)">well-know Woody Allen movie</a>, but questions that will be answered at the <a href="http://wiki.eclipse.org/Eclipse_Modeling_Day">Eclipse Modeling Days</a> which will be held in <a href="http://wiki.eclipse.org/Eclipse_Modeling_Day#New_York_City">New York</a> and <a href="http://wiki.eclipse.org/Eclipse_Modeling_Day#Toronto">Toronto</a> in November.<br />
<span id="more-333"></span><br />
So if you are wondering why everybody in Europe is so thrilled about Modeling, attend to one of these events - you'll get the answers there. </p>
<p>For your convenience, here is a list of some of the talks:</p>
<ul>
<li>Case Study: Model Transformations - Richard Woods, Credit Suisse</li>
<li>Building DSLs with Xtext - Heiko Behrens, itemis</li>
<li>Modeling on an enterprise scale - Eike Stepper, Eclipse Modeling Project</li>
<li>Query, Transaction and Validation - Bernd Kolb & Boris Gruschko, both SAP</li>
<li>Agent-Based Computer Simulation for Operational Risk Analysis - Ed MacKerrow, Los Alamos Labs</li>
<li>Papyrus: Advent of an Open Source IME - Kenn Hussey, Zeligsoft</li>
<li>B3: A Model for Unbreakable Builds</li>
<li>There's a Modeling Solution for That - Bruce Trask, MDE Systems & Angel Roman, MDE Systems</li>
</ul>
<p>If you ask me, this is a pretty decent line-up, both content-wise and people-wise. Not being one of the big conferences, the Modeling Days will actually give you the chance to talk to the experts and other end-users. Remember, networking is key.</p>
<p>So go ahead and register by adding your name to <a href="http://wiki.eclipse.org/Eclipse_Modeling_Day">this wiki page</a>. Registration is free, but you need to register in order to help the organizers to plan the catering. If you don't have an account for editing the wiki page or don't want to create one, just send mail to <a href="mailto:eclipse_modeling_nyc@eclipse.org">eclipse_modeling_nyc@eclipse.org</a> or <a href="mailto:eclipse_modeling_toronto@eclipse.org">eclipse_modeling_toronto@eclipse.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/everything-you-always-wanted-to-know-about-modeling-but-were-afraid-to-ask/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Xtext @ Google</title>
		<link>http://www.peterfriese.de/xtext-google/</link>
		<comments>http://www.peterfriese.de/xtext-google/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 12:46:02 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[MDSD]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=307</guid>
		<description><![CDATA[Last week I had the chance to speak at the EclipseDay at the GooglePlex in Mountain View, CA, an event organized by the Eclipse Foundation and hosted by Google's Open Source Programs Office.  Google is a truly amazing company and the GooglePlex is a very exciting place to be at. As I was one[...]]]></description>
			<content:encoded><![CDATA[<p>Last week I had the chance to speak at the <a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009">EclipseDay at the GooglePlex</a> in <a href="http://maps.google.com/?q=Google+Inc.%4037.423156,-122.084917&amp;hl=en&amp;ie=UTF8&amp;ll=37.42216,-122.085764&amp;spn=0.005283,0.011362&amp;t=h&amp;z=17">Mountain View, CA</a>, an event organized by the <a href="http://www.eclipse.org">Eclipse Foundation</a> and hosted by <a href="http://code.google.com/opensource/">Google's Open Source Programs Office</a>.  Google is a truly amazing company and the GooglePlex is a very exciting place to be at. As I was one day early and needed a creative place to put finishing touches on my slides, I took the chance to visit <a href="http://konigsberg.blogspot.com/">Robert Konigsberg</a> who was kind enough to host me for that day.  The organizers put together a great agenda with lots of interesting talks. <a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts">Looking at the agenda</a>, you will see that there actually are three major topics, <strong>Eclipse in the Enterprise</strong>, <strong>Modeling</strong> and <strong>Runtime</strong>:  The talks</p>
<ul>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Eclipse_in_the_Enterprise:_Lessons_from_Google">Eclipse in the Enterprise: Lessons from Google</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Distributed_OSGi_in_the_Eclipse_Runtime_Project">Distributed OSGi</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Deploying_Successful_Enterprise_Tools">Deploying Successful Enterprise Tools</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Build_and_Provision:_Two_Sides_of_the_Coin_We_Love_to_Hate">Build and Provision</a></li>
</ul>
<p>all dealt with how Eclipse can be used in the Enterprise or which challenges you will face if you start using Eclipse in large-scale scenarios.<br />
<span id="more-307"></span><br />
The talks</p>
<ul>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Developing_DSLs_with_Eclipse">Developing DSLs with Eclipse</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Eclipse_in_NASA_Mission_Control">Eclipse in NASA Mission Control</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Build_and_Provision:_Two_Sides_of_the_Coin_We_Love_to_Hate">Build and Provision</a></li>
</ul>
<p>either directly had modeling as their topic or used modeling technology to solve their problems. I especially liked the NASA talk which featured a number of computer animated videos of recent or future missions. The NASA mission control software is heavily based on Eclipse and technology form the Eclipse Modeling Project.  Finally, there were some talk on runtime technology:</p>
<ul>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#OSGi_for_Eclipse_Developers">OSGi for Eclipse Developers</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Developing_for_Android_with_Eclipse">Developing for Android with Eclipse</a></li>
<li><a href="http://wiki.eclipse.org/Eclipse_Day_At_Googleplex_2009/Session_Abstacts#Google_Plugin_for_Eclipse:_Not_Just_for_Newbies_Any_More">Google Plugin for Eclipse: Not Just for Newbies Any More</a></li>
</ul>
<p>Attending great talks of course is one of the major benefits of going to a conference like this. To me, meeting other people and connecting has become even more important than attending great talks. Of course, this is nothing new to event organizers and so there were many chances to grab a drink and chat with the other attendees. With more than 170 people attending, it was hard to have a chat with everyone, but nevertheless I managed to talk to a number of people, many of which already are Xtext users or are now planing to have a detailed look at it. Even Google is using Xtext, but ssshh!  Overall, I really enjoyed my two days at Google and look forward to more events like this. Speaking of which, <a href="http://www.eclipsecon.org/summiteurope2009/">Eclipse Summit Europe is approaching fast</a>, so make sure you register on time. Also, if you're interested in Modeling and DSLs, we're organizing a Modeling / DSL Day in North America this coming fall - see <a href="http://www.peterfriese.de/eclipse-modeling-day-in-north-america/">my previous post</a> for more info.  And finally, here are the slides and the video of my talk:</p>
<div><object style="margin:0px" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=buildingdslswitheclipseslideshare-key-090827153422-phpapp02&amp;stripped_title=building-dsls-with-eclipse-1916333" /><param name="allowfullscreen" value="true" /><embed style="margin:0px" type="application/x-shockwave-flash" width="480" height="385" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=buildingdslswitheclipseslideshare-key-090827153422-phpapp02&amp;stripped_title=building-dsls-with-eclipse-1916333" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/bhdGNTm4PzI&amp;hl=en&amp;fs=1&amp;color1=0x234900&amp;color2=0x4e9e00" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube-nocookie.com/v/bhdGNTm4PzI&amp;hl=en&amp;fs=1&amp;color1=0x234900&amp;color2=0x4e9e00" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/xtext-google/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Eclipse Modeling Day in North America</title>
		<link>http://www.peterfriese.de/eclipse-modeling-day-in-north-america/</link>
		<comments>http://www.peterfriese.de/eclipse-modeling-day-in-north-america/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 17:22:41 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[MDSD]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=302</guid>
		<description><![CDATA[We (i.e. the Eclipse Foundation, itemis and Cloudsmith) are looking into arranging one more Eclipse Modeling Day in North America this fall and would like to gather feedback from folks interested in attending. Please use this poll to help us tailor a location and program to suit your particular interests:
 http://spreadsheets.google.com/viewform?hl=en&#38;formkey=dF ZQcGh3RDhrUUdyNTAtMXlVU2pKQ1E6MA..

Our goal is to[...]]]></description>
			<content:encoded><![CDATA[<p><span class="MsgBodyText">We (i.e. the Eclipse Foundation, <a href="http://www.itemis.com">itemis</a> and Cloudsmith) are looking into arranging one more Eclipse Modeling Day in North America this fall and would like to gather feedback from folks interested in attending. Please use this poll to help us tailor a location and program to suit your particular interests:</span></p>
<p><a href="http://spreadsheets.google.com/viewform?hl=en&amp;formkey=dFZQcGh3RDhrUUdyNTAtMXlVU2pKQ1E6MA.." target="_blank"> http://spreadsheets.google.com/viewform?hl=en&amp;formkey=dF ZQcGh3RDhrUUdyNTAtMXlVU2pKQ1E6MA..</a><br />
<span id="more-302"></span><br />
Our goal is to organize an event especially for people new to modeling or who are curious what this modeling fuss is all about and how it can help them. So, this will not be an experts meeting, but you can meet experts talking about their topics. We also are interested in case studies, so ideally you will get first-hand experience from people who use modeling in real life to meet real challenges.</p>
<p>We welcome any feedback, so please fill out our little poll.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/eclipse-modeling-day-in-north-america/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting started with Xtext, part 2</title>
		<link>http://www.peterfriese.de/getting-started-with-xtext-part-2/</link>
		<comments>http://www.peterfriese.de/getting-started-with-xtext-part-2/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 20:48:56 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[english]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=298</guid>
		<description><![CDATA[Last week, I showed you how easy it is to create a DSL with Xtext. In this installment, we will have a look at how to leverage the models created with the DSL.
Goal
Let's imagine we want to create an application for orders. People can sign in to the system, place orders for various items, check[...]]]></description>
			<content:encoded><![CDATA[<p>Last week, <a href="http://www.peterfriese.de/getting-started-with-xtext/">I showed you how easy</a> it is to create a DSL with <a href="http://www.xtext.org">Xtext</a>. In this installment, we will have a look at how to leverage the models created with the DSL.</p>
<h3>Goal</h3>
<p>Let's imagine we want to create an application for orders. People can sign in to the system, place orders for various items, check out and have them sent to their address. Very simple, but we can show a lot of things here.</p>
<p>As we expect to be writing more than one application of this type and as we also would like to be able to express the structure of the application on a business level (one of the major drivers for DSLs and MDSD for that matter), we come up with the idea of using a DSL to describe what the application does. Defining the DSL is what we did last week. This week, we need to map the concepts of the DSL to some code and some APIs we're going to program against.<br />
<span id="more-298"></span><br />
So, we're going to create a set of code templates for a code generator that can then read our DSL models and create persistence code for us.</p>
<h3>Prepare</h3>
<p>In order to draw any benefit from this post, you need to have a running version of the DSL we created last time round. If you're new to this series, please go back one week and follow along the steps outlined in last week's article. It shouldn't take you longer than 30 minutes, and I promise we'll wait here for you.</p>
<h3>Create a main template</h3>
<p>As we're probably going to create more than one kind of class (we'll have Entity beans, DAOs and maybe some interfaces), it is a wise idea to separate all those templates into separate files. The code generator then needs to invoke all those template files in order to generate our application. To make things more maintainable, every code generator should have a main entry point, so let's create a main template file which serves as this main entry point.</p>
<p>First, please <em>delete</em> everything in <em>org.xtext.example.entity.generator/templates</em>. Then, create a new <em>Xpand</em> template in this directory by selecting <em>File -> New -> Other ... -> Xpand Template</em>. This is going to be our main template, so let's call it <em>Main.xpt</em>. An empty editor will open.</p>
<p>Insert the following line:</p>
<pre>«IMPORT entity»</pre>
<p>In case you wonder how to insert the special quotation marks, use code completion (press <em>CTRL + Space</em>, or <em>CMD + Space</em> on a Mac) to insert them. Upon activating code completion, a dialog will appear, asking you whether to add the <em>Xpand</em> nature to the project. Please confirm this dialog.</p>
<p>The <em>IMPORT</em> statement imports the metamodel of your DSL, thereby making it known to Xpand, which will enable us to use the metamodel in our templates.</p>
<p>Next, add the following lines:</p>
<pre>
«DEFINE main FOR Model»
  «EXPAND Entity::entity FOREACH this.types.typeSelect(Entity)»
«ENDDEFINE»
</pre>
<p>This will add a new template called <em>main</em> to your template file. By specifying <em>FOR Model</em>, we specify that this template will later be bound to a variable representing your model. The <em>EXPAND...FOREACH</em> statement will invoke a template called <em>entity</em> in a template file called <em>Entity</em> for all elements of the collection <em>types</em> in your model. Remember, your model contains a number of entities and type definitions. These are stored in the collection <em>types</em>. The <em>typeSelect(Entity)</em> statement makes sure the generator only considers elements which are of type <em>Entity</em> (we do not want to create Java beans for the plain data types).</p>
<h3>Create a template for Java beans</h3>
<p>Every data-oriented application needs a bunch of classes to hold the data. Usually referred to as <em>entities</em>, these classes are POJOs in our case. So, let's now create a template which helps us to create POJOs from our model.</p>
<p>Please add another <em>Xpand template</em> to your project by selecting <em>File -> New -> Other ... -> Xpand Template</em>. Name the new file <em>Entity.xpt</em>, making sure to save it to the same folder as <em>Main.xpt</em>.</p>
<p>Again, start the file by importing the meta model:</p>
<pre>
«IMPORT entity»
</pre>
<p>Next, add the following lines:</p>
<pre>
«DEFINE entity FOR entity::Entity»
  «FILE this.name + ".java"»
    public class «this.name» {
      «EXPAND attribute FOREACH this.attributes»
    }
  «ENDFILE»
«ENDDEFINE»
</pre>
<p>This template will create a new Java file for each entity instance it is invoked for. This is achieved by the <em>FILE</em> statement, which takes the name of the output file as it's first parameter.</p>
<p>As you can guess, we also need a template to create the attributes, so please add the following lines:</p>
<pre>
«DEFINE attribute FOR Attribute»
  private «this.type.name» «this.name»;

  public void set«this.name.toFirstUpper()»(«this.type.name» «this.name») {
    this.«this.name» = «this.name»;
  }

  public «this.type.name» get«this.name.toFirstUpper()»() {
    return this.«this.name»;
  }
«ENDDEFINE»
</pre>
<p>This template will create a private field, a setter and a getter for each attribute it is invoked for.</p>
<h3>Create a template for a DAO</h3>
<p>To load entities from a database and save them back, we will need to write some entities, but again, this is something the generator can do for us, so let's write a template:</p>
<p>Create a template <em>DAO.xpt</em> and insert this text:</p>
<pre>
«IMPORT entity»

«DEFINE dao FOR entity::Entity»
  «FILE this.name + "DAO.java"»
    import java.util.Collection;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    public class «this.name»DAO
      extends HibernateDaoSupport {
      «EXPAND crud FOR this»
    }
  «ENDFILE»
«ENDDEFINE»

«DEFINE crud FOR Entity»
  public «this.name» load(Long id) {
    return («this.name»)getHibernateTemplate().get(«this.name».class, id);
  }

  @SuppressWarnings("unchecked")
  public Collection<«this.name»> loadAll() {
    return getHibernateTemplate().loadAll(«this.name».class);
  }  

  public «this.name» create(«this.name» entity) {
    return («this.name») getHibernateTemplate().save(entity);
  }

  public void update(«this.name» entity) {
    getHibernateTemplate().update(entity);
  }

  public void remove(«this.name» entity) {
    getHibernateTemplate().delete(entity);
  }
«ENDDEFINE»
</pre>
<p>Do not forget to invoke this template from the master template file <em>Main.xpt</em>:</p>
<pre>
«IMPORT entity»

«DEFINE main FOR Model»
	«EXPAND Entity::entity FOREACH this.types.typeSelect(Entity)»
	«EXPAND DAO::dao FOREACH this.types.typeSelect(Entity)»
«ENDDEFINE»
</pre>
<h3>Create a model</h3>
<p>In your workspace, open <em>org.xtext.example.entity.generator/src/model/MyModel.entity</em>, making sure it contains the following model:</p>
<pre>
typedef String
typedef Integer
typedef Double
typedef Date mapsto java.util.Date

entity Customer {
  String firstName
  String lastName
  String email
  Address shippingAddress
  Order* orders
}

entity Address {
  String line1
  String line2
  String city
  String postcode
  String state
  String country
}

entity Product {
  String description
  Double price
}

entity Book extends Product {
  String ISBN
  String author
}

entity Order {
  Date orderDate
  OrderLine* orderLines
}

entity OrderLine {
  Product item
  Integer amount
}
</pre>
<h3>Setting up the generator</h3>
<p>Before we can actually start the generator and transform our model, we need to adjust some aspects of the generator project.</p>
<p>First, please open the generator workflow in <em>/org.xtext.example.entity.generator/src/workflow/EntityGenerator.mwe</em>. We need to change the name of the root template so it matches the name of the root template we chose. We also would like the generator to format the output properly, so we're going to add an output beautifier.</p>
<p>Change the generator component in the workflow file to match the following:</p>
<pre>
  &lt;component class="org.eclipse.xpand2.Generator"&gt;
    &lt;metaModel class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/&gt;
    &lt;fileEncoding value="MacRoman"/&gt;
    &lt;expand value="templates::Main::main FOR model"/&gt;
    &lt;genPath value="src-gen"/&gt;
    &lt;beautifier class="org.eclipse.xpand2.output.JavaBeautifier"/&gt;
  &lt;/component&gt;
</pre>
<p><strong>Note:</strong> you need to leave the <em>fileEncoding</em> attribute as-is. The above fragment is valid only on a Mac.</p>
<p>As the formatter is based on the Eclipse java formatter (see also <a href="http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/">this post on the Eclipse code formatter</a> which I wrote some years ago and which still drives 200 reader/day to my blog), we have to add some bundle dependencies to the generator project.</p>
<p>Do to so, please open <em>/org.xtext.example.entity.generator/META-INF/MANIFEST.MF</em>, go to the <em>Dependencies</em> and add the following bundles:</p>
<ul>
<li>org.eclipse.jface.text</li>
<li>org.eclipse.core.runtime</li>
<li>org.eclipse.jdt.core</li>
<li>org.eclipse.xtext.log4j</li>
<li>com.ibm.icu</li>
</ul>
<h3>Running the generator</h3>
<p>To run the generator, simply select the workflow file <em>/org.xtext.example.entity.generator/src/workflow/EntityGenerator.mwe</em> and choose <em>Run as... -> MWE Workflow</em> from the context menu. You'll see a bunch of log messages in the console view, and after a few seconds, you'll find a bunch of just generated files in <em>/org.xtext.example.entity.generator/src-gen</em>.</p>
<h3>Using the DSL to drive the generator</h3>
<p>In the above sample, we wrote the DSL model by hand (well, you probably just copied it from this post), but this certainly is not the normal usage pattern. So, how can you use the DSL editor to create models that are then being read by the generator?</p>
<p>Actually, it is very easy if you follow these steps:</p>
<ol>
<li>Start the DSL in a runtime workbench (refer to the last installment to see how)</li>
<li>In the runtime workspace, create a new <em>Eclipse Plug-in Project</em>. Choose any name you like, e.g. <em>org.xtext.example.entity.mymodel</em>. Make sure you deselect <em>Generate an activator...</em> on the second page of the wizard - we don't need one.</li>
<li>Add all of the following dependencies to the newly created project's manifest file:
<ul>
<li>org.xtext.example.entity</li>
<li>org.eclipse.xpand</li>
<li>org.eclipse.xtend</li>
<li>org.eclipse.xtext</li>
<li>org.eclipse.emf.mwe.core</li>
<li>org.eclipse.emf.mwe.utils</li>
<li>org.eclipse.xtend.typesystem.emf</li>
<li>org.eclipse.jface.text</li>
<li>org.eclipse.core.runtime</li>
<li>org.eclipse.jdt.core</li>
<li>org.eclipse.xtext.log4j</li>
<li>com.ibm.icu</li>
<li>org.xtext.example.entity.generator</li>
</ul>
</li>
<li>Create a new workflow file in <em>/org.xtext.example.entity.mymodel/src/workflow/EntityGenerator.mwe</em> and insert the following text:
<pre>
&lt;workflow>
  &lt;cartridge file="workflow/EntityGenerator.mwe" model="classpath:/model/MyModel.entity"/&gt;
&lt;/workflow&gt;
		</pre>
</li>
<li>Patch the workflow file in the generator: Go back to the original workspace and change the line containing <em>&lt;component class="org.eclipse.xtext.MweReader" uri="...</em> to <em>&lt;component class="org.eclipse.xtext.MweReader" uri="${model}"&gt;</em>
	</li>
<li>Go back to the runtime workspace and create a new file <em>MyModel.entity</em> in <em>/org.xtext.example.entity.mymodel/src/model/MyModel.entity</em>.
</ol>
<p>You can now use the DSL editor to create your model. The generator can be started by running the generator workflow file <em>EntityGenerator.mwe</em> in your newly created model project <em>/org.xtext.example.entity.mymodel</em> in the runtime workspace.</p>
<h3>Conclusion</h3>
<p>I hope the last section didn't bother you too much. The good news is that we're going to provide a wizard which will help you setting up DSL model projects - see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=281214">bug 281214</a>.</p>
<p>Due to space limitations, this example is not complete in a sense that you can generate a fully functional CRUD application from it. You should, however be able to see what's possible with textual DSLs and code generation.</p>
<p>If you're interested in seeing the whole generator, drop me a line (peter dot friese [at] itemis dot de) or DM me on Twitter (my Twitter ID is @peterfriese).</p>
<h3>Download the code</h3>
<p><a href="http://code.google.com/p/peterfriese/source/browse/xtext_getting_started/tags/xtext_getting_started_2/">Download the code</a> from my SVN repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/getting-started-with-xtext-part-2/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Getting Started with Xtext</title>
		<link>http://www.peterfriese.de/getting-started-with-xtext/</link>
		<comments>http://www.peterfriese.de/getting-started-with-xtext/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 22:17:56 +0000</pubDate>
		<dc:creator>Peter Friese</dc:creator>
				<category><![CDATA[DSLs]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[english]]></category>

		<guid isPermaLink="false">http://www.peterfriese.de/?p=284</guid>
		<description><![CDATA[Xtext has been released as a part of the Eclipse Galileo release train on June 24th, 2009. Xtext is a framework for building DLSs (domain specific languages). In fact, it can be seen as a DSL for defining DSLs. 
In this article, we will develop a small DSL for defining entities. 
Download and Install
Hop over[...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.xtext.org">Xtext</a> has been released as a part of the Eclipse Galileo release train on June 24th, 2009. Xtext is a framework for building DLSs (<b>d</b>omain <b>s</b>pecific <b>l</b>anguages). In fact, it can be seen as a DSL for defining DSLs. </p>
<p>In this article, we will develop a small DSL for defining entities. </p>
<h2>Download and Install</h2>
<p>Hop over to <a href="http://xtext.itemis.com">http://xtext.itemis.com</a> and download an Xtext distribution matching your platform. We've got all major platforms: Windows, Mac OSX Carbon, Mac OSX Cocoa 64, Mac OSX Cocoa 32, Linux Gtk 64, Linux Gtk 32.</p>
<p>To install, unzip the distribution file to a directory of your liking. </p>
<p><b>Windows users, please make sure you unzip to a directory near to your filesystem root! Eclipse contains files and folders with long names that might be hard to handle for Windows.</b><br />
<span id="more-284"></span></p>
<h2>Create a new project</h2>
<p>After launching Eclipse and creating a new workspace (or using an existing one), create a new Xtext project by selecting <em>File -> New... Project... -> Xtext Project</em>. In this article, we're creating a DSL for describing entities, so let's go with the following settings:</p>
<ul>
<li>Main project name: <em>org.xtext.example.entity</em></li>
<li>Language name: <em>org.xtext.example.Entity</em></li>
<li>DSL-File extension: <em>entity</em></li>
<li>Create generator project: <em>yes</em></li>
</ul>
<p>Click <em>Finish</em> to let Xtext create the three projects that make up your DSL:</p>
<ul>
<li>org.xtext.example.entity - this project contains the DSL itself, including all back end infrastructure like the parser and the meta model.</li>
<li>org.xtext.example.entity.ui - as the name implies, this project contains all UI centric code of the DSL: the editor, the outline, content assist and so forth</li>
<li>org.xtext.example.entity.generator - this project contains a code generator which will transform the DSL scripts (aka models) you write in your DSL into something useful. In our example, we will create some POJOs and DAOs from our models.</li>
</ul>
<p>Upon finishing creating these three projects, Xtext opens a file <em>Entity.xtext</em>, which contains a sample grammar, which we will change in a second.</p>
<h2>Define the grammar for your DSL</h2>
<p>Next up, we need to define the grammar for our DSL. To make things easier for us, let's first write down a sample model. Please open <em>org.xtext.example.entity.generator/src/model/MyModel.entity</em> and key in the following text:</p>
<pre>
typedef String
typedef Integer
typedef Date mapsto java.util.Date

entity Person {
  String name
  String surName
  Date birthDay
  Address home
  Address work
}

entity Boss extends Person {
  Person* employees
}

entity Address {
  String street
  String number
  String city
  String ZIP
}
</pre>
<p>Most of this model should be pretty obvious, but there are two things worth noting:</p>
<ul>
<li>We define our own list of data types to gain a certain degree of flexibilty, i.e. to map them to different types in the host language, as can be seen for the <em>Date</em> data type, which gets mapped to <em>java.util.Date</em> (we might also decide to map it to <em>java.sql.Date</em>)</li>
<li>The star (*) denotes multiplicity. We might also have chosen square brackets (<em>Person[] employees</em>) or something completely different - it's largely a matter of taste.</li>
</ul>
<p>Let's derive the grammar for this model. Open <em>org.xtext.example.entity/src/org/xtext/example/Entity.xtext</em>, erase its entire contents and enter the following:</p>
<pre>
grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals
generate entity "http://www.xtext.org/example/Entity"
</pre>
<p>The first line indicates we want the new grammar to be derived from the (built-in) grammar <em>Terminals</em>, which defines some basic terminal rules (like STRING, ID and so forth). If you're interested, <em>CTRL+Click</em> on the language name to navigate to its definition.</p>
<p>The second line defines the name and namespace URI for our own grammar.</p>
<p>Let's now define that our DSL supports types. Add the following lines:</p>
<pre>
Model:
  (types+=Type)*;

Type:
  TypeDef | Entity;
</pre>
<p>This tells Xtext that our <em>Model</em> contains any number (i.e. <em>0..N</em>, as declared by the <em>*</em>) of <em>Type</em>s. What exactly a <em>Type</em> is needs to be specified. Apparently, a <em>Type</em> can be either a <em>TypeDef</em> or an <em>Entity</em>:</p>
<pre>
TypeDef:
  "typedef" name=ID ("mapsto" mappedType=JAVAID)?;
</pre>
<p>A <em>TypeDef</em> starts with the keyword <em>typedef</em>, followed by an <em>ID</em> making up its name. Following the name, we can optionally (hence the question mark at the end) add a <em>mapsto</em> clause. The fragment <em>mappedType=JAVAID</em> specifies that the <em>TypeDef</em> will later have an attribute named <em>mappedType</em> of type JAVAID. As JAVAID is not yet defined, we need to do so:</p>
<pre>
JAVAID:
  name=ID("." ID)*;
</pre>
<p>So, a <em>JAVAID</em> is a sequence of <em>ID</em>s and dots, making up a qualified Java type name, such as <em>java.util.Date</em>.</p>
<p>Next, let's define how to model entities:</p>
<pre>
Entity:
  "entity" name=ID ("extends" superEntity=[Entity])?
  "{"
    (attributes+=Attribute)*
  "}";
</pre>
<p>As you might have guessed, <em>Entity</em>s start with the keyword <em>entity</em>, followed by an <em>ID</em> as their name. They may optionally extends another entity. Surrounding a rule call with square brackets means "this is a cross reference", i.e. a reference to an already existing element.</p>
<p><em>Entity</em>s do have <em>Attribute</em>s (any number, to be precise), thus we have to define how <em>Attribute</em>s look like:</p>
<pre>
Attribute:
  type=[Type] (many?="*")? name=ID;
</pre>
<p>By now, you should be able to read this rule: an <em>Attribute</em> has a <em>type</em> which is a cross reference to a <em>Type</em> (which is either a <em>TypeDef</em> or an <em>Entity</em>), it has an optional multiplicity indicator (the star) and - of course - if has a name.</p>
<p>Your grammar should look like this by now:</p>
<pre>
grammar org.xtext.example.Entity with org.eclipse.xtext.common.Terminals

generate entity "http://www.xtext.org/example/Entity"

Model:
  (types+=Type)*;

Type:
  TypeDef | Entity;

TypeDef:
  "typedef" name=ID ("mapsto" mappedType=JAVAID)?;

JAVAID:
  name=ID("." ID)*;

Entity:
  "entity" name=ID ("extends" superEntity=[Entity])?
  "{"
    (attributes+=Attribute)*
  "}";

Attribute:
  type=[Type] (many?="*")? name=ID;
</pre>
<h2>Compiling the DSL</h2>
<p>Now it is time to see the fruits of our labor. But first, we need to compile our grammar. Xtext will create:</p>
<ul>
<li>a parser</li>
<li>a serializer</em>
<li>an Ecore meta model</li>
<li>a full blown Eclipse editor</li>
</ul>
<p>from this grammar. To make this happen, please select <em>org.xtext.example.entity/src/org/xtext/example/GenerateEntity.mwe</em> and select <em>Run As -> MWE Workflow</em> from the context menu. Xtext will now generate the entire infrastructure for your DSL and after a few seconds you should have a shiny new DSL including a great editor.</p>
<h2>Taking it for a spin</h2>
<p>Seeing is believing, so let's take the DSL editor for a test drive. Select the DSL project <em>org.xtext.example.entity</em> and, from the context menu, select <em>Run As -> Eclipse Application</em>. A second instance of Eclipse will be started.</p>
<p>In this new instance, create a new, empty project (<em>File -> New -> Project... -> General -> Project</em>. Create a new file <em>Sample.entity</em> in the new project.</p>
<p>You can now insert the model we designed above or enter a new model:<br />
<a href="http://www.flickr.com/photos/81029262@N00/3673405506" title="View 'Getting Started With Xtext' on Flickr.com">
<div style="text-align:center;"><img src="http://farm3.static.flickr.com/2574/3673405506_09fed234e1.jpg" alt="Getting Started With Xtext" border="0" width="500" height="411" /></div>
<p></a></p>
<h2>Leveraging the model</h2>
<p>Now that we've got a fancy editor for our DSL, we want to transform the models we can create with this editor into something meaningful. This, however, will be the topic of the next installment.</p>
<h2>More info</h2>
<p>Feel free to discuss this article in the comments section of <a href="http://www.peterfriese.de">my blog</a>. Should you have any technical questions regarding Xtext, we've got an <a href="http://www.eclipse.org/newsportal/thread.php?group=eclipse.modeling.tmf">excellent newsgroup</a> where the committers answer your questions. We (itemis) also offer <a href="http://xtext.itemis.com/xtext/language=en/23946/professional-services">professional (commercial) support</a>, i.e. customized trainings to get your team up to speed. Just <a href="mailto:peter.friese@itemis.de">drop us a note</a>, we're happy to discuss the details with you.</p>
<h2>Download the code</h2>
<p>You can check out the code from <a href="http://code.google.com/p/peterfriese/source/browse/xtext_getting_started/tags/xtext_getting_started_1/">this SVN repository location</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.peterfriese.de/getting-started-with-xtext/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
