Skip to content

Commit

Permalink
tests, constructor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tinybike committed Aug 20, 2014
1 parent 625312c commit 22404bd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/FiniteStateMachine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export StateMachine, state_machine

type StateMachine
initial::Dict{String,String}
states::Array{String,1}
events::Array{Dict{String,String},1}
callbacks::Array{String,1}
map::Dict
callbacks::Dict{String,Function}
end

include("constants.jl")
Expand Down
6 changes: 3 additions & 3 deletions src/state_machine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function state_machine(cfg::Dict)
add(event, _map)
end
for (name, target) in _map
fsm[name] = build_event(name, target)
fsm[name] = build_event(fsm, name, target)
end
for (name, cb) in callbacks
fsm[name] = cb
Expand All @@ -67,7 +67,7 @@ function state_machine(cfg::Dict)
end
end
end
fsm
StateMachine(initial, events, _map, callbacks)
end

function add(event::Dict, _map::Dict)
Expand All @@ -94,7 +94,7 @@ function add(event::Dict, _map::Dict)
end
end

function build_event(fsm::StateMachine, name::String, _map::Dict)
function build_event(fsm::Dict, name::String, _map::Dict)
function ()
from = fsm["current"]
if haskey(_map, "from")
Expand Down
28 changes: 14 additions & 14 deletions src/transitions.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function callback(fsm::StateMachine, func::Function,
function callback(fsm::Dict, func::Function,
state::String, from::String, to::String)
if isdefined(func)
try
Expand All @@ -10,55 +10,55 @@ function callback(fsm::StateMachine, func::Function,
end

# Before event
function before_event(fsm::StateMachine, state::String, from::String, to::String)
function before_event(fsm::Dict, state::String, from::String, to::String)
specific = before_this_event(fsm, state, from, to)
general = before_any_event(fsm, state, from, to)
(~specific || ~general) ? false : ASYNC
end

before_this_event(fsm::StateMachine, state::String, from::String, to::String) =
before_this_event(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onbefore"] + state, state, from, to)

before_any_event(fsm::StateMachine, state::String, from::String, to::String) =
before_any_event(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onbeforeevent"], state, from, to)

# After event
function after_event(fsm::StateMachine, state::String, from::String, to::String)
function after_event(fsm::Dict, state::String, from::String, to::String)
after_this_event(fsm, state, from, to)
after_any_event(fsm, state, from, to)
end

after_this_event(fsm::StateMachine, state::String, from::String, to::String) =
after_this_event(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onafter"] + state, state, from, to)

after_any_event(fsm::StateMachine, state::String, from::String, to::String) =
after_any_event(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onafterevent"] || fsm["onevent"], state, from, to)

# Change of state
change_state(fsm::StateMachine, state::String, from::String, to::String) =
change_state(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onchangestate"], state, from, to)

# Entering a state
function enter_state(fsm::StateMachine, state::String, from::String, to::String)
function enter_state(fsm::Dict, state::String, from::String, to::String)
enter_this_state(fsm, state, from, to)
enter_any_state(fsm, state, from, to)
end

enter_this_state(fsm::StateMachine, state::String, from::String, to::String) =
enter_this_state(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onenter"] + to, state, from, to)

enter_any_state(fsm::StateMachine, state::String, from::String, to::String) =
enter_any_state(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onenterstate"] || fsm["onstate"], state, from, to)

# Leaving a state
function leave_state(fsm::StateMachine, state::String, from::String, to::String)
function leave_state(fsm::Dict, state::String, from::String, to::String)
specific = leave_this_state(fsm, state, from, to)
general = leave_any_state(fsm, state, from, to)
(~specific || ~general) ? false : ASYNC
end

leave_this_state(fsm::StateMachine, state::String, from::String, to::String) =
leave_this_state(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onleave"] + from, state, from, to)

leave_any_state(fsm::StateMachine, state::String, from::String, to::String) =
leave_any_state(fsm::Dict, state::String, from::String, to::String) =
callback(fsm, fsm["onleavestate"], state, from, to)
27 changes: 15 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
using FiniteStateMachine
using Base.Test

fsm = StateMachine(
"initialstate",
["state1", "state2", "state3"],
["event1", "event2"],
["callback1", "callback2"]
)

@test fsm.initial == "initialstate"
@test fsm.states == ["state1", "state2", "state3"]
@test fsm.events == ["event1", "event2"]
@test fsm.callbacks == ["callback1", "callback2"]

cfg = {
"initial" => "first",
"final" => "fourth",
Expand All @@ -33,4 +21,19 @@ cfg = {
"to" => "fourth",
],
],
"callbacks" => (String => Function)[
"onfirst" => () -> println("boom!"),
],
}

fsm = state_machine(cfg)

@test fsm.initial["event"] == "startup"
@test fsm.initial["state"] == "first"
@test fsm.map["startup"] == {"none"=>"first"}
@test fsm.map["skip"] == {"second"=>"third"}
@test fsm.map["jump"] == {"third"=>"fourth"}
@test fsm.map["hop"] == {"first"=>"second"}
@test fsm.events[1] == ["name"=>"hop","to"=>"second","from"=>"first"]
@test fsm.events[2] == ["name"=>"skip","to"=>"third","from"=>"second"]
@test fsm.events[3] == ["name"=>"jump","to"=>"fourth","from"=>"third"]

0 comments on commit 22404bd

Please sign in to comment.