July 24, 2008
by Branden
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.





