![]() |
Articles Feed |
Categories
Archives
- March 2010 (1)
- February 2010 (2)
- January 2010 (1)
- December 2009 (1)
- October 2009 (4)
- September 2009 (2)
- August 2009 (2)
- July 2009 (5)
- June 2009 (2)
- May 2009 (2)
- April 2009 (8)
- March 2009 (7)
- January 2009 (2)
- December 2008 (3)
- November 2008 (5)
- October 2008 (4)
- September 2008 (6)
- August 2008 (4)
- July 2008 (5)
- June 2008 (5)
- May 2008 (4)
- April 2008 (2)
- February 2008 (4)
- January 2008 (2)
- December 2007 (2)
- November 2007 (2)
- October 2007 (2)
- September 2007 (1)
- August 2007 (3)
- July 2007 (1)
- June 2007 (4)
- May 2007 (7)
- April 2007 (2)
- February 2007 (3)
- January 2007 (3)
- November 2006 (3)
- October 2006 (3)
- September 2006 (17)
- November 2004 (1)
Understanding Statemachines, Part 2: Actions
by: micah | November 29th, 2006 |
Actions
Part 1 demonstrated how to build states and transitions. Add some actions to that and you’ve got a truly useful statemachine. 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!
Example:
Remember the vending machine statemachine. It had some problems. Adding some actions will solve many of them. Here’s the same statemachine with actions.

The Vending Machine Statemachine Diagram, Version 2
You can see I’ve added three transition actions (the Mealy type). Check out the transition from Waiting to Paid. When this transition is triggered the activate action will be called which will activate the hardware that dispenses goodies. Also, when a selection is made, transitioning from Paid to Waiting, the release action will cause the hardware to release the selected product. Finally, this version of the vending machine won’t steal your money any more. When an extra dollar is inserted, the refund event is invoked and the dollar is refunded.
Notice that the Waiting state has an entry action (Moore type) and an exit action. When ever the Waiting states is entered, the sales_mode action is invoked. The intent of this action is to make the vending machine blink or flash or scroll text; whatever it takes to attract customers. When the Waiting state is exited, the vending will go into operation_mode where all the blinking stops so the customer do business.
Implementation:
Here’s how the new vending machine can be implemented in Ruby:
- vending_machine = Statemachine.build do
- state :waiting do
- event :dollar, :paid, :activate
- event :selection, :waiting
- on_entry :sales_mode
- on_exit :operation_mode
- end
- trans :paid, :selection, :waiting, :release
- trans :paid, :dollar, :paid, :refund
- context VendingMachineContext.new
- end
There are several new tricks to learn here. First is the state method. This is the formal syntax for declaring a state. The informal syntax is the trans method which we’ve already seen. The state method requires the state id and an option block. Every method invoked within the block is applied to the state being declared.
With a state block you may declare events, entry actions, and exit actions. The event method is used to declare transition out of the current state. Its parameters are the event, destination state, and an optional action. The on_entry and on_exit methods are straight forward. They take one parameter: an action. (See below for more on action syntax)
After the waiting state declaration we see the familiar calls to trans. The trans method takes an option 4th action parameter. You can see that the release and refund actions were added this way.
Context:
The final line sets the context of the statemachine. This is an interesting aspect. Every statemachine may have a context and if your statemachine has actions, you should definitely give it a context. Every action of a statemachine will be executed within its context object. We’ll discuss this more later.
Here is a simple context for the vending machine statemachine.
- class VendingMachineContext
- def activate
- puts "activating"
- end
- def release(product)
- puts "releasing product: #{product}"
- end
- def refund
- puts "refuding dollar"
- end
- def sales_mode
- puts "going into sales mode"
- end
- def operation_mode
- puts "going into operation mode"
- end
- end
Action Declarations:
With the statemachine gem, actions can be declared in any of three forms: Symbols, String, or Block.
When the action is a Symbol, (on_entry :sales_mode) it is assumes that there is a method by the same name on the context class. This method will be invoked. Any parameters in with the event will be passed along to the invoked method.
String actions should contains ruby code (on_entry "puts 'entering sales mode'"). The string will use invoked with in the context object using instance_eval. Strings allow quick and dirty actions without the overhead of defining methods on your context class. The disadvantage of String actions is that they cannot accept parameters.
If the action is a Proc (on_entry Proc.new {puts 'entering sales mode'}), it will be called within the context of the context. Proc actions are also nice for quick and dirty actions. They can accept parameters and are preferred to String actions, unless you want to marshal your statemachine. Using one Proc actions will prevent the entire statemachine from being marhsal-able.
Execution
For kicks let’s put this statemachine thought a few events.
- vending_machine.dollar
- vending_machine.dollar
- vending_machine.selection "Peanuts"
Here’s the output:
going into operation mode activating refuding dollar releasing product: Peanuts going into sales mode
That sums it up for actions. Next, we’ll talk about how do deal with conditional login in your statemachine.
Understanding Statemachines, Part 3: Conditional Logic

October 18th, 2008 at 06:32 PM Well, I'm not sure if this really provides a full UNDERSTANDING of Statemachines (like, what they are, and what that definition means) but I think (as a newbie) that I'm understanding a little of where you're going with this. It may be a little more clear if I take a stab at it anyway as well as take a read of part two. Maybe part two will put things in better context for me so I'll have a broader understanding.
November 15th, 2008 at 07:25 AM
good
October 14th, 2009 at 01:10 PM
There is a slight error with the output. There should one additional line at the beginning to represent the initial state of the statemachine.