Simplifying code with finite state machines

Yes, it's been a while. We've been busy. The good news (for you) is that we've been making fun new things (i.e. code libraries) that we'll be sharing soon!

Now, on to the actual topic of this post - finite state machines. What is a finite state machine you ask? Well it's just a construct that lets you track the states within a piece of code and ensure that you only do what you're allowed to do when you're allowed to do it.

A while back I wrote a generic finite state machine (I called it PastaMachine - since every time I wrote FSM I thought of the Flying Spaghetti Monster). It is exceptionally generic - you just define states, define the allowed transitions between those states (state A can go to state B, but B can't go back to A, etc) and the actions that each state may take (an action may trigger a transition, but it doesn't have to). The states themselves can have attached to them an arbitrary amount of metadata and in less than 340 lines of code you have a simple finite state machine.

What can you do with it? Well, originally it was written to handle a branching logic game where depending on the users success at a simple game they went down multiple paths and were shown various different videos (yeah... it was a PG-13 strip poker game). But recently Keenan has been using PastaMachine to track the logic of an application to assist doctors in treating patients with HPV. If that's not a nice range of usefulness, I don't know what is!

One nice extra trick PastaMachine has up it's sleeve is that once you've defined a state machine with it you can export the information about that state machine to data in DOT format. That DOT data can be loaded into the ever-so-awesome GraphViz and BAM! you have a directed graph diagram of your state machine! It's actually really useful in debugging the state machine's connections, but beyond that it adds a very nice "WOW!" factor when you present it to your client.

Over the next few weeks we'll be cleaning up PastaMachine and sharing it with everyone. Then, if everything goes smoothly, there will be a number of other Automata code libraries following along behind it.

Charles - A great web application debugging tool!

During a recent project of ours we found ourselves in the position where the developer creating the server-side code was a full 5 time zones away. This meant that we often had to do debugging over email. This was, as one would expect, excruciatingly slow. The issues that arose during integration almost always resolved around how we were mapping the servers .Net classes to our AS3 classes - one small change to the server class could break things on our end in a non-obvious way.

Luckily early in this process we discovered a piece of shareware called Charles, a web debugging proxy that's available for Mac, PC, and Linux. Once you fire Charles up it acts as a proxy for all of your web traffic. This lets you capture, view, and even throttle the data that's flowing between your machine and the web server. And best of all for us, it lets you view inside of AMF packets and see exactly what's coming over the wire. Once we got Charles up and working we able to tackle kind of issues that used to take us hours and resolve them in minutes.

We haven't done a done with Charles yet, but so far our only wish is that we knew about it sooner. Suffice it to say that we found the $50 registration fee to be well worth it!

Making Leopard’s Quicklook Feature (more) Useful

I've been dealing with piles of two types of files that are somewhat awkward to work with on the Mac - ZIP files and EPS files. On the PC I can just double click on a ZIP file and peer into what's inside without actually unzipping it, on the Mac, not so much. When it comes to EPS files, I'll open then with preview and then get a lovely spinning beachball while it is "Converting Postscript to PDF...". Now admittedly it's faster on Leopard that it was on Tiger, but still, that sucks. Luckily these are exactly the kinds of problems Quicklook was made to handle.

If you haven't played with Quicklook, you just select a file and hit the spacebar - you then get a preview of the contents of the file. By default it works with iWork formats, and all the usual MS Office formats. The best part though is that third parties can write their own plugins for Quicklook - and developers have already made some for ZIP and EPS! Now I just select either file type and smack the spacebar and BAM! almost immediately I can peer inside of ZIP files or preview EPS files.

It's times like these I *really* love the Mac developer community (and Apple ain't half bad either, though I'm still not fully digging Stacks!).