Archive for February, 2007

A small GWT application

I had some time today, so I built a little AJAX application for this blog. It’s an expression calculator. The world really doesn’t need another one, but it was the easiest thing that includes ANTLR for building a lexer/parser/AST and the Google Web Toolkit for building the client. Why? Because it’s fun …

If the application doesn’t load, please click here!

Before I was able to include a GWT application within a post in this WordPress powered blog, I had to create a WordPress plugin that generates the necessary meta tags. GWT applications only work, when those meta tags are present.

The actual calculation of the expression is done on the server with the help of my Java based framework. The expression is sent to the server asynchronously as a JSON data packet. The server logic evaluates the expression and sends back either the result, or an error message. If you want to see an error message, try a division by 0. The response is sent also in the JSON format back to the browser, where the GWT application takes it and displays it without refreshing the whole page.

The server side logic is built with the help of ANTLR and ANTLR Studio for Eclipse. Instead of writing a lexer and parser for evaluation of the expressions myself, I’ve used a grammar to let these tools do it for me. The following screenshot shows ANTLR Studio for Eclipse in action:

Eclipse screenshot with ANTLR Studio

If you want to know what ANTLR is, here is a quote from Terence Parr, the primary author of ANTLR. He has been working on translation tools since the late 80s:

ANTLR, ANother Tool for Language Recognition, (formerly PCCTS) is a parser and translator generator tool, akin to the venerable lex/yacc duo, that lets you construct recognizers, compilers, and source-to-source translators from grammatical descriptions containing C++ or Java actions. You can build translators for database formats, graphical data files (e.g., PostScript, AutoCAD), text processing files (e.g., HTML, SGML), etc. ANTLR is designed to handle all of your translation tasks. ANTLR is recommended by the co-inventor of LL(k) parsers and by the inventor of SLR(k) and LALR(k) parser.

Another tool makes generation of GWT clients easier. It’s the GWT Designer from Instantiations. I’m having a few problems with it, but it still helps a lot. See the following screenshot for an impression:

Eclipse screenshot with GWT Designer

If you’re interested in this example built with GWT, ANTLR, JSON, etc., and you want to look at the source code, just download it from here.

Do you remember the little problem from the Geometry for kids post earlier today? With the calculator, you can verify if the solution is correct: d = sqrt(3)*5.3.

Update: As of GWT 1.4, a new application-loading mechanism has been introduced, that doesn’t require a meta tag in the page header. It’s still supported for legacy applications. But now, the applications HTML code can directly reference the JavaScript file. I’ve changed the example above accordingly.

Comments (1)

Geometry for kids

One of our girls visits the 7th grade (13 years, secondary school) right now. In geometry, they are talking about the congruence rules for triangles and triangles in general. Yesterday’s homework included a geometrical problem: construct the space diagonal of a cube geometrically and specify its length (side length of the cube is 5.3cm).

While this might be an easy problem for grownups, it seemed not so easy for her. She drew a grid model of the cube, connected the opposite vertices, and measured off the length of the resulting diagonal. It seem logical and right for her. When I asked her about the actual angles in a cube, and how they relate to her projection, it became quite clear, that some angles in the drawing weren’t quite what they should have been.

In my opinion, kids have to learn how to ask the right questions, and how to find answers. And then they should explain it to somebody else. As a freshman I earned some extra money by giving private lessons for juniors and seniors. I quickly learned, that I was only able to explain a problem and its solution in depth, when I was able to answer all kinds of questions they were throwing at me. It didn’t have anything to do with knowledge, but with the ability to express it. And that is quite harder. So whenever you want to make sure you truly understand something, talk to someone about it for a couple of minutes, and have her or him ask you questions.

Constructing the space diagonal of a cube geometrically

Finally, she was able to find a solution to the problem. I didn’t explain it to her, but I asked her some questions. She had to explain it to me.

Comments

Creating a WordPress Plugin

It wasn’t planned that way, but it turned out that I had to write a WordPress plugin myself to solve a problem. I want to have a Google Web Toolkit application run inside my blog. No big deal, but I couldn’t accomplish one tiny detail. GWT applications need a meta tag with the name gwt:module in the page header section. How do I manage to get this meta tag up there whenever a post includes a GWT application?

First, I looked for help where all the WordPress plugins/meta are listed. None of them seems to solve my problem. To be honest, I only looked at a few, so I really don’t know if I’ve overlooked the one I need. One Meta plugin came quite close to what I want, but it was for description and keywords only. Except that, it would have been perfect. So I took its idea and modified it.

You have to use WordPress’ custom fields for storing the gwt:module meta tags. Just create a new custom field when you create or edit a post. The key should be set to gwt:module, and the value should be the path and name of your GWT module. The plugin then collects all gwt:module meta tags for all posts on the page and puts them in the header section of the page.

Creating a custom field for a GWT-module

In case you want to use this plugin yourself, you can download the WordPress plugin for creating gwt:module meta tags from this web site.

Later today, I will write a post that actually has a GWT application included.

Comments (1)

Programming editors

Everybody who is programming is using one or more of them. My favorite programming editor is Epsilon from Lugaru Software Ltd., and that since 1996. It’s around since 1984. Although my first version was for DOS, Windows, and OS/2, I used it primarily under DOS for writing C programs. I started in 1978 with a BASIC dialect and 8080/Z80 assembler on a SORD M200 computer. I wasn’t thinking about what editor to use back then, because there was no choice. I had to use the one that came with this SORD computer. A little later I switched to C and then C++, followed by Java in 1996. There will be something after Java, but I can’t see it yet. It’s not so much the programming language itself that keeps me, but the Java platform.

Screenshot from Epsilon editor under Windows

Epsilon is an Emacs clone, but unlike Emacs, which can be extended with Lisp code, Epsilon uses a C-like language called EEL (Epsilon extension language). As you can see from the list of programming languages I’m sufficient familiar with, Lisp isn’t on it, but C is. Although this short enumeration of programming languages isn’t complete, I haven’t used any of the others for a substantial amount of time. It’s important for me, that Epsilon is programmable in a language I’m fluent in.

There’s one exception to my otherwise consistent use of Epsilon. When it comes to Java programming, I’m using Eclipse most of the time. Not always, because some things I still do with Epsilon, but most of the things are a little easier within Eclipse.

During the time I worked with Epsilon, I observed a strange thing: some programmers (even brilliant ones) worked with editors that are not supporting them well with their work. Even when shown alternative editors that would support them much more, they gave up early on learning the features of the new tool. Instead, they spend hours writing some small utility programs for tasks that are possible within minutes for editors like Epsilon. Instead of investing the time in learning a new tool, they spend much more time with fixing the shortcomings of the editors they were used to.

Don’t get me wrong, Epsilon is not a panacea for everything, and I’m gone the minute I find something better. But in the past 10 years, I haven’t.

Comments