Getting started with Xtext, part 2
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 out and have them sent to their address. Very simple, but we can show a lot of things here.
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.
Read more
Getting Started with Xtext
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 to http://xtext.itemis.com 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.
To install, unzip the distribution file to a directory of your liking.
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.
Read more
How Twitter solved my P2 problems during my lunch break
Today, I wanted to migrate from Eclipse 3.5M5 to Eclipse 3.6M6 (I know, it's a bit late for that already - but then, I had been busy with #eclipsecon and preparing the Stupid Modeling Talk during the past weeks).
As of course I've got quite a number of additional bundles installed (e.g., EMF, Window Builder, Mylyn, Xpand and Xtend), I wanted to re-use them. I recalled that a while back someone (probably Chris?) blogged that it basically is possible to use an existing Eclipse installation as a local update site. However, I couldn't find the blog posting describing how to do that.
Given the fact it was about lunch time, I decided to let the #wisecrowd figure out how to solve my problem. So I twittered "desperately looking for a hint how to use an existing Eclipse install as a P2 repo. Goal:transport features from my M5 install to a fresh M6".
So, during my lunch break, the wise crowd (Kai, Ekke, Boris and Paul) sent me their opinions on the matter:
At first, it seemed like there is no solution, but in the end Paul's tweet reveals the solution I was looking for:
Add eclipse/p2/org.eclipse.equinox.p2.engine/profileRegistry/SDKProfile.profile/ as a local update site.
All of you out there who still doubt the practical use of Twitter: you really should give it another try.
Canada, eh!
So this is the last post I am writing in Canada for quite some time. I shall be heading back to Germany tonight on AC 876 - let's see if they've got disco lights again
It has been a packed time and I guess I met more people in these two months than I ever met before in a comparable period of time. Oh, did I tell you that I used to be a shy person? Well, at least I seem to have found ways to cope with that trait.
As I look back onto the past two months, here are some things that I really like about Canada:
- People generally seem to be more open than people in Germany. This might of course be influenced by the fact that everybody uses their first names when communicating. I also have gotten used to the little "how are you today" conversation at the beginning of each transaction. Let's see what happens when I try that on German shop assistants...
- Although (or maybe just because) it was winter when I came to Canada, I really like the landscape: the wide fields, the forests, the snow... Often, on my rides through Ontario, I would get the felling of somehow knowing how much greater it must be in summer.
- I also like the fact you can go shopping almost any hour of day, any day of the week. In fact, I was surprised to hear that there are some stores that are not open 24/7
- Turning right on a red light. Very time-saving, I wonder why we don't have this in Germany, as we're said to be oh so efficient...
The list of things I didn't like too much is much shorter:
- Tim Hortons coffee. Don't go there. It tastes like metal. Besides, they do not know what Coffee Latte is. And don't even try to get a Cappucino - it will be Vanilla-flavoured. Check out Second Cup instead - they have some decent coffee there and they know how to make a Latte.
There are a number of people that really have made my stay really memorable:
Thanks to Christian and Linda, Jeff and Nancy for so generously hosting me when I came to Ottawa.
Thanks to Ian, Mike and Wayne of the Eclipse Foundation for connecting me with so many people.
Thanks to Lynn for taking me on the Ottawa tour, that was a fun day out!

Thanks to Peter and Gisela Weidelich and the fine people at the Sunnyvale Baptist Church for giving me a spiritual home for the last few weeks.
Thanks to Simon and Anneta for spending Sunday before EclipseCon with me in San Francisco and for giving me a lift!

Most of all, thanks to Ed Merks for hosting me at his place in the probably most difficult time of his life. Ed, I owe you.

Oh, and thanks to everybody who reassured me that they couldn't figure out where I come from by just listening to my dialect
You all have given me a sense of being welcome to Canada. Although I am really looking forward to returning to Germany and seeing my family again, I am feeling like I am leaving a new family behind.
Modeling is dead. Long live Modeling!
Although some people seem to be convinced that Eclipse and Eclipse Modeling are dead, I have been sensing quite healthy life signals of both on EclipseCon 2009 (or rather #eclipsecon).
On Monday, Bernd, Kenn and I hosted the official Modeling BOF:

The topics discussed were focussing on consumability and long-term sustainability of EMF:
Documentation and Marketing
We all agreed that the entire modeling project lacks a decent documentation. While of course all of the projects and subprojects do have their own documentation, there is so such thing as an overview documentation. Modelers as we are, we quickly realized we're lacking a documentation superstructure. The idea is to provide a guide for users (newbies and seasoned experts alike) to navigate the Eclipse Modeling world. The modeling project offers a great variety of technologies, so it is not always easy to find out which set of technologies / frameworks you might want to use - especially if you are new to EMF.
We also agreed that we need more success stories (in the form of blog entries, articles and shiny flyers for the modeling landing page). A quick poll revealed that more than 90% of all people attending have been working on projects in which modeling was successfully applied, so there should be enough material to create success stories!
Kenn mentioned the Eclipse Foundation wants to start a documentation project (much like Babel) to improve overall documentation, we might eventually join this project. In the meantime, we decided to form a working group comprised of members of the Eclipse Modeling Project that starts to create this overview documentation. The initial members of this working group will be Kenn, Bernd, Peter, Tamer, Pierre, Marcello, Thibault and Anthony (guys, please send me your email addresses). The working group will start its work on the EMF dev mailing list. You are welcome to join us there.
Consuming Modeling Technology
I think there is no other technology that causes more emotions in an Eclipse committers life than P2. So we also had our little discussion about how to best provision Modeling technology. No big news here - we just re-iterated the pros and cons of using ZIPs vs. P2 and also had a short discussion about the Friends of Eclipse Download Wizard (which, rumor has it, will allow you to assemble a custom distro by just selecting the pieces you need). Several people mentioned that Amalgamation is intended to be a place for modeling distros.
EMF 3.0
We also discussed whether we should have a new major release of EMF that allows us to break API. Though not everybody agreed that we need a new major version (because the current version works smoothly for most people) we decided to set up an additional BOF to discuss this topic in more detail. I will write about the EMF 3 BOF later (yes, I am teasing)
Miscellanea
Someone asked if we need something like a model bus, the answer was no - we've got the modeling workflow engine (MWE).
We also briefly discussed the recent change of leadership on the MDT OCL component (Christian Damus revoked his committer and component lead status recently). Everybody agreed that to lead a component, a long-term committment is needed (which has been met by Christian in the most excellent way). We also agreed that we need to make avoid situations in which a whole bunch of projects / components suddenly are without a leader. This will be a task for the PMC.

After the official part of the BOF, there still was time to chat and shake hands with people you never met in person before. I had the pleasure to shake hands with Anthony Hunter, who works for IBM. From our discussions on mailing lists and bugs, you could guess that we have a totally different view on the marketing of software, but it turns out we're actually not that far apart
BOFs are a great way to interact with like-minded people at conferences. So, next time you go to a conference, make sure you participate in a BOF to make yourself be heard and take part in the discussion.
Using Teneo, EMF and Hibernate to update and query your data
Last week I showed you how to use Teneo, EMF and Hibernate to store your data in a database.
This week, we're going to have a look at how to update the data, add more records and how to query the database for information using HQL, the Hibernate Query Language.
If you haven't done so, you might consider taking last week's tutorial in order to get your environment set up and understand the basic concepts. If you're lazy, you can as well download the code of the tutorial to get started more quickly. It is available here.
As it turns out, in order to change anything in the library database, we must first fetch the to-be-changed data, so we need to have a look at querying first.
Retrieving and updating data
Let's assume we'd want to the update the number of pages in a book, because the new edition has an additional chapter.
- So, first of all we need to open a session and begin a transaction:
{ Session session = sessionFactory.openSession(); session.beginTransaction(); - Next, let's create an HQL query. We want to find any books that are written by an author by the name "A. K. Dewdney" that contain "Omnibus" in their title.
Query query = session.createQuery( "SELECT book from " + " Book book, " + " Writer writer " + "WHERE " + " book.title like '%Turing Omnibus%' " + "AND " + " writer.name = 'A. K. Dewdney'"); - We can now execute the query and display the results:
Listbooks = query.list(); Book book = books.get(0); System.out.println(book.getTitle()); - Updating the page count is pretty obvious:
book.setPages(520); - Finally, don't forget to commit the transaction and close the session:
session.getTransaction().commit(); session.close(); }
Adding data
Let's now assume we want to add more books (and authors) to the library.
- By now, you should be pretty familiar with the pattern of opening a new session:
{ Session session = sessionFactory.openSession(); session.beginTransaction(); - As we want to add new items to the library, we need to retrieve the library instance first of all:
Query query = session.createQuery("from Library"); Listlibraries = query.list(); Library library = libraries.get(0); - Creating a new book and its author is easy, as we just have to use the API EMF so kindly generated for us:
Writer writer = LibraryFactory.eINSTANCE.createWriter(); writer.setName("J.R.R. Tolkien"); Book book = LibraryFactory.eINSTANCE.createBook(); book.setTitle("The Hobbit"); book.setPages(320); book.setAuthor(writer); book.setCategory(BookCategory.MYSTERY); library.getBooks().add(book); library.getWriters().add(writer); - Finally, commit the transaction and close the session:
session.getTransaction().commit(); session.close(); }
Using Teneo and EMF to store your data
Most of you know that I am working as a committer for various Eclipse-related projects (such as Xtext, Xpand and the Modeling Workflow Engine). You might not know, however, that I also work as a consultant for itemis. On one of my recent consulting assignments in Ottawa, Canada, I was asked "How can we use EMF to store our data in a database?"
Well, it turns out EMF can help a long way to store data in a database. Here is how.
Prepare your development environment
- Get Eclipse 3.5 M5 (click here to download)
- Unpack and start Eclipse
- Bring up the "Install New Software" dialog (Help -> Install New Software)
- Select Teneo EMF Hibernate Runtime and Teneo EMF Hibernate SDK, version 1.0.3
- Cick Finish
- You most probably will be asked to restart Eclipse.
Create a target definition that includes Hibernate and HSQLDB
In order to keep things simple, we will store the data in an HSQLDB database. We will use the Hibernate OR Mapper to perform the mapping between your data objects and the database. As you might guess, quite a number of libraries will be involved to get the task accomplished. Instead of creating a bunch of plug-in projects containing the respective libraries, or - even worse - copying all libraries into our project, we'll set up a target definition. Target definitions help to maintain a common set of dependencies for all developers on a team, which is a Good Thing.
- Create a new project library.target (File -> New -> Project... -> General -> Project)
- Create three folders in this project: hibernate, dependencies, hsqldb
- Go to the SpringSource Bundle Repository and download the following OSGi bundles:
file save in folder com.springsource.org.hibernate-3.2.6.ga.jar library.target/hibernate com.springsource.org.apache.commons.logging-1.1.1.jar library.target/dependencies com.springsource.org.dom4j-1.6.1.jar library.target/dependencies com.springsource.org.apache.commons.collections-3.2.0.jar library.target/dependencies com.springsource.javax.transaction-1.1.0.jar library.target/dependencies com.springsource.antlr-2.7.7.jar library.target/dependencies com.springsource.org.hsqldb-1.8.0.9.jar library.target/hsqldb - Create a new target definition library.target in this project (File -> New -> Other... -> Plug-in Development -> Target Definition)
- Open the target definition and add the three directories to it's contents. As the GUI does not allow you to work with relative paths, you might consider to use a text editor to paste the following text:
<?xml version="1.0" encoding="UTF-8"?> <?pde version="3.5"?> <target description="Teneo-related stuff, mostly Hibernate" name="Library Target Definition"> <locations> <location path="${eclipse_home}" type="Profile"/> <location path="${workspace_loc}/library.target/hibernate" type="Directory"/> <location path="${workspace_loc}/library.target/dependencies" type="Directory"/> <location path="${workspace_loc}/library.target/hsqldb" type="Directory"/> </locations> </target> - Open the target definition in the Target Definition Editor and click on the Set as Target Platform hyperlink in the upper right area. This will activate the target definition. All contained bundles are now available and can be referenced as dependencies.
Create a model for your data
The data model will be based on the well-known library tutorial that ships with EMF. If you are interested in a more in-depth look, I recommend taking this tutorial. However, to shortcut things, here is the ultra-slim version of the EMF-Tutorial:
- Download the Rose class model and save it on your computer
- Create a new EMF project (File -> New -> Other... -> Eclipse Modeling Framework -> EMF Project)
- Project name: library
- Model importer: Rose class model
- Browse to the Rose class model library.mdl mentioned before
- Click on Load to load the model
- Click Next, then Finish
- In the library.genmodel editor, right-click on the Library node and select Generate Model Code
Create the library main application
In order to demonstrate how to use the data model and how to perform CRUD operations with your data, we will create a simple Java class. In the good spirit of encapsulation and components, we will create a new plug-in project to host this class:
- Create a new Plug-in project library.main (File -> New -> Project... -> Plug-in Project)
- Open the manifest and add the following dependencies:
- library (this is the bundle which contains our data model)
- org.eclipse.emf.teneo.hibernate
- org.eclipse.emf.ecore.xmi
- com.springsource.org.hibernate
- com.springsource.org.apache.commons.logging
- com.springsource.org.dom4j
- com.springsource.org.apache.commons.collections
- com.springsource.javax.transaction
- com.springsource.antlr
- com.springsource.org.hsqldb
- Create a new Hibernate configuration file hibernate.properties in library.main/src and paste the following lines:
hibernate.connection.driver_class=org.hsqldb.jdbcDriver # use the following line to run the embedded db: # hibernate.connection.url=jdbc:hsqldb:file:/some/path/on/your/computer/dbname # the following line will connect to a standalone (local) DB server: hibernate.connection.url=jdbc:hsqldb:hsql://127.0.0.1/library hibernate.connection.username=sa hibernate.connection.password= hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.hbm2ddl.auto=true
Implement the library main application
With all the boilerplate in place, we're finally ready to write some code. We will create a new class and add some code to create an author and his book and store both in a library.
- Create a new class LibraryDemo, making sure it has a main method
- In order to use Teneo to persist our data, we first need to create a datastore and register our model package with it:
// create the data store String dataStoreName = "LibraryDataStore"; HbDataStore dataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName); // register the model package with the data store dataStore.setEPackages(new EPackage[] { LibraryPackage.eINSTANCE }); // initialize the data store, which creates the tables dataStore.initialize(); - Next, we need to get hold of a session factory and request a new session form it:
SessionFactory sessionFactory = dataStore.getSessionFactory(); { Session session = sessionFactory.openSession(); session.beginTransaction(); - Now, let's create a new library and save it to the session:
// create a library Library library = LibraryFactory.eINSTANCE.createLibrary(); library.setName("Developer's bookshelf"); // store the library session.save(library); - In the following part, we will create an author and his book, link them to each other and add them to the library. There is no specific Teneo aspect to this part of the code, it is just straightforward usage of the API EMF generated for your datamodel:
// create an author Writer writer = LibraryFactory.eINSTANCE.createWriter(); writer.setName("A. K. Dewdney"); // create a book Book book = LibraryFactory.eINSTANCE.createBook(); book.setTitle("The New Turing Omnibus"); book.setPages(480); book.setCategory(BookCategory.MYSTERY); // oh well, let's hope it's not mystery to most readers! book.setAuthor(writer); // add book and writer to library library.getBooks().add(book); library.getWriters().add(writer); - Finally, we need to commit our changes to the database and close the session:
// commit changes to the database and close the session session.getTransaction().commit(); session.close(); }
Start the DB server and run the application
- Open a command line and navigate to the directory that contains hsqldb.jar
- Start the HSQLDB server using this command line:
java -cp com.springsource.org.hsqldb-1.8.0.9.jar org.hsqldb.Server -database.0 file:library -dbname.0 library
- Finally (!) go back to Eclipse and start LibraryDemo. You should get an output similar to this one:
Mar 6, 2009 1:50:26 PM org.eclipse.emf.teneo.hibernate.HbHelper createRegisterDataStore INFO: Creating emf data store and registering it under name: LibraryDataStore ... Mar 6, 2009 1:50:28 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: schema update complete
- To actually see the data stored in the database, navigate to the directory containing the database and open library.log:
CREATE USER SA PASSWORD "" ADMIN /*C1*/SET SCHEMA PUBLIC CONNECT USER SA ... INSERT INTO "library" VALUES(1,'Library',0,'Developer''s bookshelf') INSERT INTO "writer" VALUES(1,'Writer',0,'A. K. Dewdney',NULL,NULL,'Library','1',-2) INSERT INTO "book" VALUES(1,'Book',0,'The New Turing Omnibus',480,'Mystery',1,NULL,NULL,'Library','1',-3) DELETE FROM "writer" WHERE E_ID=1 INSERT INTO "writer" VALUES(1,'Writer',0,'A. K. Dewdney',1,0,'Library','1',-2) DELETE FROM "book" WHERE E_ID=1 INSERT INTO "book" VALUES(1,'Book',0,'The New Turing Omnibus',480,'Mystery',1,1,0,'Library','1',-3) INSERT INTO "writer_books" VALUES(1,1,0) COMMIT DISCONNECT
In the next installment, I will show you how to retrieve objects from the database and query the database using Hibernate Query Language (HQL).
Download the source
If you are interested in the source for the solution so far, you can download it here: (click to download).
Keyboard shortcuts for Apple Mail
I use Apple Mail on my MacBook Pro, mainly because it has a better integration with Spotlight than Thunderbird. It also has a nicer UI and is easier to configure.
However, the keyboard shortcuts are really strange. In case you didn't find out yourself, here are the most important ones:
- Send message - CMD+SHIFT+D (what does the D stand for? Deliver?)
- Apply rules to selected mails - CMD+ALT+L (L for ruLe ???)
- Select all messages in a thread - CMD+SHIFT+K (K for seleKt ???)
- Erase junk mail - CMD+ALT+J (now, this is obvious - J for Junk)
Apple developers, if you read this, please comment on the deeper meaning behind the shortcuts.
CVS on a Mac
In order to be able to work on some of our Xtext / Eclipse related build scripts, I needed to install a CVS command line client on my Mac. Now if you google for "cvs mac", you'll get a large list of result, basically telling you to get the Apple Xcode SDK. While the Xcode SDK is for free, and usually you don't even need to download it from the Apple Developer Connection's website (as you already have it on your Mac install disks as Lullabot points out), it occurred to me that installing a 1+GB space hog seems to be a bit of an overkill for getting a tiny application.
So I decided to give Fink a try. Here is what you need to do to get a CVS commandline client on your Mac:
- Download Fink
- Install Fink
- Copy FinkCommander to your Applications folder
- Start FinkCommander
- In the search box, type "cvs"

- Click on the "install binary package" button (it's the leftmost, with the blue plus sign)
- In the lower pane, you can now watch Fink downloading and installing the CVS package.

- Let's see if it works. Open a command line window and type "cvs":

Perfect!



