Subscribe Favorite

Statemachine: Finite Statemachine framework

Written on November 25, 2007 by ceefour

The Statemachine library is a simple yet full-featured Finite Statemachine framework. Define your statemachine in Ruby code and execute it in your system.

A statemachine keeps track of the status(state) of an application or device and responds to different inputs, which alter the state of the machine.

We have identified 3 fundamental components to a statemachine: States, Transitions, and Events.

  • State: This is the status of the device or application the statemachine is being used for. At any given time, the statemachine is in one of its predefined states.
  • Transition: Moving from one state to another is called a transition. Transitions are invoked by Events.
  • Event: Events are the inputs to a statemachine.

This is a simple statemachine showing use of states and transitions.

Each transition can be defined by identifying the state where it begins, the event by which is invoked, and the state where it ends.Using this scheme we can define out vending machine like so…

Origin State Event Destination State
Waiting dollar Paid
Paid selection Waiting
Waiting selection Waiting
Paid dollar Paid

Defining it in ruby is not much harder:

require 'rubygems'
require 'statemachine'

vending_machine = Statemachine.build do
  trans :waiting, :dollar, :paid
  trans :paid, :selection, :waiting
  trans :waiting, :selection, :waiting
  trans :paid, :dollar, :paid
end

Actions allow statemachines to perform operations at various point during execution. There are two models for incorporating actions into statemachines.

  • Mealy: A Mealy machine performs actions on transitions. Each transition in a statemachine may invoke a unique action.
  • Moore: A Moore machine performs actions when entering a state. Each state may have it’s own entry action.

Mealy and Moore machines each have advantages and disadvantages. But one great advantage of both it that they are not mutually exclusive. If we use both models, and toss in some exit actions, we’ve got it made!

Oftentimes duplication can arise within a statemachine. One way to solve this problem is through the use of superstates. A superstate is a state that contains other states. One statemachine may have multiple superstates. And every superstate may contain other superstates. Superstates can be nested.But, one problem with superstates is that they may not know which state to return to when coming into that state. To solve this problem, superstates come with the history state.

More detailed download and installation instructions are available on Statemachine library project page.

 

 

Update: An insightful reader, Justin Jones, suggests an alternative: acts_as_state_machine plugin. Read more on Aizatto’s and Practical Ruby.

If you enjoyed this post Subscribe to our feed

 

Trackbacks

(Trackback URL)

  • ourdesigns

    November 25, 2007 at 4:35 pm

    [...] here for full story No Comments so far Leave a comment RSS ...

  • » The Links » roarin’ reporter

    November 26, 2007 at 9:13 pm

    [...] Statemachine: Finite Statemachine Framework [...]

close Reblog this comment
blog comments powered by Disqus