First time here? Please, start at the beginning.

Tuesday, September 7

uChat as an FSM

No, not a Flying Spaghetti Monster, a Finite State Machine.

Wikipedia's article is rather long and dry. To put it briefly, a finite state machine is a device (or program) that has a set number of "states". An example would be a light switch: it has 2 states, "on" and "off". A dimmer switch, to contrast, would be an Infinite State Machine: it has a (nearly) infinite number of positions.

It addition to its set of states, a state machine also has "transition conditions" that cause it to change its state and (usually) a set of reactions that change depending on its state.

Here's another example, you could model a body of water as an FSM. It's states would be "solid", "liquid" and "gas". Its transition conditions would be defined as temperature changes, and its reactions would change depending on its state. If you poke a piece of ice sitting on a table, it will likely slide away; however, if you poke the surface of a pool of water, it will ripple. A slightly odd example, but I believe it serves its purpose.

Virtually every computer program you interact with is a Finite State Machine. Some have obvious states, others are more subtle, but few complex programs can be written without maintaining an internal state and altering its reactions to user input accordingly.

"But," you ask, "how is a simple program like uChat a Finite State Machine?" It may not be obvious from a glance at the source, but it is. A simple one, granted, with only 2 states: "connected" and "disconnected".

Fun fact: this picture's filesize is larger than uChat's source.

Lets list uChat's input sources, they're all part of the UI: the Name field, the Join button, the Message field, and the Send button.

The text area is marked an non-editable, so no user input can come from it. Also, clicking the Send button has the same effect as pressing enter in the Message field. There's a similar relation exists between the Name field and the Join button.

So, simplified slightly to "user intents" (actions the user intends to perform) we now have: "join the chat" (connect), "leave the chat" (disconnect), and "send a message".

The program's possible responses are: join the chat, leave the chat, send a message, ignore.

This post is incomplete. I may or may not return to this subject later, but I believe I've explained sufficiently for you to understand where I was going.

No comments:

Post a Comment