Thursday, October 28, 2010

Learn how to create a language for Android development using Eclipse Modeling

If you are lucky, you have already registered for
Eclipse Summit Europe 2010 in Ludwigsburg, Germany. If not, I have sad news for you: It is sold out. Sorry.

I'm speaking at Eclipse Summit Europe 2010

If you're there, I hope you registered for our tutorial Creating a Language for Android Apps with Eclipse Modeling. A joint team from Obeo (Mikael Barbero and Stephane Begaudeau)
and itemis (Holger Schill, Dennis Hübner, and me) will introduce you to the basics of the Android SDK and show how EMF, Xtext, and Acceleo can be leveraged to simplify the development of Android Apps.
Bring your laptops, it's going to be a hands-on experience.

If you like this approach, learn how it works on larger scale in the talk Models To Go: How We Built a DSL for Mobile Apps With Tools From the Eclipse Modeling Project
by Peter Friese and Heiko Behrens who implemented the official ESE App for the iPhone.

Curious about what we have in mind for the future of Xtext? Then you must see Live Coding: Building a UI styling language for E4 with Xtext. Sven Efftinge and Sebastian Zarnekow will demonstrate Xbase, an Xtext based expression language bringing behaviour to your DSLs.
Sven will also talk about Dependency Injection which is the key to extensibility and an easy programming model, not only in Xtext.

Itemis will give two more presentations: Andreas Unger and Axel Terfloth from our embedded team will teach you on Developing Embedded Software with Eclipse, State Machines and Block Diagrams.
Nirmal Sasidharan will present Eclipse - an IDE for Organisational Change. I am sure you'll like these, too!

I am particularly looking forward to the keynotes, especially the one by Jeff Norris from NASA who already rocked this year's EclipseCon.

See you in Ludwigsburg!

Sunday, June 20, 2010

Semantic Model Access in Xtext

In Xtext, the parser creates the EMF-based semantic model or abstract syntax tree from the textual representation of the model. Many components, e.g. validation or outline, but also an interpreter or code generator work on this semantic model rather than on the textual representation. In this post, I want to introduce you to Xtext's APIs to safely read and modify the semantic model of an Xtext document.

Why do we need model synchronization at all?

In a running Xtext Workbench, there are a number of components which access the semantic model, i.e. the parser, the linker, the validator, the outline, the index builder etc. While some of these components are executed by the display thread, others like the parser or the indexer use different concurrent threads to not deteriorate the editing experience. If you for example want to have a consistent outline of your model, it is essential to keep other threads from modifying the model while the outline component reads it.

Why not use EMF Transaction?

Editors that use EMF Transaction usually employ a so called TransactionalEditingDomain that shields a ResourceSet with a lock and manages a transactional command stack. The Resources in the ResourceSet can be read safely - i.e. excluding any concurrent write operation - inside a Runnable that is passed to the TransactionalEditingDomain.runExclusive() method. Write access is only possible by issuing a command. A change listener throws an Exception if write operations are executed outside of write command.

This quite heavy-weight mechanism can be appropriate for pure model editors, i.e. editors modifying the model directly like the generated EMF tree editor, but experience shows that it is not easily integrated with other command frameworks. (If you don't believe this, just look at the way GMF deals with EMF Transactions, GEF commands and the Eclipse Operation history.)

Architecturally, an Xtext editor is a text editor in the first place. The commands on its undo/redo stack are the usual commands used in text editors. So instead of inventing yet another adapter technology, we headed for an easier synchronization mechanism.

How do I read a model safely?

Each XtextEditor uses an IXtextDocument to store its model. The IXtextDocument allows reentrant read/write access to the underlying semantic model by means of the two methods
readOnly() and modify(). Both take an argument of type IUnitOfWork(<T>, IXtextResource) which defines a method <T> exec(IXtextResource) that contains what you want to do with the model and allows to deliver a result of arbitrary type.

So here is an example of safely reading a model:
IXtextDocument myDocument = ...;
String rootElementName = myDocument.readOnly(
new IUnitOfWork(){
public String exec(IXtextResource resource) {
MyType type = (MyType)resource.getContents().get(0);
return myType.getName();
}
});

How do I modify a model safely?

Direct write-access on the document is usually only performed inside the framework. If you want to change a document by means of its semantic model, you should rather use an IDocumentEditor which uses the modify() method internally but takes care of synchronizing the node model, too:
@Inject
private IDocumentEditor documentEditor;

public void setRootName(IXtextDocument myDocument,
final String newName) {
documentEditor.process(
new IUnitOfWork.Void() {
public void process(IXtextResource resource) {
MyType type = (MyType)resource.getContents().get(0);
myType.setName(newName);
}
}, myDocument);
}

BTW, the QuickFix-API internally uses an IDocumentEditor, too.

PS:

In Xtext 2.0 we have changed the semantics of XtextDocument.modify() to use the former IDocumentEditor functionality. So the model write example now symmetrically becomes
public void setRootName(IXtextDocument myDocument,
final String newName) {
myDocument.modify(
new IUnitOfWork.Void(){
public void process(IXtextResource resource) {
MyType type = (MyType)resource.getContents().get(0);
myType.setName(newName);
}
});
}

Monday, June 7, 2010

Xtext: Webinar and Eclipse Demo Camp

Tomorrow (Tue, 8th of June) will be my Xtext presentation day ;-)

At 5pm CEST I will jump in for Sebastian in the Xtext Webinar. Sven and me will give a free one hour live seminar on Xtext introducing the framework and demonstrating its new fancy features in Helios. Moritz will assist us in answering your questions on the live chat. It's free and it's broadcast live via internet. Why not join?

If you're based in Germany's Ruhr area, you might want to visit the Eclipse Demo Camp in Dortmund. I will give another 20min demo of Xtext and - guess what - its new amazing features! But even for non-Xtext enthusiasts there will be nice demos, e.g on SAP's new graphical modeling framework "Graphiti", on developing Android apps with Eclipse and a lot more intriguing topics.

Finally, I plan to have my hopefully well-earned after-work pint in the Eclipse Stammtisch.

Wednesday, March 31, 2010

Xtext For Your Ecore Models

The new M6 release of Xtext ships with a new component: The Ecore2Xtext Wizard.

Why should you use the
Ecore2Xtext Wizard?
  • You want your models to be in a syntax that humans can not only read but also understand.
  • You want a model editor that offers all the convenience of a modern IDE.
  • You already have an Ecore model but don't know how to start with Xtext.
  • Your Ecore model is huge and you want a quick start with Xtext. You can easily fine-tune the syntax later on.
How do you use it?
  1. Start the wizard by choosing New -> Xtext -> Xtext Project From Existing Ecore Models.
  2. Select the EMF generator models1 from your workspace for which you want a textual syntax and choose your root element's type.
  3. Fill in all the language metadata on the second page of the wizard. Remember the file extension.
  4. Click Finish and wait until Xtext has generated the two common Xtext plug-ins and the Xtext grammar for your language.
  5. Run the MWE2 workflow located in the same directory as the grammar. Now Xtext generates the language infrastructure (parser, editor, formatter etc) .
  6. Spawn a new Eclipse runtime workbench, create a sample Java Project, and open a new model file with the file extension you have chosen in the wizard. Play around and have fun with your new textual model editor.
1 the genmodel is needed because we need the fully qualified names of the generated Java classes as well as the location of the Ecore file. The genmodel offers both of them.

Watch this short screencast to see it in action:



What is the generated syntax like?

Names of EClasses and EStructuralFeatures become keywords, containment is marked with curly braces, elements in lists are separated by commas, etc... Here's an example of an entity model in the generated language:



What if it doesn't work?

The grammar is the primary artifact of every Xtext language, but there are a couple of further services you might have to configure:
  • A IQualifiedNameProvider to define how the fully qualified name of an element is derived.
  • A IScopeProvider to define which elements are candidates for a cross reference.
  • ...
Please consult the Xtext documentation for more information.

Another geekish meta-confusing example

All right geeks, Ecore itself is defined in Ecore, so let's generate a textual syntax for Ecore and see how Ecore looks in that syntax! Only two adaptions of the generated code where necessary to get this editor:



(The '^' chars are automatically added by Xtext to distinguish identifiers from keywords, which of course collide a lot in this example) It is certainly not as complete as EMFatic, and it has a quite verbose syntax, but it could be the starting point for a nice textual Ecore editor.

Thursday, March 25, 2010

EclipseCon 2010 - Xtext everywhere

It's the last day of this year's EclipseCon. My second time in Santa Clara and I even liked it better than last year: Many intriguing talks around Eclipse, OSGi, e4 and others, interesting people, much time for getting to know each other.

The itemis team has had a fair share on the program: With Moritz and Heiko, I gave a workshop in which we showed how to build an Xtext editor for the e4 workbench model. I also had a lightning talk on my favorite topic "Combining text and graphics in modeling editors". As always, you can download the slides to all talks from slideshare. We also had talks on Xtext, MWE2, Xpand and model-driven iPhone application development.


Xtext has been ubiquitous. We have seen a lot of cool Xtext editors in many talks. Quite a bunch of people came to our booth just to say thank you for the cool piece of software we've been working on for the past years. The absolute highlight was when Xtext received the Eclipse Community Award for the most innovative Eclipse project. It is so inspiring to get such a huge amount of positive feedback.

NASA also sponsored a competition of building a e4 client to control a small robot simulating the Mars Rover. Peter and Heiko have decided not to sleep but to rather build an iPhone client instead. Even though it was not based on e4, people were really impressed. Watch Jeff Norris from NASA driving the rover with an iPhone! By the way, Jeff gave the best keynote I have ever seen on "Rocket Science and the Republic".

Tonight will be the European Reception sponsored by itemis and the conference will end. We'll have another day in San Francisco before returning home to Germany. I hope I will make it next year again. Thanks to the organizational team for giving such a wonderful conference.

Friday, October 30, 2009

Eclipse Summit Europe 2009 retrospective

Attending Eclipse Summit Europe 2009 once again felt like a big family meeting. It is always a pleasure to meet people you usually only correspond with electronically in person. The organization was once again almost perfect and the food excellent. Thanks a lot to the organizers and sponsors!

We had a very productive project meeting for the EMF Index project, clarifying a lot of critical points and further aligning the efforts and requirements of the involved parties.

Markus, Sebastian, Karsten and me were impressed by the large number of about 50 participants in our workshop on Domain-specific Languages with Eclipse Modeling. The slides are now online at slideshare, just in case you're interested. With such an audience it was of course a little bit difficult to make it a real hands-on experience, but I think we at least managed to leave a good impression on the presented toolchain.

Jos's and my talk on Combining text and graphics in modeling tools was also well attended, and from the quality of questions that were asked I guess our message has been understood. The slides are also available at slideshare.

Apart from a number of really impressive talks, the thing that most impressed me was SAP's graphical editing framework. Instead of using a code-generation approach it rather focusses on an provider-based Java API. They are currently in the process of adapting it to EMF and their plan is to make it open source. I hope they really do, because it seems to have the potential of greatly reducing the complexity of creating graphical editors.

Sunday, October 25, 2009

GMFTools Part 2: Sharing an EditingDomain

In this blog, I will elaborate on how GMFTools facilitates to share the same instance of an EditingDomain across multiple diagram editors.

What is an EditingDomain?

In GMF, each diagram editor works on its own instance of a so called TransactionalEditingDomain. TransactionalEditingDomain is a concept from EMF Transaction. In GMF, it contains an EMF ResourceSet holding the diagram model, its semantic model and all referenced models as well as a command stack for undo/redo support. In addition, each editor registers a WorkspaceSynchronizer to its resource files, making sure external changes are synchronized with the editor's content.

Why share the EditingDomain?

In the default setup, problems occur if two editors operate on the same resources: If both editors change the same resource, saving one will overwrite the changes of the other. This situation already happens when you're using the Related Diagrams feature in the gmfmap model: On double click, it will open a new diagram that is saved in the same resource but in a different editing domain. As the original editor is still open, conflicts can arise quickly.

Another reason for sharing the editing domain is to save memory and performance, especially if you're working on big models with a couple of editors.

In many cases you might want to see changes immediately in all editors without having to save first, e.g. when your editors show different parts or aspects of the same model. GMF has already built-in support for that as it uses EMF's notification mechanism to synchronize the notation (diagram) model with the semantic model.

Consequences of sharing an EditingDomain

Sharing the editing domain across several editors has a number of consequences
  • The editors will also share the same command stack. Undo/redo becomes a global operation and can therefore affect other editors than the currently active one.
  • For reasons of simplicity, all editors should be saved synchronously, and they will all be either dirty or clean at the same time.
  • You should no longer dispose the editing domain when closing an editor. That means, to avoid memory leaks you need strategy for unloading unneeded resources.
  • The command stack will keep references to unloaded model elements, too, so you might want to have an garbage collecting strategy here as well or at least keep the command history short.
Solution in GMFTools

The solution in GMFTools is based on a WiKi page of GMF and a couple of newsgroup entries combined with our own experiences. It has been recently refactored to reenable the WorkspaceSynchronizer.

It comes in a number of aspectual template changes for the GMF code generator, an additional runtime plug-in and tool support for fixing the type registry, such that it is rather easy to consume even if you're not too familiar with the GMF runtime. Note that so far we have only been using it to generate plain diagram plug-ins, i.e. not for GMF's RCP generator.

The major changes are
  • Redirect all creation statements of an editing domain to a new utility class and the dispose statements to an unloading tool.
  • Make sure to use consistent element types.
  • The common behavior of all XXDocumentProvider.ResourceSetInfo has been extracted to avoid synchronization feedback loops.
If you're interested in the details have a look at the WiKi-page.