Begin main content

State Machines for UI Code

I have been doing some more thinking about finite state machines and their applicability to different problems.

If you're not familiar with my previous thinking, you can check out the OpenACS workflow package (which I had a small hand in) and some lectures I have presented (see the Relevant Links section below).

I re-read an article titled "State Machines & User Interfaces" in the January 2003 Dr Dobb's Journal which got me thinking about my current project and it's overly complex UI code.

The article basically discusses modelling the UI interaction with your application as a finite state machine—which is something I always do. But then it discusses driving the UI from finite state machine code. In a sense many partsd of UI code do exactly that without realising it, but driving the whole thing from a state machine has all sorts of benefits.

It specifically discusses JStateMachine for driving GUI or Web applications with Java Swing or Servlet technology, but the concepts are applicable anywhere.

The bullet point summary of what JStateMachine does is: (quoted directly from the article)

  1. Locate the source state and event on the chart.
  2. Ensure users have permission to fire the event, throwing an exception if not.
  3. Load the IController delegate for the event and ask it for the correct result transition.
  4. Ask the controller to perform any required side effects.
  5. Follow that transition, including computing the history or deep history if required.
  6. Ensure users have permission to enter the result state.
  7. If there is a default event, follow it. Keep processing default events until you end in a state with none, always ensuring permissions are not broken.
  8. Load the IView delegate for the result state and ask it to update the display.

Some other smart things is that the project defines a dtd for specifying the state machine and callbacks etc. in an xml file. It also defines a default view controller that renders a form based on the possible action and required field information in the specification thus allowing you to walk through the whole system and simply fill in the display code as you go.

Alas, jstatemachine.org seems to have gone commercial, but you can still download the old LGPL licensed code from SourceForge. I'm going to investigate porting it to Perl, otherwise I will write a new module from scratch using the same ideas—it just makes too much sense to keep doing large application UI coding any other way. (Of course there are some projects where a finite state machine is the wrong model, but I think you would be surprised...)

Relevant links:

08:26 PM, 18 Jul 2004 by Mark Aufflick Permalink

Add comment