Finite State Machine in Java

Sheeraz Gul May 27, 2022
Finite State Machine in Java

The Finite State Machine is used to recognize patterns. This tutorial demonstrates how to implement a finite state machine in Java.

Finite State Machine in Java

The Finite State Machine, also known as a finite automata machine, takes the string of symbols as input and then changes its state accordingly. The transition occurs when the desired symbol is found in the input, and during the transition, the automata can stay in the same state or change to the next state.

Follow the steps below to implement a Finite State Machine in Java.

  • First, create a wrapper class to model the finite state machine.

    That wrapper class will contain the array of concrete state objects, the index to its current state, and the state transition table.

  • Next, all client requests will be delegated to the current state object.
  • Then, create a state base class to make the concrete states interchangeable.

    That state base class will specify the default behavior of all states.

  • Finally, the state-derived class will only override the required messages.

Let’s implement a finite state machine in Java based on the steps above.

package delftstack;

//Step 1. create a wrapper class which models the finite state machine.
class Java_FSM {
    // Step 2. wrapper class states
    private FSM_State[] FSM_States = {new State1(), new State2(), new State3()};
    // Step 4. wrapper class transitions
    private int[][] FSM_Transition = {{0,1,2}, {2,2,0}, {1,0,2}};
    // Step 3. wrapper class current state
    private int Current_State = 0;

    private void FSM_Next(int Message) {
    	Current_State = FSM_Transition[Current_State][Message];
    }

    // Step 5. all client requests will be delegated to the current state object.
    public void FSM_ON() {
    	FSM_States[Current_State].FSM_ON();
    	FSM_Next(0);
    }

    public void FSM_OFF() {
    	FSM_States[Current_State].FSM_OFF();
    	FSM_Next(1);
    }

    public void FSM_ACK() {
    	FSM_States[Current_State].FSM_ACK();
    	FSM_Next(2);
    }
}

//Step 6. Create a state base class to make the concrete states interchangeable.
//Step 7. state base class will specify the default behavior of all states
abstract class FSM_State {
    public void FSM_ON() {
        System.out.println("An error occurred");
    }

    public void FSM_OFF() {
        System.out.println("An error occurred");
    }

    public void FSM_ACK() {
        System.out.println("An error occurred");
    }
}

class State1 extends FSM_State {
    public void FSM_ON() {
        System.out.println("State1 + ON  = State3");
    }

    public void FSM_OFF() {
        System.out.println("State1 + OFF = State2");
    }

    public void FSM_ACK() {
        System.out.println("State1 + Ack = State1");
    }
}

class State2 extends FSM_State {
    public void FSM_ON() {
        System.out.println("State2 + ON  = State1");
    }

    public void FSM_OFF() {
        System.out.println("State2 + OFF = State3");
    }
}

class State3 extends FSM_State {
    // Step 8. state derived class will only override the messages it require to
    public void FSM_ON() {
        System.out.println("State3 + ON  = State2");
    }
}

public class Finite_State_Machine {
    public static void main(String[] args) {
    	Java_FSM JavaFSM = new Java_FSM();
        int[] Messages = {1, 1, 2, 2, 0, 0, 1, 2};
        for (int Message : Messages) {
            if (Message == 0) {
            	JavaFSM.FSM_ON();
            }
            else if (Message == 1) {
            	JavaFSM.FSM_OFF();
            }
            else if (Message == 2) {
            	JavaFSM.FSM_ACK();
            }
        }
    }
}

The code above demonstrates a finite state machine that changes states based on the input messages. See output:

State1 + OFF = State2
State2 + OFF = State3
An error occurred
An error occurred
State3 + ON  = State2
State2 + ON  = State1
An error occurred
State1 + Ack = State1
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook