<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>8th Light Blog: Category Statemachine</title>
    <link>http://blog.8thlight.com/articles/category/statemachine</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In the minds of the craftsmen...</description>
    <item>
      <title>Chirb Statemachine Talk</title>
      <description>&lt;p&gt;Last night I presented the Ruby Statemachine Gem to the &lt;a href="http://chirb.org"&gt;Chicago Ruby Users Group (Chirb)&lt;/a&gt; Below are links to download the slides and coding example used.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Finite State Machines and the Statemachine Ruby Gem (Slides):&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="/files/statemachines_chirb0407.pdf"&gt;statemachines_chirb0407.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;Statemachine Exercises:&lt;/b&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="/files/statemachine_exercise.zip"&gt;statemachine_exercise.zip&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="/files/parser_spec.rb"&gt;parser_spec.rb - Additional specs contributed by Michael Buselli&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Tue, 08 May 2007 07:28:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:99319e73-6711-43e6-9954-9d55a3823c8d</guid>
      <author>Micah</author>
      <link>http://blog.8thlight.com/articles/2007/05/08/chirb-statemachine-talk</link>
      <category>Statemachine</category>
      <category>Micah</category>
      <enclosure type="text/x-ruby-script" length="1334" url="http://blog.8thlight.com/files/parser_spec.rb"/>
    </item>
    <item>
      <title>Understanding Statemachines, Part 4: Superstates</title>
      <description>&lt;h3&gt;Superstates&lt;/h3&gt;

&lt;p&gt;Often in statemachines, duplication can arise. For example, the vending machine in our examples may need periodic repairs.  It&amp;#8217;s not certain which state the vending machine will be in when the repair man arrives.  So all states should have a transition into the &lt;code&gt;Repair Mode&lt;/code&gt; state.&lt;/p&gt;

&lt;div style="text-align: center;"&gt;&lt;img width="400" style="border: 1px solid black" src="/files/vending_machine4b.png"&gt;&lt;br/&gt;&lt;b&gt;Diagram 1 - Without Superstates&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;In this diagram, both the &lt;code&gt;Waiting&lt;/code&gt; and &lt;code&gt;Paid&lt;/code&gt; states have a transition to the &lt;code&gt;Repair Mode&lt;/code&gt; invoked by the &lt;code&gt;repair&lt;/code&gt; event.  Duplication!  We can dry this up by using the &lt;b&gt;Superstate&lt;/b&gt; construct. See below:&lt;/p&gt;

&lt;div style="text-align: center;"&gt;&lt;img width="400" style="border: 1px solid black" src="/files/vending_machine4a.png"&gt;&lt;br/&gt;&lt;b&gt;Diagram 2 - With Superstates&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;Here we introduce the &lt;code&gt;Operational&lt;/code&gt; superstate.  Both the &lt;code&gt;Waiting&lt;/code&gt; and &lt;code&gt;Paid&lt;/code&gt; states are contained within the superstate which implies that they inherit all of the superstate&amp;#8217;s transitions.  That means we only need one transition into the &lt;code&gt;Repair Mode&lt;/code&gt; state from the &lt;code&gt;Operational&lt;/code&gt; superstate to achieve the same behavior as the solution in &lt;i&gt;diagram 1&lt;/i&gt;.  &lt;/p&gt;

&lt;p&gt;One statemachine may have multiple superstates.  And every superstate may contain other superstates. ie. Superstates can be nested.&lt;/p&gt;

&lt;h3&gt;History State&lt;/h3&gt;

&lt;p&gt;The solution in &lt;i&gt;diagram 2&lt;/i&gt; has an advantage over &lt;i&gt;diagram 1&lt;/i&gt;.  In &lt;i&gt;diagram 1&lt;/i&gt;, once the repair man is done he triggers the &lt;code&gt;operate&lt;/code&gt; event and the vending machine transitions into the &lt;code&gt;Waiting&lt;/code&gt; event.  This is unfortunate.  Even if the vending machine was in the &lt;code&gt;Paid&lt;/code&gt; state before the repair man came along, it will be in the &lt;code&gt;Waiting&lt;/code&gt; state after he leaves.  Shouldn&amp;#8217;t it go back into the &lt;code&gt;Paid&lt;/code&gt; state?&lt;/p&gt;

&lt;p&gt;Superstates come with the &lt;b&gt;history state&lt;/b&gt; which solves this problem.  Every superstate will remember which state it is in before the superstate is exited.  This memory is stored in a pseudo state called the &lt;b&gt;history state&lt;/b&gt;.  Transitions that end in the history state will recall the last active state of the superstate and enter it.  &lt;/p&gt;

&lt;p&gt;You can see the history state being use in &lt;i&gt;diagram 2&lt;/i&gt;.  In this solution, the history state allows the vending machine to return from a repair session into the same state it was in before, as though nothing happened at all.&lt;/p&gt;

&lt;h3&gt;Code&lt;/h3&gt;

&lt;p&gt;The following code builds the statemachine in &lt;i&gt;diagram 2&lt;/i&gt;.  Watch out for the &lt;code&gt;_H&lt;/code&gt;.  This is how the history state is denoted.  If you have a superstate named &lt;code&gt;foo&lt;/code&gt;, then it&amp;#8217;s history state will be named &lt;code&gt;foo_H&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'statemachine'

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

    event :repair, :repair_mode,  Proc.new { puts "Entering Repair Mode" }
  end

  trans :repair_mode, :operate, :operational_H, Proc.new { puts "Exiting Repair Mode" }

  on_entry_of :waiting, Proc.new { puts "Entering Waiting State" }
  on_entry_of :paid, Proc.new { puts "Entering Paid State" }
end

vending_machine.repair
vending_machine.operate
vending_machine.dollar
vending_machine.repair
vending_machine.operate&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;Entering Repair Mode
Exiting Repair Mode
Entering Waiting State
Entering Paid State
Entering Repair Mode
Exiting Repair Mode
Entering Paid State&lt;/pre&gt;

&lt;p&gt;Next we should cover pseudo states.&lt;/p&gt;</description>
      <pubDate>Sat, 07 Apr 2007 21:19:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3fb702aa-c403-4fad-9760-b5431dbf81f7</guid>
      <author>Micah</author>
      <link>http://blog.8thlight.com/articles/2007/04/07/understanding-statemachines-part-4-superstates</link>
      <category>Coding</category>
      <category>Statemachine</category>
      <category>Micah</category>
    </item>
    <item>
      <title>Understanding Statemachines, Part 3: Conditional Logic</title>
      <description>&lt;h3&gt;Conditions&lt;/h3&gt;

&lt;p&gt;If you&amp;#8217;re doing any significant amount of work with statmachines, you will most certainly encounter some conditional logic in your statemachines.  Take our vending machine.  When ever a coin is inserted, the invoked event will depend on whether the total amount of money inserted is sufficient to buy something.  If enough money has been tendered, the display should suggest that the customer make a selection.  If insufficient money has been inserted, the customer should be prompted to insert more.
&lt;br/&gt;&lt;br/&gt;
Conditional logic can be accomplished by using &lt;b&gt;entry actions&lt;/b&gt;.  See the diagram below.&lt;/p&gt;

&lt;div style="text-align: center;"&gt;&lt;img width="400" style="border: 1px solid black" src="/files/vending_machine3.png"&gt;&lt;br/&gt;&lt;b&gt;State Diagram with Conditional Logic&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;Starting in the &lt;code&gt;Accept Money&lt;/code&gt; state, when a coin is inserted, the &lt;code&gt;coin&lt;/code&gt; event is fired and the statemachine transitions into the &lt;code&gt;Coin Inserted&lt;/code&gt; state.  This is where it gets fun.  Upon entering of the &lt;code&gt;Coin Inserted&lt;/code&gt; state its entry event is invoked: &lt;code&gt;count_amount_tendered&lt;/code&gt;.  This method will count the money and invoke the &lt;code&gt;not_paid_yet&lt;/code&gt; or &lt;code&gt;paid&lt;/code&gt; event accordingly.  This will cause the statemachine to transition into the appropriate state.
&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;Coin Inserted&lt;/code&gt; state is unique.  You wouldn&amp;#8217;t expect to find the statemachine in the &lt;code&gt;Coin Inserted&lt;/code&gt; state for any reason except to make this decision.  Once the decision is made, the state changes.  States like this are called &lt;b&gt;Decision States&lt;/b&gt;.&lt;/p&gt;

&lt;h3&gt;Code&lt;/h3&gt;

&lt;pre&gt;require 'rubygems'
require 'statemachine'

class VendingMachineContext

  attr_accessor :statemachine

  def initialize
    @amount_tendered = 0
  end

  def add_coin
    @amount_tendered = @amount_tendered + 25
  end

  def count_amount_tendered
    if @amount_tendered &gt;= 100
      @statemachine.paid
    else
      @statemachine.not_paid_yet
    end
  end

  def prompt_money
    puts "$.#{@amount_tendered}: more money please"
  end

  def prompt_selection
    puts "please make a selection"
  end
end

vending_machine = Statemachine.build do
  trans :accept_money, :coin, :coin_inserted, :add_coin
  state :coin_inserted do
    event :not_paid_yet, :accept_money, :prompt_money
    event :paid, :await_selection, :prompt_selection
    on_entry :count_amount_tendered
  end
  context VendingMachineContext.new
end
vending_machine.context.statemachine = vending_machine

vending_machine.coin
vending_machine.coin
vending_machine.coin
vending_machine.coin&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;$.25: more money please
$.50: more money please
$.75: more money please
please make a selection&lt;/pre&gt;

&lt;p&gt;Next lesson: Superstates&lt;br/&gt;
&lt;a href="/articles/2007/04/07/understanding-statemachines-part-4-superstates"&gt;Understanding Statemachines, Part 4: Superstates&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 13 Feb 2007 03:34:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a7c8f8e8-9f01-4b56-90b9-dcedc59b2ede</guid>
      <author>Micah</author>
      <link>http://blog.8thlight.com/articles/2007/02/13/understanding-statemachines-part-3-conditional-logic</link>
      <category>Coding</category>
      <category>Statemachine</category>
      <category>Micah</category>
      <enclosure type="image/png" length="6987" url="http://blog.8thlight.com/files/vending_machine31.png"/>
    </item>
    <item>
      <title>Understanding Statemachines, Part 2: Actions</title>
      <description>&lt;h3&gt;Actions&lt;/h3&gt;

&lt;p&gt;Part 1 demonstrated how to build states and transitions.  Add some actions to that and you&amp;#8217;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.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mealy:&lt;/strong&gt; A Mealy machine performs actions on transitions.  Each transition in a statemachine may invoke a unique action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moore:&lt;/strong&gt; A Moore machine performs actions when entering a state.  Each state may have it&amp;#8217;s own entry action.&lt;/p&gt;

&lt;p&gt;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&amp;#8217;ve got it made!&lt;/p&gt;

&lt;h3&gt;Example:&lt;/h3&gt;

&lt;p&gt;Remember the vending machine statemachine.  It had some problems.  Adding some actions will solve many of them.  Here&amp;#8217;s the same statemachine with actions.&lt;/p&gt;

&lt;div style="text-align: center;"&gt;&lt;img style="border: 1px solid black" src="/files/vending_machine2.png"&gt;&lt;br/&gt;&lt;b&gt;The Vending Machine Statemachine Diagram, Version 2&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;You can see I&amp;#8217;ve added three transition actions (the Mealy type).  Check out the transition from &lt;em&gt;Waiting&lt;/em&gt; to &lt;em&gt;Paid&lt;/em&gt;.  When this transition is triggered the &lt;em&gt;activate&lt;/em&gt; action will be called which will activate the hardware that dispenses goodies.  Also, when a selection is made, transitioning from &lt;em&gt;Paid&lt;/em&gt; to &lt;em&gt;Waiting&lt;/em&gt;, the &lt;em&gt;release&lt;/em&gt; action will cause the hardware to release the selected product.  Finally, this version of the vending machine won&amp;#8217;t steal your money any more.  When an extra dollar is inserted, the &lt;em&gt;refund&lt;/em&gt; event is invoked and the dollar is refunded.&lt;/p&gt;

&lt;p&gt;Notice that the &lt;em&gt;Waiting&lt;/em&gt; state has an entry action (Moore type) and an exit action.  When ever the &lt;em&gt;Waiting&lt;/em&gt; states is entered, the &lt;em&gt;sales_mode&lt;/em&gt; 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 &lt;em&gt;Waiting&lt;/em&gt; state is exited, the vending will go into &lt;em&gt;operation_mode&lt;/em&gt; where all the blinking stops so the customer do business.&lt;/p&gt;

&lt;h3&gt;Implementation:&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s how the new vending machine can be implemented in Ruby:&lt;/p&gt;

&lt;pre&gt;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&lt;/pre&gt;

&lt;p&gt;There are several new tricks to learn here.  First is the &lt;code&gt;state&lt;/code&gt; method.  This is the formal syntax for declaring a state.  The informal syntax is the &lt;code&gt;trans&lt;/code&gt; method which we&amp;#8217;ve already seen.  The &lt;code&gt;state&lt;/code&gt; method requires the state id and an option block.  Every method invoked within the block is applied to the state being declared.  &lt;/p&gt;

&lt;p&gt;With a &lt;code&gt;state&lt;/code&gt; block you may declare events, entry actions, and exit actions.  The &lt;code&gt;event&lt;/code&gt; method is used to declare transition out of the current state.  Its parameters are the event, destination state, and an optional action. The &lt;code&gt;on_entry&lt;/code&gt; and &lt;code&gt;on_exit&lt;/code&gt; methods are straight forward.  They take one parameter: an action. (See below for more on action syntax)&lt;/p&gt;

&lt;p&gt;After the &lt;code&gt;waiting&lt;/code&gt; state declaration we see the familiar calls to &lt;code&gt;trans&lt;/code&gt;.        The &lt;code&gt;trans&lt;/code&gt; method takes an option 4th action parameter.  You can see that the &lt;code&gt;release&lt;/code&gt; and &lt;code&gt;refund&lt;/code&gt; actions were added this way.&lt;/p&gt;

&lt;h3&gt;Context:&lt;/h3&gt;

&lt;p&gt;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&amp;#8217;ll discuss this more later.&lt;/p&gt;

&lt;p&gt;Here is a simple context for the vending machine statemachine.&lt;/p&gt;

&lt;pre&gt;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&lt;/pre&gt;

&lt;h3&gt;Action Declarations:&lt;/h3&gt;

&lt;p&gt;With the statemachine gem, actions can be declared in any of three forms: Symbols, String, or Block.  &lt;/p&gt;

&lt;p&gt;When the action is a &lt;strong&gt;Symbol&lt;/strong&gt;, (&lt;code&gt;on_entry :sales_mode&lt;/code&gt;) it is assumes that there is a method by the same name on the &lt;code&gt;context&lt;/code&gt; class.  This method will be invoked.  Any parameters in with the event will be passed along to the invoked method. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;String actions&lt;/strong&gt; should contains ruby code (&lt;code&gt;on_entry "puts 'entering sales mode'"&lt;/code&gt;). The string will use invoked with in the &lt;code&gt;context&lt;/code&gt; object using &lt;code&gt;instance_eval&lt;/code&gt;.  Strings allow quick and dirty actions without the overhead of defining methods on your &lt;code&gt;context&lt;/code&gt; class.  The disadvantage of String actions is that they cannot accept parameters. &lt;/p&gt;

&lt;p&gt;If the action is a &lt;strong&gt;Proc&lt;/strong&gt; (&lt;code&gt;on_entry Proc.new {puts 'entering sales mode'}&lt;/code&gt;), it will be called within the context of the &lt;code&gt;context&lt;/code&gt;.  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.&lt;/p&gt;

&lt;h3&gt;Execution&lt;/h3&gt;

&lt;p&gt;For kicks let&amp;#8217;s put this statemachine thought a few events.&lt;/p&gt;

&lt;pre&gt;vending_machine.dollar
vending_machine.dollar
vending_machine.selection "Peanuts"&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s the output:&lt;/p&gt;

&lt;pre&gt;going into operation mode
activating
refuding dollar
releasing product: Peanuts
going into sales mode&lt;/pre&gt;

&lt;p&gt;That sums it up for actions.  Next, we&amp;#8217;ll talk about how do deal with conditional login in your statemachine.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blog.8thlight.com/articles/2007/02/13/understanding-statemachines-part-3-conditional-logic"&gt;Understanding Statemachines, Part 3: Conditional Logic&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 30 Nov 2006 04:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:32fa4095-dee0-4c09-ae56-a0dcb85ef842</guid>
      <author>Micah</author>
      <link>http://blog.8thlight.com/articles/2006/11/30/understanding-statemachines-part-2-actions</link>
      <category>Coding</category>
      <category>Statemachine</category>
      <category>Micah</category>
      <enclosure type="image/png" length="14732" url="http://blog.8thlight.com/files/vending_machine2.png"/>
    </item>
    <item>
      <title>Understanding Statemachines, Part 1: States and Transitions</title>
      <description>&lt;h3&gt;Introduction:&lt;/h3&gt;

&lt;p&gt;I consider State Machines to be a programming gem.  An invaluable tool for the software craftsman&amp;#8217;s toolkit.  It&amp;#8217;s not everyday that a statemachine comes in handly, but for some problems statemachines are the most elegant and robust solution you&amp;#8217;ll find.&lt;/p&gt;

&lt;p&gt;Perhaps you learned about Finite State Automata in school but could use a refresher.  Or perhaps you&amp;#8217;ve never heard of these crazy statemachines in your entire software career and your curiosity is piqued.  This is a place to learn more.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve found statemachines so valuable I&amp;#8217;ve build a &lt;a href="http://rubyforge.org/projects/statemachine/"&gt;Ruby framework to build statemachines&lt;/a&gt;.  I hope you find this tool valuable&amp;#8230; but for that to happen you have to understand statemachines.  To that end, this is the first installment of a complete statemachine lesson.  Statemachines are simple.  You&amp;#8217;ll see.&lt;/p&gt;

&lt;h3&gt;States and Transitions:&lt;/h3&gt;

&lt;div style="text-align: center;"&gt;&lt;img style="border: 1px solid black" src="/files/vending_machine.png"&gt;&lt;br/&gt;&lt;b&gt;The Vending Machine Statemachine Diagram&lt;/b&gt;&lt;/div&gt;

&lt;p&gt;Above is a UML diagram of the statemachine the runs a simple vending machine.  We can see that there are two rectangles with rounded corners.  These are &lt;strong&gt;States&lt;/strong&gt;.  The vending machine has two possible states, &lt;em&gt;Waiting&lt;/em&gt; and &lt;em&gt;Paid&lt;/em&gt;.  At any given time during execution, the vending machine will be in one of these states.&lt;/p&gt;

&lt;p&gt;Note the arrows going from one state to another.  These are called &lt;strong&gt;Transitions&lt;/strong&gt;. Transitions are how statemachines change state.  Also note that each transition is labeled with an &lt;strong&gt;Event&lt;/strong&gt;.  Events are the input to statemachines. They invoke transitions.  For example, when the vending machine is in the &lt;em&gt;Waiting&lt;/em&gt;  state and the &lt;em&gt;dollar&lt;/em&gt; event is received, the statemachine will transition into the &lt;em&gt;Paid&lt;/em&gt; state.  When in the paid state and the &lt;em&gt;selection&lt;/em&gt; event is received, the statemachine will transition back into the &lt;em&gt;Waiting&lt;/em&gt; state.&lt;/p&gt;

&lt;p&gt;This should seem reasonable.  Imagine a real vending machine.  When you walk up to it it&amp;#8217;s waiting for you to put money in.  You pay by sticking a dollar in and then you make your selection.  After this happy transaction, the vending machine waits for the next client.&lt;/p&gt;

&lt;p&gt;This scenario is not the only possibility though.  Statemachine are very helpful in examining all possible flows through the system.  Take the &lt;em&gt;Waiting&lt;/em&gt; state.  We don&amp;#8217;t normally expect users to make selections if they haven&amp;#8217;t paid but it&amp;#8217;s a possibility.  As you can see this unexpected event is handled by our vending machine.  It will simply continue to wait for your dollar.  And it would be foolish for someone to put more money in the the vending machine if they&amp;#8217;ve already paid. Foolish or not, you and I know it happens.  Our vending machine handles this graciously by taking the money and allowing the user to make a selection for the fist dollar they supplied.  Effectively the client loses the extra money they put in. &lt;em&gt;(grin)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Implementing the Statemachine:&lt;/h3&gt;

&lt;p&gt;We have identified 3 fundamental components to a statemachine: &lt;strong&gt;States, Transitions, and Events&lt;/strong&gt;.  It turns out that the simplest way to define a statemachine is to define its 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&amp;#8230;&lt;/p&gt;

&lt;table style="border: 1px solid black; margin: 10px;"&gt;
&lt;tr&gt;&lt;th&gt;Origin State&lt;/th&gt;&lt;th&gt;Event&lt;/th&gt;&lt;th&gt;Destination State&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Waiting&lt;/td&gt;&lt;td&gt;dollar&lt;/td&gt;&lt;td&gt;Paid&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Paid&lt;/td&gt;&lt;td&gt;selection&lt;/td&gt;&lt;td&gt;Waiting&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Waiting&lt;/td&gt;&lt;td&gt;selection&lt;/td&gt;&lt;td&gt;Waiting&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Paid&lt;/td&gt;&lt;td&gt;dollar&lt;/td&gt;&lt;td&gt;Paid&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Defining it in ruby is not much harder:&lt;/p&gt;

&lt;pre&gt;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&lt;/pre&gt;

&lt;p&gt;The above snippet assumes you have the &lt;em&gt;statemachine&lt;/em&gt; gem installed.&lt;/p&gt;

&lt;pre&gt;Mac users$ sudo gem install statmachine
Windows users&gt; gem install statemachine&lt;/pre&gt;

&lt;p&gt;The outcome of this code an instance of Statemachine stored in the variable named &lt;code&gt;vending_machine&lt;/code&gt;.  To use our statemachine we need to send events to it.  This is done by calling methods that correspond to events.&lt;/p&gt;

&lt;pre&gt;puts vending_machine.state
vending_machine.dollar
puts vending_machine.state
vending_machine.selection
puts vending_machine.state&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s it for the basics.&lt;/p&gt;

&lt;p&gt;This concludes Part 1 of the lessing.  Next we&amp;#8217;ll learn how to make our statemachine more functional with by adding actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="/articles/2006/11/30/understanding-statemachines-part-2-actions"&gt;Understanding Statemachines, Part 2: Actions&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 17 Nov 2006 05:25:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e99b9d85-6e6a-4239-9a74-7763bd8a5b2c</guid>
      <author>Micah</author>
      <link>http://blog.8thlight.com/articles/2006/11/17/understanding-statemachines-part-1-states-and-transitions</link>
      <category>Coding</category>
      <category>Statemachine</category>
      <category>Micah</category>
      <enclosure type="image/png" length="4594" url="http://blog.8thlight.com/files/vending_machine.png"/>
    </item>
  </channel>
</rss>
