Finite State Machines (FSM)

Finite State Machines is a model of computation that can be used to model the behavior of a system.

Example


Here is an example of a simple state machine:


This state machine models a button state machine that controls the power to some other system.  A state is represented as a circle, in this example: "Power On" and "Power Off".  A transition is represented by an arrow leading from one state to another: "Button click".

Let us do a quick run through of what this represents.  We imagine that this state machine is controlling the lights in your room.  You enter the room, and it is dark.  This means that you are currently in the state "Power Off".  You want to turn on the lights, and in order to that you click the button that turns the light on.  This is taking the transition from "Power Off" --> "Power On" through the "event" Button Click.

How does this Apply to Computer Science?


I first learned the concept of state machines in my computer architecture course, where it we tracked the behavior of a system.  It would then continue to come up in my upper division course, including: Operating Systems, Embedded Systems, Compilers and many more classes.  It was clear that state machines have their uses throughout computer science.

Using Enums in Java to simulate a State Machine


There are many ways you can create a state machine in Java, such as maintaining a string called "state" that enters a switch statement or if/else ladder.  Also alternatively you could use a integer to keep track of your state by assigning a number to each state.

The best way to create a state machine in Java is to use enums to represent each of your states.  Using enums allows you to store certain values into your enums, which will be useful in some situations.

public static enum state {
           state1, state2, state3, state4, state5, state6;
}

To "transition" between states, you can use if/else statements or switch statements.  I choose to show a switch statement in this example:

switch(currentState) {
case state1: currentState = state2; break;
case state2: currentState = state3; break;
case state3: currentState = state4; break;
case state4: currentState = state5; break;
case state5: currentState = state6; break;
default: //Invalid state
}

This switch statement models a FSM that enters the next state every cycle of the system.  How do I make this applicable to a real, simple program.

You can create state machine that is controlled by user input, by using Scanner with System.in.

Scanner sc = new Scanner(System.in)

The next thing you want to do is to create a loop that accepts a user input, or other parameter, and checks it against it's current state to see if a transition needs to be made.

while(true) {
         //loop
}

or you can designate a leave state that exits the loop

while(currentState != state6) {
        //loop
}

For a more complete version of the code above: State Machine Example

Concluding Thoughts


Many of you will probably have written some code in one form or another that is similar to this computational model.  Based on a current "State" and given new information, change the "State" of your program to this.  This is a more formal way of describing this action.  I have found that knowing the basics of this is important to programming for embedded systems.  Basically any model that requires a user input will need to be based on a state machine, because of the you need a "state" to describe how an object at a certain point of time during operation.  The state machine I have described in this example is a Moore state machine, because it relies solely on the entry action, or the event that caused the transition.  There is another model of the state machine that relies on both the current state as well as the event action.  You can find more details about the difference between the two at the Wiki page: Finite State Machine

Comments

Popular posts from this blog

Uncle Bob's Clean Architecture

C4 Model: Describing Software Architecture

Running RabbitMQ with Docker