Understanding Statemachines, Part 3: Conditional Logic

If you’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.

Conditional logic can be accomplished by using entry actions. See the diagram
below.

State Diagram with Conditional Logic

Starting in the Accept Money state, when a coin is inserted, the
coin event is fired and the Statemachine transitions into the Coin
Inserted state. This is where it gets fun.

Upon entering of the Coin Inserted state its entry event is
invoked: count_amount_tendered. This method will count the money
and invoke the not_paid_yet or paid event
accordingly. This will cause the Statemachine to transition into the
appropriate state.

The Coin Inserted state is unique. You wouldn’t expect to find
the Statemachine in the Coin Inserted state for any reason
except to make this decision. Once the decision is made, the state changes.
States like this are called Decision States.