No description or website provided.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Tiny State Machine Trait

This is a tiny state machine with a single public method trigger, which takes an Event which is an edge between two states, and will progress the current state into the next.

More than anything this is an attempt to put down on paper the pattern I'm using elsewhere.


Below I will build a classic State Machine:

import com.conbere.statemachine._

The "Turnstile" which has two states:

case object Locked extends State
case object UnLocked extends State

When it's locked, the machine sit's waiting for someone to feed it coins.

case class Coin(val value: Int) extends Event

If the coins exceed the cost of the turnstile it can be Pushed, the person can pass, and it will return to Locked.

case object Push extends Event

Let's look at the code given those states and events:

class Turnstile(val value: Int, val state: State)
extends StateMachine[Turnstile] {
  def this() = this(0, Locked)

  val requiredPayment = 10

  override def defaultTransition: Transition = {
    case _ => this

  val transitions: Transition = {
    case (Locked, Coin(v)) =>
      if (value + v >= requiredPayment)
        new Turnstile(0, UnLocked)
        new Turnstile(value + v, state)
    case (Locked, Push) =>
    case (UnLocked, Push) =>
      new Turnstile()
    case (UnLocked, Coin(_)) =>

  override def toString = "Turnstile: %s: %s".format(state, value)