用数组实现有限自动机FSM

FSM的实现方法好像蛮多的, 用数组实现是比较常见的一种。

 

思路是这样的,用一个二维数组来保存在某个状态下遇到某种事件或激励时该执行哪个动作。二维数组中有多少“记录”就表示了状态机中有多少状态, 而“记录”中有多少栏表示了有多少种事件。 当然不是在所有的状态下,都会处理所有事件的。

 

有了这个概念,看代码就明白很多了。下面是个小例子,虽然不能运行,但其搭建起来的结构,却给以后的工作打下了良好的基础。

 

#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <stdio.h>


enum PROCESS_STATES
{
        INIT,
        RUNNING,
        SLEEPING,
        DEAD,
        MAX_FSM_STATES
};

enum PROCESS_EVENT
{
        START,
        REQ_RESOURCE,
        TERM,
        MAX_FSM_EVENTS
};

void transaction_func1(void *param)
{
        return;
}
typedef void (*fsm_action)(void *param);

 

typedef struct fsm_state_table {
    /* List of actions when moving into this state from any other state */
    const fsm_action action;
    const fsm_action pre_action;
    const fsm_action post_action;
} fsm_state_table;

 

// 这张就是重要的FSM数组了,定义了在某状态下收到某事件时,该执行什么动作

static const fsm_state_table
        fsm_state_table_process[MAX_FSM_STATES][MAX_FSM_EVENTS] =
{
        /* Current State = { { [Event] = func, ... } }, ... */
        [INIT] =
        {
                [START] = {transaction_func1, },
                [REQ_RESOURCE] = {transaction_func1, },
                [TERM] = {transaction_func1, },
        },
        [RUNNING] =
        {
                [START] = {transaction_func1, },
                [REQ_RESOURCE] = {transaction_func1, },
                [TERM] = {transaction_func1, },
        },
        [SLEEPING] =
        {
                [START] = {transaction_func1, },
                [REQ_RESOURCE] = {transaction_func1, },
                [TERM] = {transaction_func1, },
        },
        [DEAD] =
        {
                [START] = {transaction_func1, },
                [REQ_RESOURCE] = {transaction_func1, },
                [TERM] = {transaction_func1, },
        },
};

// 这个是执行FSM的地方, 其实就是一次执行FSM数组中的函数

int fsm(enum PROCESS_STATES current_state, enum PROCESS_EVENT event)
{
        if(fsm_state_table_process[current_state][event].pre_action)
                (*fsm_state_table_process[current_state][event].pre_action)(1);
        if(fsm_state_table_process[current_state][event].action)
                (*fsm_state_table_process[current_state][event].action)(1);
        if(fsm_state_table_process[current_state][event].post_action)
                (*fsm_state_table_process[current_state][event].post_action)(1);

        // 必要的时候需要改变FSM的状态

        return 0;
}


int main(int argc, char *argv[])
{
        printf("max states %d, %d/n", INIT, MAX_FSM_STATES);
        return 0;
}

 

好了,有了这个框架,以后有什么再加就好了。

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于Java实现有限状态自动机FSM的示例代码: ```java import java.util.HashMap; import java.util.Map; public class FSM { private final Map<State, Map<Character, State>> transitionTable; private State currentState; public FSM(State initialState) { transitionTable = new HashMap<>(); currentState = initialState; } public void addTransition(State fromState, Character input, State toState) { transitionTable.putIfAbsent(fromState, new HashMap<>()); transitionTable.get(fromState).put(input, toState); } public void setInput(String input) { for (char c : input.toCharArray()) { Map<Character, State> transitions = transitionTable.get(currentState); if (transitions == null) { throw new IllegalStateException("Invalid state: " + currentState); } State nextState = transitions.get(c); if (nextState == null) { throw new IllegalStateException( "Invalid input: " + c + " in state: " + currentState); } currentState = nextState; } } public boolean isEndState() { return currentState.isEndState(); } public State getCurrentState() { return currentState; } } class State { private final boolean isEndState; public State(boolean isEndState) { this.isEndState = isEndState; } public boolean isEndState() { return isEndState; } @Override public String toString() { return "State{" + "isEndState=" + isEndState + '}'; } } ``` 在此示例中,我们创建了一个FSM类和一个State类。FSM类包含了一个状态转移表,可以通过addTransition()方法添加状态转移规则,在setInput()方法中,我们将输入字符转换为状态转移表中的下一个状态。isEndState()方法可以检查当前状态是否为终止状态。 State类只有一个构造函数,用于指定该状态是否为终止状态。 这是一个使用FSM类的示例: ```java public class FSMExample { public static void main(String[] args) { State initialState = new State(false); State state1 = new State(false); State state2 = new State(true); FSM fsm = new FSM(initialState); fsm.addTransition(initialState, 'a', state1); fsm.addTransition(state1, 'b', state2); fsm.setInput("ab"); System.out.println("Is end state: " + fsm.isEndState()); System.out.println("Current state: " + fsm.getCurrentState()); } } ``` 在此示例中,我们创建了三个状态,其中第3个状态为终止状态。我们添加了两个状态转移规则,在输入“ab”后,FSM应该处于第3个状态,并且isEndState()方法返回true。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值