@Cybrilla » January 24, 2013

Daily Archives: January 24, 2013

Uncategorized

Statemachine

Published by:

What is Statemachine?

Statemachine is any device that stores the status of something at a given time and can operate on input to change the status and/or cause an action or output to take place for any given change. A computer is basically a state machine and each machine instruction is input that changes one or more states and may cause other actions to take place.

example1: Turnstile

Statemachine diagram for turnstile

Turnstile has two states:
1) states: locked and unlocked
2) There are two inputs that affect its state: putting a coin in the slot (coin) and pushing the arm (push).
3) In the locked state, pushing on the arm has no effect; no matter how many times the input push is given it stays in the locked state. Putting a coin in, that is giving the machine a coin input, shifts the state from Locked to Unlocked.
4) In the unlocked state, putting additional coins in has no effect; that is, giving additional coin inputs does not change the state. However, a customer pushing through the arms, giving a push input, shifts the state back to Locked.

Statemachine has 3 fundamental components:
1) States
2) Events
3) Transactions

From above example: we have
States: locked and unlocked
Events : putting the coin and push the arm
Transactions: locked to unlocked, unlocked to locked

example2: Vending Machine
Uml diagram: States and Transitions:
The Statemachine that runs a simple vending machine.
The Statemachine that runs a simple vending machine.

From the above UML diagram: we have
States: Waiting, Paid
Events : dollar, selection
Transactions: waiting to paid, paid to waiting

What is event?
Event is an action or trigger, which invoke the transition to change the state of the object.

What is Transition?
Each transition can be defined by identifying the state where it begins, the event by which is invoked, and the state where it ends.

Ruby Toolbox Statemachine

Ruby Toolbox Statemachine has list of gems which supports statemachine functionality.

Why we need statemachine functionality in ruby?
The main reason for using state machines is to help the design process. It is much easier to figure out all the possible edge conditions by drawing out the state machine on paper. This will make sure that your application will have less bugs and less undefined behavior. Also, it clearly defines which parts of the internal state of your object are exposed as external API.

Most web apps contain several examples of statemachine, like accounts, blog posts, orders, invoices and many more.The problem is we never think of them as statemachines while designing your application. Therefore, it is good to have some indicators to recognize them early.

Since Ruby has list of gems.Among them, below 3 gems are fully-featured,
statemachine
aasm
workflow

The above three gems vary in their syntax and some additional features.
You can find the detailed explanation of:
statemachine
@:https://github.com/pluginaweek/state_machine
aasm
@:https://github.com/aasm/aasm
workflow
@: http://rubydoc.info/gems/workflow/frames

Table: List of features in gems.

STATEMACHINE AASM WORKFLOW
Callbacks yes yes yes
Guards(Conditional transition) yes yes yes
Automatic Scope no yes yes
Validation errors yes no no
Dirty attributes yes no no
Tracking & Observers yes no no
Transaction Support yes yes yes
Named Scope yes no no
Transition Context yes no no
Generating Graphs yes no no
Transaction Event Handler no no yes
Inheritance yes no yes
Transition hooks yes no yes
Integration with ActiveRecord yes yes yes
Integration with couchDB no no yes
Integration with Mongoid yes no no
Integration with ActiveModel,
datamapper,mongomapper & sequel
yes no no
Static/dynamic definitions yes no no
Core Extensions yes no no