Posts tonen met het label gwt. Alle posts tonen
Posts tonen met het label gwt. Alle posts tonen

woensdag 28 januari 2009

Generating dynamic web apps with DSL and GWT

The idea for my presentation at Devoxx'08 originated last summer when I attended the java event organized by ProfICT. There I saw Neal Ford build the case for Domain Specific Languages (DSL) and subsequently saw Sven Efftinge demo xText and explain how to craft your own DSL with this Eclipse based plugin called openArchitectureWare. A very interesting event and I immediately saw the potential of DSLs for one of my clients.
For this client I once built an intranet application that let their internal sales reps generate quotes for their customers selling complicated power generators. These quotes consisted of different components with many dependencies between them. Back then I created an XSLT/JSP solution which described the problem domain as well as the front end representation in one huge XML file. After attending Neal Ford's relentless XML bashing I understood that this application needed a rewrite based on a DSL.
So basically the idea was to describe the problem domain (complex power generators) in an external DSL and visualize it using a nice front end application. Out of personal interest I decided to construct the front end using Google Web Toolkit (GWT) but without writing too much code for it.
Just to verify the feasability of this idea I wrote a proof of concept for a fictitious bicycle store, looking like this:

So what did I need to build this proof of concept? Well, I needed to think about how I'd like to model my problem domain. Basically, the bicycles sold in our shop are composed of a few Entities with certain values and there are relations between these entities. Also, I wanted to be able to group the potentially long list of entities over a few steps. So I came up with a model which looked like this:

So we have two entities (Type and Color) and the values of Color depend on the Type. Example: Racing bikes are only sold in silver whereas City bikes are available in all four colors. Both entities are displayed in the one and only Step (actually in the demo above there are two steps with four entities but you get the idea)
This model was constructed according to the new DSL which I had to define in xText at the same time. xText is an eclipse plugin which offers you a wide range of tools and aids to build your own DSL, making use of the familiar context sensitive code completion features in Eclipse.
The definition of the DSL is another text file which looks like this:

Model:
(elements+=Element)* (steps+=Step)*;

Element:
"elt" name=ID "{"
((option+=Option) ("," option+=Option)*)?
"}" (dependsOn+=Reference)*;

Reference:
"dependsOn" elt=[Element] "{"
((dependency+=Dependency) ("," dependency+=Dependency)*)?
"}";

Option:
description=STRING (deflt?="default")?;

Dependency:
fromOption=Option ":" "{" ((toOption+=Option) ("," toOption+=Option)*)? "}";

Step:
"step" nr=INT label=STRING "{"
(elt+=[Element] ("," elt+=[Element])*)?
"}";


This format of this grammar description is an extension of the Backus-Naur-Form. It basically says that our Model consists of one or more Elements and one or more Steps, which in turn have their own restrictions.
With a correct grammar definition you can ask openArchitectureWare to generate a new eclipse plugin editor which enables you to actually write your model. This editor automatically contains code completion compliant to your grammer, i.e. it will display a red cross when you accidentally type Elemend instead of Element. Moreover you can expand your editor with your own rules. It might, for example, not make sense to have multiple Elements with the same name. This is not enforced by the DSL grammar but can be added to the plugin editor with the following rule:

context Element ERROR "Names of all entities must be unique." :
allElements().typeSelect(Element).select(e|e.name == this.name).size == 1;

xText/openArchitectureWare is actually quite a powerful environment, backed by an active user community. Anyway, this is all nice and interesting but what if we want to do something useful with our model? This is where xPand templates come into play. The idea now is that we will write a few templates which generate java code which, in turn, is added to a skeleton GWT application. An excerpt from one of the templates:

What this template will do is generate the source code of a java enum called Entity. In the constructor of the enum we pass in an array of possible values for the entity and a String with the default value. Two more templates were needed: one to generate an enum of the Steps and the third one to define the dependencies between the entities.

The GWT application is not much more than a StackPanel with one panel for each step. Inside these steps we render the entity values in a select boxes. These select boxes contain onChange event handlers which cause the dependent entities to be recalculated, i.e. when we change the bicycle type from City bike to Racing bike, the list of possible Colors has to be reduced from four to only one Color. This logic was built in GWT and is the same for any model we generate into the GWT application using our DSL.

So in theory it would now be possible to construct a real world application with the entire list of product entities and their dependencies in the xText editor; generate the GWT classes using the xPand templates, re-generate the GWT application and we're ready! Of course the GWT application would also have to be augmented to actually DO something with all the selections, at the moment we only allow to the user to click through the different options without actually doing something (i.e. ordering a Green Citybike).

By the way, I think the power of GWT is justly demonstrated by the fact that it was ridiculously easy to include the real proof of concept into this blog post; I only had to upload the GWT generated HTMLs and other files to a directory on my hosted server and include an iframe with a reference to the app. in this post!

For those interested in the details I include the links to the presentation as well as the source code of the prototype.

maandag 15 december 2008

Devoxx'08 a great conference

Three days at Devoxx'08 have really refueled the engine with new creative ideas for the (near) future. The conference was well organized at an excellent location: MetroPolis, just off the Antwerpen city center. We didn't make it to the opening keynote on Wednesday so we missed the JavaFX commercial but by lunch time we arrived and could listen to Brian Goetzke explain the concurrency API and how we should stop thinking in Threads and move on to multi core parallelism. The same afternoon we saw the future of rich internet applications or at least Adobe's version of it; saw a bloke from New Zealand explain the wonders of easyb and behavior driven development; were slightly disappointed by the filthy rich android clients talk because of its very dry nature and lack of cool effects and finished up with a great talk by Peter Kriens, one of the founding fathers of OSGi. As it turned out OSGi was quite hot because of the new directions SUN is taking with java and modularity in project Jigsaw.
Thursday was my personal big day with my BOF scheduled for 20:00 at night. The day started with a great keynote by Josh Bloch who showed some excerpts from his book Effective Java. We got in early so we also got to see the dancing girls, as opposed to those who watched the keynote from adjacent rooms :-). Next up Ivar Jacobson, Mr. Use Case who explained how to be smart on software projects. Later that day I came across the book The Mythical Man-Month by F.P. Brooks; quite interesting how such a relatively 'old' book can still be up to date as exemplified by Ivar Jacobson's talk. Next up more talks on JavaFX, a weird live video connection talk by javaposse.com with free beer, and a boring overview of jBoss SEAM.
My own talk on DSL and GWT went really well; the hour of day was not one to attract large audiences but those who had ventured into my room all stayed until the end. Some really good questions also during the talk and a few interested people at the end while we were gently being shoveled out of the room by the next speaker. A big sigh of relief after two months of preparations and we took the first tram back to Antwerpen city center to taste a few Belgian beers.
Friday morning three concluding talks: a pretty impressive live demo of the new Spring DM server demonstrating how one OSGi could real-time be replaced by another while the main application kept running. Java + XSLT 2.0 was a bit too high level but the final presentation was a really good presentation on EJB 3.1 and why we should reconsider the idea of never ever doing EJBs again. Good stuff.
So all in all three days well spent; I bumped into a few people I'd worked with years ago, made some new friends, scored three new (black) t-shirts, bought two new books one of them signed by The Josh Bloch himself and just generally enjoyed being immersed in Java for three days on end! I'll try to summarize my own talk on DSL and GWT in a later posting.

woensdag 8 oktober 2008

Talk on DSL and GWT at Devoxx'08

So this weekend the people from Devoxx sent me a mail with confirmation for me to present a Birds Of a Feather session at their next conference! Wow, that's certainly something I didn't really expect when I submitted the proposal two weeks ago. My presentation is about how I designed and used a Domain Specific Language to generate a Google Web Toolkit based user interface. I guess it must have been the combination of the two buzzwords DSL and GWT that did the trick. So the next few weeks I know what to do after I get back from the day job and putting the kids to bed! 

Since I've never before visited Devoxx (formerly known as JavaPolis) I don't really know what to expect from the conference; how many people will attend such a BOF, etc. My BOF is scheduled for 20:00 on Thursday night when everybody has started tasting the Belgium beers I suppose? We'll see.