Java 中的有限狀態機
Sheeraz Gul
2022年5月31日
有限狀態機用於識別模式。本教程演示如何在 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
Author: Sheeraz Gul
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