Introduction to Finite State Machines

Reading Time: 2 minutes

In this we are discuss about the Finite State Machines(FSM) implementation using the Akka Actor Model.

Finite State Machines

First we go through the the proper defination of FSM as describes in Erlang Design Principles.

A FSM can be understand in the form of relation.

State(S) x Event(E) -> Actions (A), State(S')
...

This can be define as:

If we are in state S and the event E occurs, we should perform the actions A and make a transition to the state S'.

An FSM is called a machine because it can only be in one of a finite number of states.

Changing from one state to another is triggered by an event or condition.

It has different number of stages and change their stage as different action performed.This changing of stage is know as Transition

Diagram shows the transtion of FSM states

Akka provides this very helpful FSM actor which makes very easy and concise to change between the different state in actor.

As we uses context.become and context.unbecome to switch between the states but sometimes these implementation becomes so hard to understand and debugging.

On the other hand in Akka FSM actor state are switch without the use of receive handler as it works on the action of triggered Events.

Akka FSM basics

To set up and use an FSM actor,we need:-

  • the initial state of the FSM
  • the behaviours of the FSM in each state
  • and optionally what happens when a transition occurs

Example

class Account() extends Actor with FSM[State, StateData] {
  startWith(WaitForRequests, new StateData(0,Seq()))
  when(WaitForRequests) {
    case Event(request:Deposit, data:StateData) => {
      .....
goto(PendingRequests) using data
    }
    case Event(PendingRequests, data:StateData) => {
      ...
    }
  }

  onTransition {
    case WaitForRequests -> PendingRequests => println("state changes from WaitForRequests to PendingRequests")
 }   
...
}

StartWith

The initial state is set with StartWith. StartWith is a function that takes 2 parameters, the initial state and the associated state data.

When

Then we need to provide function that represent the response function that the actor will use when in a particular state. The When methods accepts 3 parameters, The state, a state transition function and a timer.

This Account actor start with initial state WaitForRequests and with data StateData as the different action is trigger it changes its states on the particular event.

goto method is use to switch betwwen the states using the data provides to the particular state.

onTransition

This method is use to triggered event on the action as one state is switches to another state.

Conclusion

Our regular Actor has just one receive function and the FSM trait wraps a sophisticated implementation of the receive method which delegates calls to the block of code that handles the data in a particular state.

It is very helpul to use the FSM to implement the code which have lots of switches from one state to another.

knoldus

Written by 

Akash Kumar is a Software Consultant at Knoldus Software LLP. He has done B.tech from Abdul Kalam Technical University. He is majorly focused on Scala and Angular. On the personnel side, he loves to play Cricket and online video games.