transitions
简介
提供有限状态机功能
组件接口
transitions.gs
类
State
状态
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 | 
|---|---|---|---|---|
| state | string | nil | 可选 | 状态名称 | 
| on_enter | function | nil | 可选 | 进入状态时执行的处理 | 
| on_exit | function | nil | 可选 | 离开状态时执行的处理 | 
| transitions | map | nil | 可选 | 转换操作表 key: 转换操作, value: 转换处理 - Transition实例 | 
成员方法
| 函数原型 | 函数作用 | 
|---|---|
| void add_transition(string event, Transition transition) | 添加一个状态转换操作 | 
| void set_transitions(map transitions) | 设置状态转换操作表 | 
| Transition get_transition(string event) | 获取状态转换操作对应的转换处理 | 
| void exit() | 退出状态(执行离开状态时处理) | 
| void enter() | 进入状态(执行进入状态时处理) | 
Transition
状态转换处理
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 | 
|---|---|---|---|---|
| condition | function | nil | 可选 | 转换条件 | 
| on_action | function | nil | 可选 | 转换时执行的操作 | 
| next_state | string | nil | 可选 | 转换后状态 | 
成员方法
| 函数原型 | 函数作用 | 
|---|---|
| bool guard(string event) | 转换操作条件检查 | 
| void action(string event, State current_state, State next_state) | 执行转换操作 | 
Machine
状态机
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 | 
|---|---|---|---|---|
| states | map | nil | 可选 | 状态表 key: 状态名称, value: 状态 - State实例 | 
| initial_state | string | nil | 可选 | 初始状态 | 
| current_state | string | nil | 可选 | 当前状态 | 
| locking | bool | false | 可选 | 是否正在转换操作中 - true - 正在转换操作中, false - 未在转换操作中 | 
成员方法
| 函数原型 | 函数作用 | 
|---|---|
| bool lock() | 标记为正在转换操作中 | 
| void unlock() | 标记为未在转换操作中 | 
| void set_initial_state(string state_name) | 设置初始状态 | 
| State new_state(string state_name, function on_enter = nil, function on_exit = nil, map transitions = nil) | 创建并增加一个状态 | 
| void add_state(State state) | 增加一个状态 | 
| void remove_state(State state) | 移除一个状态 | 
| State get_state(string state_name) | 获取一个状态 | 
| bool is_in_state(string state_name) | 当前是否处于指定状态 | 
| void set_current_state(string state_name) | 设置当前状态 | 
| bool is_running() | 判断当前是否处于运行状态(即当前状态不为nil) | 
| void start() | 状态机启动(即设置当前状态为初始状态) | 
| void stop() | 状态机停止(即设置当前状态为nil) | 
| void process_event(string event) | 执行一个状态转换操作 | 
样例
// test.gs
import pkg.gtest;
import pkg.transitions;
test();
test1();
test2();
public void test()
{
    // a/b/c三个状态
    array states = [
        "a",
        "b",
        "c"
    ];
    // 状态各自的转换操作表
    //
    // 状态a的转换操作表
    // 转换操作down -> b
    // 转换操作downdown -> c
    //
    // 状态b的转换操作表
    // 转换操作up -> a
    // 转换操作down -> c
    //
    // 状态c的转换操作表
    // 转换操作up -> b
    // 转换操作upup -> a
    map transition_dict = {
        "a" : {
            "down" : "b",
            "downdown" : "c"
        },
        "b" : {
            "up" : "a",
            "down" : "c"
        },
        "c" : {
            "up" : "b",
            "upup" : "a"
        }
    };
    // 创建有限状态机,初始状态是a
    Machine machine = transitions.new_machine(states, transition_dict, "a");
    machine.start();
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作up(无效操作,a状态没有'up'转换操作)
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作downdown
    machine.process_event("downdown");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作upup
    machine.process_event("upup");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 停止
    machine.stop();
}
void on_enter(State state)
{
    printf("entering state: %s\n", state.state);
}
void on_exit(State state)
{
    printf("leaving state: %s\n", state.state);
}
void on_action(string event, State current_state, State next_state)
{
    printf("action %s, %s====>%s\n", event, current_state.state, next_state.state);
}
public void test1()
{
    // a/b/c三个状态
    array states = [
        [ "a", (: on_enter :), (: on_exit :) ],
        [ "b", (: on_enter :), (: on_exit :) ],
        [ "c", (: on_enter :), (: on_exit :) ]
    ];
    // 状态各自的转换操作表
    //
    // 状态a的转换操作表
    // 转换操作down -> b
    // 转换操作downdown -> c
    //
    // 状态b的转换操作表
    // 转换操作up -> a
    // 转换操作down -> c
    //
    // 状态c的转换操作表
    // 转换  操作up -> b
    // 转换操作upup -> a
    map transition_dict = {
        "a" : {
            "down" : [ "b", (: on_action :) ],
            "downdown" : [ "c" , (: on_action :), ]
        },
        "b" : {
            "up" : [ "a", (: on_action :) ],
            "down" : [ "c", (: on_action :) ],
        },
        "c" : {
            "up" : [ "b", (: on_action :) ],
            "upup" : [ "a", (: on_action :) ],
        }
    };
    // 创建有限状态机,初始状态是a
    Machine machine = transitions.new_machine(states, transition_dict, "a");
    machine.start();
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作up(无效操作,a状态没有'up'转换操作)
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作downdown
    machine.process_event("downdown");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作upup
    machine.process_event("upup");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 停止
    machine.stop();
}
void on_enter2(State state)
{
    printf("2=>entering state: %s\n", state.state);
}
void on_exit2(State state)
{
    printf("2=>leaving state: %s\n", state.state);
}
void on_action2(string event, State current_state, State next_state)
{
    printf("2=>action %s, %s====>%s\n", event, current_state.state, next_state.state);
}
public void test2()
{
    // a/b/c三个状态
    array states = [
        transitions.new_state("a", (: on_enter2 :), (: on_exit2 :)),
        transitions.new_state("b", (: on_enter2 :), (: on_exit2 :)),
        transitions.new_state("c", (: on_enter2 :), (: on_exit2 :)),
    ];
    // 状态各自的转换操作表
    //
    // 状态a的转换操作表
    // 转换操作down -> b
    // 转换操作downdown -> c
    //
    // 状态b的转换操作表
    // 转换操作up -> a
    // 转换操作down -> c
    //
    // 状态c的转换操作表
    // 转换操作up -> b
    // 转换操作upup -> a
    map transition_dict = {
        "a" : {
            "down" : transitions.new_transition("b", (: on_action2 :)),
            "downdown" : transitions.new_transition( "c" , (: on_action2 :))
        },
        "b" : {
            "up" : transitions.new_transition("a", (: on_action2 :)),
            "down" : transitions.new_transition("c", (: on_action2 :))
        },
        "c" : {
            "up" : transitions.new_transition("b", (: on_action2 :)),
            "upup" : transitions.new_transition("a", (: on_action2 :))
        }
    };
    // 创建有限状态机,初始状态是a
    Machine machine = transitions.new_machine(states, transition_dict, "a");
    machine.start();
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作up(无效操作,a状态没有'up'转换操作)
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作down
    machine.process_event("down");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("b"), true);
    // 处理转换操作up
    machine.process_event("up");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 处理转换操作downdown
    machine.process_event("downdown");
    gtest.test_equal(machine.is_in_state("c"), true);
    // 处理转换操作upup
    machine.process_event("upup");
    gtest.test_equal(machine.is_in_state("a"), true);
    // 停止
    machine.stop();
}