Apply format
parent
2bf7b519b2
commit
c25e91f660
|
|
@ -1,6 +1,7 @@
|
||||||
module statemachine
|
module statemachine
|
||||||
|
|
||||||
pub type EventHandlerFn = fn (receiver voidptr)
|
pub type EventHandlerFn = fn (receiver voidptr)
|
||||||
|
|
||||||
pub type ConditionFn = fn (receiver voidptr) bool
|
pub type ConditionFn = fn (receiver voidptr) bool
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
|
|
@ -12,14 +13,14 @@ mut:
|
||||||
|
|
||||||
struct Transition {
|
struct Transition {
|
||||||
mut:
|
mut:
|
||||||
to string
|
to string
|
||||||
condition ConditionFn
|
condition ConditionFn
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StateMachine {
|
struct StateMachine {
|
||||||
mut:
|
mut:
|
||||||
states map[string]State
|
states map[string]State
|
||||||
transitions map[string]Transition
|
transitions map[string]Transition
|
||||||
current_state string
|
current_state string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,7 +29,7 @@ pub fn new() &StateMachine {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run EventHandlerFn, exit EventHandlerFn) {
|
pub fn (mut s StateMachine) add_state(name string, entry EventHandlerFn, run EventHandlerFn, exit EventHandlerFn) {
|
||||||
s.states [name] = State{
|
s.states[name] = State{
|
||||||
entry_handler: entry
|
entry_handler: entry
|
||||||
run_handler: run
|
run_handler: run
|
||||||
exit_handler: exit
|
exit_handler: exit
|
||||||
|
|
@ -45,10 +46,10 @@ pub fn (mut s StateMachine) add_transition(from string, to string, condition Con
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) run(receiver voidptr){
|
pub fn (mut s StateMachine) run(receiver voidptr) {
|
||||||
for from_state, transition in s.transitions{
|
for from_state, transition in s.transitions {
|
||||||
if from_state == s.current_state {
|
if from_state == s.current_state {
|
||||||
if transition.condition(receiver){
|
if transition.condition(receiver) {
|
||||||
s.change_state(receiver, s.transitions[from_state].to)
|
s.change_state(receiver, s.transitions[from_state].to)
|
||||||
}
|
}
|
||||||
s.states[s.current_state].run_handler(receiver)
|
s.states[s.current_state].run_handler(receiver)
|
||||||
|
|
@ -56,10 +57,10 @@ pub fn (mut s StateMachine) run(receiver voidptr){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s StateMachine) change_state(receiver voidptr, newstate string){
|
pub fn (mut s StateMachine) change_state(receiver voidptr, newstate string) {
|
||||||
mut current_state := s.current_state
|
mut current_state := s.current_state
|
||||||
s.states[current_state].exit_handler(receiver)
|
s.states[current_state].exit_handler(receiver)
|
||||||
current_state = newstate
|
current_state = newstate
|
||||||
s.states[current_state].entry_handler(receiver)
|
s.states[current_state].entry_handler(receiver)
|
||||||
s.current_state = current_state
|
s.current_state = current_state
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ mut:
|
||||||
fn test_statemachine_works_when_single_transition() {
|
fn test_statemachine_works_when_single_transition() {
|
||||||
mut receiver := &MyReceiver{}
|
mut receiver := &MyReceiver{}
|
||||||
mut s := statemachine.new()
|
mut s := statemachine.new()
|
||||||
s.add_state("A",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('A', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_state("B",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('B', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_transition("A", "B", condition_transition)
|
s.add_transition('A', 'B', condition_transition)
|
||||||
s.run(receiver)
|
s.run(receiver)
|
||||||
|
|
||||||
assert receiver.data.len == 3
|
assert receiver.data.len == 3
|
||||||
|
|
@ -19,36 +19,38 @@ fn test_statemachine_works_when_single_transition() {
|
||||||
fn test_statemachine_works_when_typical() {
|
fn test_statemachine_works_when_typical() {
|
||||||
mut receiver := &MyReceiver{}
|
mut receiver := &MyReceiver{}
|
||||||
mut s := statemachine.new()
|
mut s := statemachine.new()
|
||||||
s.add_state("A",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('A', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_state("B",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('B', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_transition("A", "B", condition_transition)
|
s.add_transition('A', 'B', condition_transition)
|
||||||
s.run(receiver)
|
s.run(receiver)
|
||||||
|
|
||||||
assert receiver.data[0] == "on_state_exit"
|
assert receiver.data[0] == 'on_state_exit'
|
||||||
assert receiver.data[1] == "on_state_entry"
|
assert receiver.data[1] == 'on_state_entry'
|
||||||
assert receiver.data[2] == "on_state_run"
|
assert receiver.data[2] == 'on_state_run'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_statemachine_works_when_final_state() {
|
fn test_statemachine_works_when_final_state() {
|
||||||
mut receiver := &MyReceiver{}
|
mut receiver := &MyReceiver{}
|
||||||
mut s := statemachine.new()
|
mut s := statemachine.new()
|
||||||
s.add_state("A",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('A', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_state("B",on_state_entry, on_state_run, on_state_exit)
|
s.add_state('B', on_state_entry, on_state_run, on_state_exit)
|
||||||
s.add_transition("A", "B", condition_transition)
|
s.add_transition('A', 'B', condition_transition)
|
||||||
s.run(receiver)
|
s.run(receiver)
|
||||||
s.run(receiver)
|
s.run(receiver)
|
||||||
|
|
||||||
assert receiver.data.len == 3
|
assert receiver.data.len == 3
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_state_entry(mut receiver &MyReceiver) {
|
fn on_state_entry(mut receiver MyReceiver) {
|
||||||
receiver.data << "on_state_entry"
|
receiver.data << 'on_state_entry'
|
||||||
}
|
}
|
||||||
fn on_state_run(mut receiver &MyReceiver) {
|
|
||||||
receiver.data << "on_state_run"
|
fn on_state_run(mut receiver MyReceiver) {
|
||||||
|
receiver.data << 'on_state_run'
|
||||||
}
|
}
|
||||||
fn on_state_exit(mut receiver &MyReceiver) {
|
|
||||||
receiver.data << "on_state_exit"
|
fn on_state_exit(mut receiver MyReceiver) {
|
||||||
|
receiver.data << 'on_state_exit'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn condition_transition(receiver &MyReceiver) bool {
|
fn condition_transition(receiver &MyReceiver) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue