Java 中的有限状态机

Sheeraz Gul 2023年10月12日
Java 中的有限状态机

有限状态机用于识别模式。本教程演示如何在 Java 中实现有限状态机。

Java 中的有限状态机

有限状态机,也称为有限自动机,将符号串作为输入,然后相应地改变其状态。当在输入中找到所需的符号时发生转换,并且在转换期间,自动机可以保持相同的状态或更改为下一个状态。

按照以下步骤在 Java 中实现有限状态机。

  • 首先,创建一个包装类来对有限状态机进行建模。

    该包装类将包含具体状态对象的数组、当前状态的索引和状态转换表。

  • 接下来,所有客户端请求都将委托给当前状态对象。
  • 然后,创建一个状态基类以使具体状态可互换。

    该状态基类将指定所有状态的默认行为。

  • 最后,状态派生类将只覆盖所需的消息。

让我们根据上面的步骤在 Java 中实现一个有限状态机。

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();
      }
    }
  }
}

上面的代码演示了一个有限状态机,它根据输入消息改变状态。见输出:

State1 + OFF = State2
State2 + OFF = State3
An error occurred
An error occurred
State3 + ON  = State2
State2 + ON  = State1
An error occurred
State1 + Ack = State1
作者: 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