跳到主要内容

transitions

简介

提供有限状态机功能

组件接口

transitions.gs

函数原型函数作用
Machine new_machine(array state_list = nil, map transition_dict = nil, string initial_state = nil)创建一个有限状态机
State new_state(string state_name, function on_enter = nil, function on_exit = nil, map transitions = nil)创建一个状态实例
Transition new_transition(string next_state, function on_action = nil, function condition = nil)创建一个状态迁移处理实例

样例


import pkg.transitions;

test();
test1();
test2();

public void test()
{
array states = [
"a",
"b",
"c"
];

map transition_dict = {
"a" : {
"down" : "b",
"downdown" : "c"
},
"b" : {
"up" : "a",
"down" : "c"
},
"c" : {
"up" : "b",
"upup" : "a"
}
};

Machine machine = transitions.new_machine(states, transition_dict, "a");
machine.start();

machine.process_event("down");
machine.process_event("down");
machine.process_event("up");
machine.process_event("up");

machine.close();
}

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()
{
array states = [
[ "a", (: on_enter :), (: on_exit :) ],
[ "b", (: on_enter :), (: on_exit :) ],
[ "c", (: on_enter :), (: on_exit :) ]
];

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 :) ],
}
};

Machine machine = transitions.new_machine(states, transition_dict, "a");
machine.start();

machine.process_event("down");
machine.process_event("down");
machine.process_event("up");
machine.process_event("up");

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()
{
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 :)),
];

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

Machine machine = transitions.new_machine(states, transition_dict, "a");
machine.start();

machine.process_event("down");
machine.process_event("down");
machine.process_event("up");
machine.process_event("up");

machine.stop();
}