Introduction to State Machines

5,174 views

Published on

An Introduction to State Machines - presented by CodeOffcer (Russell Jones) at the Ruby Users Group in Westbrook Maine.

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,174
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
112
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Introduction to State Machines

  1. 1. STATE MACHINES <ul><li>By CodeOfficer </li></ul>codeofficer.com github.com/code officer twitter.com/co deofficer
  2. 2. State Machines? A model of behavior composed of a finite number of states, transitions between those states, and actions. A finite state machine is an abstract model of a machine with a primitive internal memory. http://en.wikipedia.org/wiki/Finite_state_machine
  3. 3. <ul><li>Spree for processing orders </li></ul><ul><li>in_progress, new, canceled, returned, resumed, paid, shipped </li></ul><ul><li>RestfulAuthentication </li></ul><ul><li>passive, pending, active, suspended, deleted </li></ul><ul><li>ActiveModel in Rails </li></ul><ul><li>validations, callbacks, observers </li></ul><ul><li>Tracks (GTD) for todo statuses </li></ul><ul><li>active, project_hidden, completed, deferred </li></ul>Common Uses?
  4. 4. <ul><li>Machine </li></ul><ul><li>States </li></ul><ul><li>Events </li></ul><ul><li>Transitions </li></ul>So, What’s in a State Machine?
  5. 5. Machines? <ul><li>Consist of states, events and transitions that define how a state changes after an event is fired </li></ul><ul><li>Model behavior for a class via an attribute of that class </li></ul><ul><li>Multiple state machines can be used in a class, each one tracked by a unique attribute </li></ul>
  6. 6. States? <ul><li>Have an initial state </li></ul><ul><li>Represent the value for a particular machine in an attribute of your class </li></ul><ul><li>Attribute can be a value of any type. The default type in Ruby is often “String” </li></ul><ul><li>Can be used to define a behavioral context for a given machine </li></ul>
  7. 7. Events? <ul><li>Define an action that transitions a machine from one state to another </li></ul><ul><li>Guards can be put in place to make state transitions conditional when an action is fired </li></ul>
  8. 8. Gems and Plugins State Machine A plugin By Aaron Pfeifer (pluginaweek) http://github.com/pluginaweek/state_machine/ http://api.pluginaweek.org/state_machine/ Acts As State Machine A gem By Scott Barron http://github.com/rubyist/aasm/ Which state machine project will have the most github watchers by RailsConf 2009? https://opensource.inklingmarkets.com/markets/18368
  9. 9. A Simple Machine (using the State Machine plugin) (using the State Machine plugin) class Light < ActiveRecord::Base attr_accessor :intensity state_machine :state, :initial =>:off do state :off { def intensity; 0; end } state :low { def intensity; 5; end } state :high { def intensity; 10; end } event :switch do transition :off => :low, :low => :high, :high => :off end end end @light = Light.new @light.state #=> “off” @light.intensity #=> 0 @light.off? #=> true @light.can_switch? #=> true @light.switch! #=> true @light.state #=> “low” @light.intensity #=> 5 @light.off? #=> false @light.can_switch? #=> true @light.switch! # repeat and rinse ...
  10. 10. Another Machine (also using the State Machine plugin) (also using the State Machine plugin) class Vehicle state_machine :initial => :parked do event :park do transition [ :idling , :first_gear ] => :parked end event :ignite do transition :stalled => same, :parked => :idling end event :idle do transition :first_gear => :idling end event :shift_up do transition :idling => :first_gear , :first_gear => :second_gear , :second_gear => :third_gear end event :shift_down do transition :third_gear => :second_gear , :second_gear => :first_gear end event :crash do transition [ :first_gear , :second_gear , :third_gear ] => :stalled end event :repair do transition :stalled => :parked end end end
  11. 11. Machine Integrations (for the State Machine plugin) (for the State Machine plugin) <ul><li>Database Transactions </li></ul><ul><li>every transition is wrapped within a transaction </li></ul><ul><li>Automatically Saves Records </li></ul><ul><li>@thing.event vs @thing.event! (bang throws an exception on fail) </li></ul><ul><li>Named Scopes </li></ul><ul><li>Thing.with_state(:off).all or Thing.with_state([:off, :on]).all </li></ul><ul><li>Validation Rrrors </li></ul><ul><li>@thing.errors.full_messages # => [&quot;State cannot ... via :xxx from :yyy&quot;] </li></ul><ul><li>Observers </li></ul><ul><li>observers can hook into before/after callbacks for events and generic transitions </li></ul>
  12. 12. Textmate Bundles <ul><li>State Machine </li></ul><ul><li>http://github.com/drnic/ruby-state-machine-tmbundle/ </li></ul><ul><li>Acts As State Machine </li></ul><ul><li>http://github.com/levicole/acts-as-state-machine-tm-bundle/ </li></ul>Auto completion for machines, events, transitions and callbacks

×