Sunday, 15 February 2009

NetBeans 6.5, JavaFX 1.0, Groovy and arithmetic training

I'm reasonably good at mental arithmetic, and I want my son to learn to do it as well. I think it's a disadvantage in life if you have to search for a calculator every time you need to add, subtract, multiply, or divide a couple of numbers. For that reason, I've written a number of arithmetic training programs for him. The first one I wrote was written in C# using the GUI building abilities of a cross-platform Java/C# IDE that I used to use, Xdevelop. Actually, I would still be using that IDE if they hadn't stopped supporting it a couple of years ago; I thought it was great.

Eventually, I wanted to write a second version of the arithmetic program, one that adjusts to the user's abilities. This is a fairly simple technique that I learned back in my educational multimedia days. You make the problems harder each time the user gets one correct, you make them easier each time the user gets one wrong. In this way, the program can quickly auto-adjust to the user's current level of skill. For this second version, I decided to use Java and Swing, but I wanted to use something like a GUI builder. You can write a user interface directly in Java, but it's a slow tedious process, and I don't have the time to waste on that. At that time, there was an early version of JavaFX available. JavaFX allowed you to declare the structure of your user interface by defining a simple structure of components within components; a bit like an XML file, but not XML, a hierarchical syntax more akin to Java syntax. It wasn't as good as a full-on visual GUI builder, but still seemed easier and more manageable than writing Swing code directly in Java. If you are writing JavaFX, I strongly suggest you download NetBeans, as its built-in JavaFX support is not bad. There is also a plug-in for Eclipse, but no plug-in (that I know of) for Intellij. There is a slim version of NetBeans now that has just the bits you need for Java and JavaFX development; it's much smaller than the "complete" version of NetBeans.

Anyway, this new arithmetic program has worked very well, but I need to make a few minor improvements, so I thought I should check the latest version of JavaFX. Recently, JavaFX 1.0 was released, and this has changed significantly. The old version was a simpler way to write Swing programs, the actual 1.0 release now has a "Stage" and "Scene" metaphor that is reminiscent of the Macromedia tools I used to use in my multimedia development days, particularly Macromedia Director (Adobe subsequently bought Macromedia). That's a good thing; those Macromedia tools really impressed me because both programmers and artists could use the same tool and usefully contribute to the final product. The same can't be said of most programming IDEs. The Macromedia tools were genuinely inclusive, in a way that so many software development tools are not. There is something wrong when so much software still only targets only the cleverest 5% of users. The other 95% deserve to have a chance to contribute as well, and they tend to be excluded unnecessarily.

Anyway, NetBeans 6.5 has support for JavaFX 1.0, including a new "palette" of visual items that you can drag-and-drop onto your "Stage". What I found, though, is that JavaFX has lost its ability to do Swing GUIs. Instead, a bit like the Macromedia tools I used to use, you have to lay things out yourself based on absolute pixel locations. This kind of user interface development works well when you have a very graphical interface created by an artist, and when you have an IDE that makes it easy to work with images, etc., and lay them out visually in the correct pixel positions. However, I'm not an artist, and NetBeans 6.5's JavaFX facilities are poor when compared to Macromedia's tools from 1996 (yes, over 12 years ago). I would hate to have to use NetBeans to do any serious JavaFX development. If JavaFX is going to compete with Flash, it can't just do the run-time, back-end things properly, it also has to be easy to author. At the moment, it is generations behind, and I really wonder which group, Sun or other, is going to be able to step up and provide the necessary interface, if it is to be a success.

Having discovered how different JavaFX 1.0 is from old JavaFX, it occurred to me that I was better off porting my program to Groovy. Groovy is a Java-based scripting language that compiles to Java byte codes and can use any Java library, so it's easy to learn if you have a background in Java development. Like JavaFX, Groovy provides a syntax for declaring the structure of a user interface (and uses a similar structure for XML, very useful, but a topic for another post sometime). Unlike JavaFX, Groovy uses the normal Swing components, so the conversion was fairly straightforward. It took me about a day, mainly because I was learning Groovy at the same time as doing the conversion.

The only significant difference between Groovy and the old JavaFX is that JavaFX has a neat "bind" keyword that can be used to make the value of a field in the user interface stay synchronised with a particular variable. It's a simple idea, but very powerful. It removes a lot of event handling code that you would normally have to add to a Java program. So, the Groovy version of my application required some new event handling code. Luckily, Groovy provides "closures", essentially "pure" methods that aren't attached to a particular class, and these simplify the writing of event handling code. As compared to converting to JavaFX 1.0, converting to Groovy and adding some event handling code was definitely easier. My son has his new arithmetic program running now, with some new features. Groovy is stable enough that I shouldn't face the same migration problem the next time I want to add some new features.

The more I use Groovy, the more I like it. It's worth the time to learn it, if you write enough code in plain old Java and would like to save some time.

Sunday, 25 January 2009

Null propagation, part 3

Almost two years ago now (#1, #2), I wrote about the idea of being able to modify the way Java handles the situation where a method is called on a null value. Normally this causes an exception; I was wanting such a call to return a null instead, so that the result is more like what happens with XPaths when a node in a path doesn't exist. For example, the XPath

/document/person/address

doesn't cause a program exception if there is no <person> element, whereas

getDocument().getPerson().getAddress()

will throw an exception if "getDocument().getPerson()" returns null. These exceptions make it painful to use Java (etc.) for working with XML, because you end up with so many try/catch blocks to deal with of the places that an exception could possibly be thrown.

So, it was interesting when I stumbled across the fact that Groovy (a Java-based scripting language) solves this issue! Groovy has a "?." operator, which returns null if called on a null, and executes the method normally if called on an object, i.e. "a?.b" is equivalent to "a == null ? null : a.b". So

getDocument()?.getPerson()?.getAddress()

will return null if "getDocument()?.getPerson()" returns null. That makes Groovy (for this and other reasons) a better way to write your XML processing code in Java, if you are trying to keep your code as simple and maintainable as possible.

Friday, 02 January 2009

The Virtual Laptop ― From Linux to Windows XP, part 2

John Cowan submitted a comment to my post about switching my laptop from Linux to Windows XP. He asks, quite reasonably, why I need to install OpenOffice directly on my laptop if I do my work in VMware virtual machines. Don't I only need OpenOffice in the virtual machines? Shouldn't I keep the laptop itself free of almost everything except VMware Server?

The answer is something of an 80/20 rule. There are compromises that you have to make in running VMware on a laptop (and even on a lot of desktops). The two most obvious ones are

  • available memory;
  • available disk space.

My laptop has 2G RAM, and that is the maximum it supports (it's 2 years old, what can I say? This Christmas, at a large UK chain called PC World, they had £499 laptops with 4G RAM and 250G disk, both figures about double what I have now. But I don't have one of those). When I'm running both Linux and Windows in VMware virtual machines on my laptop, I'm running 3 operating systems at the same time, 2 x Windows + 1 x Linux. That's quite a squeeze. I can only assign 768M to each virtual machine, which is less that most people are used to having these days when using Windows or Linux. It's just barely enough, really, although it is enough for the most part.

Now, there are certain common services I sometimes want to run that are used by both my virtual machines. The most important is my Subversion repository, but I also sometimes run XML or relational databases. As I'm already so squeezed for memory, there is no way I can run extra virtual machines for these. I also don't want to run them within one of my existing virtual machines, because them I'm forced to run both at once. It's better to be able to run them independently, not the least because I sometimes need to bump the memory up to double (1.5G) in order to run something big, and that means one virtual machine only. The upshot is that I have to run certain common services on the host directly, there isn't really a practical alternative.

There are also some applications that run sufficiently better on the host, enough to make it worth using the host directly. Skype is a key one for me, but also Bibble Pro which I use for processing RAW photographs from my digital SLR. And why OpenOffice? The main reason is because, when I'm giving a presentation at a conference or meeting, there is always that natural tendency for technical things to fail at the worst possible moment. Adding a virtual machine into the equation is just an extra distraction that I don't need. Additionally, sometimes I might want to quickly look at an office file that someone has put on a USB memory stick. I don't want to have to fire up both a host operating system and a virtual machine operating system just to view a word processing document or a spreadsheet quickly.

I am nonetheless fairly strict about what things I do on the host operating system, and what data or files I keep on it. Any data or files on the host have to be backed up separately to my virtual machines, which is important. I don't do e-mail on my host operating system (maybe the occasional bit of webmail, nothing more than that), nor anything much else that is part of my daily work tasks. So, it's 80 for the virtual machines and 20 for the host operating system. I think that's a reasonable compromise.

Data modelling, metamodelling and MPS

One side of the work I do for my clients is XML and related technologies, the other side is general data modelling. One thing that interests me in data modelling is the use of metamodels. Now, what is a metamodel? Normally, people switch off or break into a cold sweat when they hear "meta-anything" in a sentence. However, there is nothing scary about metamodels. A metamodel simply defines what things are allowed to be in your data models; it's a model of what you are allowed to have in your data models.

Why do we need metamodels? It's because data modelling is usually done using general purpose modelling technologies. Examples are E-R diagrams, UML, and XML Schemas. Any of these general purpose modelling technologies give you more features than you need or want in any single project. It can be a major problem to constrain people to use as few features as possible, which is what you want to make your data models as simple to understand and as cheap to maintain as possible. The role of a metamodel is to define a limited set of features which are the only ones that can be used out of all of the features that the general purpose modelling methodology provides.

For example, it's common to find people using XML Schemas that (a) use elements only for data, not attributes, and (b) use local element definitions only in the XML Schemas, not global element definitions (root elements accepted). This constraining of XML Schema to a simple subset of its features is equivalent to defining a metamodel that is a subset of the full metamodel for XML Schema. In practice, you can implement just such a "metamodel validation" on an XML Schema by applying some kind of schema language to the XML Schema itself. As it happens, XML Schema editors often won't let you apply an XML Schema to another XML Schema, but you can apply both Schematron and/or RELAX NG to XML Schemas in order to implement checks that the XML Schemas conform to your metamodel (i.e. your design rules).

UML is widely used for data models, and metamodels for UML are written in using a thing called MOF. With the latest versions, UML 2 and MOF 2, MOF is now a subset of UML that can be used for writing metamodels which constrain UML models. Sadly, support for validating UML models using MOF metamodels is still poor (at least in any tools I have used), but I live in hope that within a couple of years it will be possible to validate a UML model with a MOF metamodel as easily as you can validate an XML document with an XML schema.

Today I was catching up on some reading, and I stumbled across a mention of JetBrains Meta Programming System (MPS). I tried an early version of this a couple of years ago; it lets you design custom/bespoke mini programming languages (or mini data formats) that are specific to a particular purpose. The MPS tutorial is a good way to get a feel for MPS, what it does, and how it works. There is an obvious analogy between defining a mini-language for a task rather than letting people loose with a general purpose language, and using a metamodel to constrain people's usage of the features of a general purpose modelling methodology. MPS allowed you to define structured data models that have something of the feel of (simple) textual computer program, or a textual data file like a simple properties file. Although they have the look of text files, they are actually structured, and in some ways it's not unlike generating form-based user interfaces or Web pages. MPS is a neat looking tool, and it's free. I like JetBrains (commercial) Intellij IDE for Java, because it's so clever in the degree of analysis it does of your code as your write it. I was amazed how many issues it was able to point out to me as I was writing my code, before I even compiled it. If the JetBrains folks can apply the same cleverness to MPS, it could be a useful tool. That said, it's not the only tool that can generate user interfaces from models. Eclipse has EMF (like a cut-down version of UML) and Visual Studio has support for Domain Specific Languages (DSL). All of these tools are working to the same end, to provide you with a way to allow people to work with only the minimum set of features that are needed for any particular problem, so that they produce the simplest, most consistent, most maintainable solution that they can.

Metamodels and their equivalents let you take a complex software tool or methodology, and constrain it to effectively turn it into a simpler software tool or methodology. It's a bit more effort, but for a project of any size, it can pay real benefits in the end.

Thursday, 01 January 2009

The Virtual Laptop ― From Linux to Windows XP

For over 2 years now, since I bought it, I have been running openSUSE Linux on my laptop. Although I spent a lot of time making it work, more or less, I have also lost a lot of time in trying to get simple things to work well. For example, with a laptop, it's useful if you can hibernate it, and do so quickly and safely. Also, when travelling, you want things like Skype to work well. I never managed to get these kinds of "mobile" aspects working well enough. So, when I recently had a couple of hard disk crashes on my laptop in the space of a couple of months, I decided to bite the bullet and switch my laptop back to Windows XP. That's what I've done, my laptop is running Windows XP now.

The reason I could switch so quickly, in less than half a week of spare time, is because most things that I do are done in VMware Server virtual machines. So, as long as I can run VMware Server on a PC of any sort, I can quickly be up and running with my working environments.

Is Windows better than Linux? No, not really. I'm trading one set of issues for another. However, my new principle is that you should stick the the operating system that comes with your laptop. Not because that operating system is better than any other, but because it should work with the hardware in your laptop; manufacturer testing of the operating system with the hardware is worth a lot in practice. So, while hibernating is now much better (I can hibernate even with VMware virtual machines running, something that was out of the question with Linux), I've already had problems with software not running properly when installed, or new installations seeming to stop previously installed software from running. Linux has a sophisticated system for managing dependencies between versions of software, and Windows doesn't seem to have anything to match, so Windows is a generation or more behind in terms of making different software applications co-exist. I'm losing that, compromising instead to get better hardware support.

By the way, where does all of the disk space go in Windows? I don't like the idea of having just a single huge disk partition as is the default for Windows, so I split my partition (using "Parted Magic" - free and recommended by me) so that I have a C drive for Windows and application files, and a D drive for data. I thought 25G should be plenty, even for something as profligate as Windows XP (which itself is still leaner than Vista). However, with XP and selection of applications (including VMware Server and the Novell edition of OpenOffice 3, which are big installs), I now find I only have 4G free out of 25G! Where did my disk space go? I've examined the C drive once, but the numbers don't add up, something is missing. I may have some large "system restore point" file backups, perhaps. I'll be checking into that as I go along.

Windows XP needs more protection than openSUSE Linux, which comes with built-in support for disk encryption and also comes with Novell's AppArmor, which is a better way for Linux to protect against viruses/etc., than the virus-signature-based approach that is most common for Windows. That meant software to buy. I like to use Norman software (not Norton) for virus checking and firewall, because the firewall allows very fine-grained control over incoming and outgoing network connections. To encrypt my hard disk, I decided to follow Bruce Schneier and use PGP Whole Disk Encryption, which is straightforward to use and also provides support for signing and encrypting your files using PGP.

By the way, my laptop may be running Windows XP, but remember, I do most of my work in VMware virtual machines, so I'll still be doing most of my work in Linux. I'm just trying to get the best of both worlds.

Twittering Away

I signed up for Twitter quite a while ago, but never used it. I couldn't quite see the point; if I'm at my PC, I would rather use my weblog if I have something to say. So while I had some followers on Twitter, I never posted any "tweets".

However, I recently came across "twibble", a Twitter client for mobile phones. On my phone, Twitter makes sense to me. The maximum length of a single "tweet" is about the same as an SMS text message, so it's the kind of short thing that phones are good for. So, I've started posting snippets of thoughts to Twitter. If you are interested, feel free to follow my ramblings via Twitter.

Happy New Year 2009!

Happy New Year, everyone! I don't know what 2009 will bring for us all, but I'm sure we all hope that 2009 will be the year when the current global recession comes to an end. That said, there are no promises, it is no time to be complacent. When the economy rebounds, as it surely will, it will be a time of great opportunities for those who are ready for it. I wish you all the best success in making the most of those opportunities.

Saturday, 29 November 2008

Opera bug in handling of Atom feeds

I use the Opera browser (currently version 9.62) not only as my browser, but also as my e-mail and feed reader. I had noticed that, sometimes, the link at the bottom of a feed (blog) post doesn't take me to the correct page. In particular, this was happening with the Atom feed for Tim Bray's 'ongoing' blog. Tim was one of the authors of the Atom specification for blog feeds; I know Tim, and I know his blog feed follows the Atom specification rigorously.

I wrote to Tim and asked him about it, and it appears that this a known bug in Opera's handling of Atom feeds, that it doesn't not correctly process relative URLs in Atom feeds. Apparently Opera has been doing this wrongly for some years now. For example, for Tim's most recent post, Opera shows the link the Web page as being

http://www.tbray.org/ongoing/CL-Rain

but the actual page URL is

http://www.tbray.org/ongoing/When/200x/2008/11/28/CL-Rain

What went wrong? Well, in the Atom feed for 'ongoing',

  • the value of "/feed/id" is "http://www.tbray.org/ongoing/"
  • the value of the first "/feed/entry/link/@href" is "CL-Rain"

and Opera appears to be concatenating these to produce the URL

http://www.tbray.org/ongoing/CL-Rain

However, that first "/feed/entry" has its "xml:base" set to "When/200x/2008/11/28/". What Opera should do is add this in the middle, to produce the correct URL

http://www.tbray.org/ongoing/When/200x/2008/11/28/CL-Rain

Anyway, I have now posted a bug for Opera (DSK-240409), so we can only wait and see now when it gets fixed. Fingers crossed that it's sooner, rather than later.

Wednesday, 26 November 2008

XML Spy, Schema Documentation, and Java, part 2

I wrote previously that I had tried using the XML Spy 2008 Java API to generate Schema documentation, and it hadn't worked. I ended up with an exception in the DLL code that the Java API uses to control XML Spy, and I assumed it was a problem with the Java API, perhaps that it wasn't well enough tested. However, Liz Andrews from Altova kindly posted a comment with some sample code for using the Java API. Thanks, Liz! I can see now where I made my mistake; the Java API reflects the style of the XML Spy COM API, so it's slightly unusual for a Java API, and I hadn't understood the correct way to create a "SpySchemaDocumentationDlg" object. However, the sample code shows exactly how it is done. I haven't tested it yet, but it looks good to me, I can see why it would work and why my own code didn't work.

The documentation that comes with the XML Spy Java API is somewhat minimal, so I hope Altova will think of including this example with the documentation, or adding the example to their web site, at it's an example that explains a lot that the documentation doesn't cover.

Saturday, 22 November 2008

A Bunch of Rocks

I came across this great cartoon recently. If you have any background or interest in physics, computing, life, the universe, and everything, take a look.

Reuters Market News

Search this blog (Google)

  • Google

    WWW
    kontrawize.blogs.com

What links to this blog?

Subscribe to a kontrawize feed

February 2009

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
My Photo

BlogBurst

Blog powered by TypePad