forked from kmarkus/rFSM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rfsm_preview.lua
63 lines (54 loc) · 1.79 KB
/
rfsm_preview.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
--- rFSM discrete preview coordination
--
-- (C) 2010-2013 Markus Klotzbuecher <[email protected]>
-- (C) 2014-2020 Markus Klotzbuecher <[email protected]>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
-- This extension extends transitions with a 'likely' predicate (side
-- effect free function) states with a 'prepare' function.. When the
-- source state is active and the likely function returns true, then
-- the prepare function of the target state is invoked.
--
local rfsm = require("rfsm")
local print, ipairs, pairs, error, type = print, ipairs, pairs, error, type
local actchild_get = rfsm.actchild_get
module 'rfsm_preview'
local function do_preview(fsm)
local function preview_otrs(state)
local likely=nil
local prepare=nil
for i,tr in ipairs(state._otrs) do
likely = tr.likely
if likely and likely(fsm) then
prepare = tr.tgt.prepare
if prepare then prepare(fsm) end
end
end
end
local next = actchild_get(fsm)
while next do
preview_otrs(next)
next = actchild_get(next)
end
end
--- Setup preview coordination.
-- @param fsm initialized root fsm.
local function setup_preview(fsm)
fsm.info("rfsm_preview: discrete preview extension loaded")
rfsm.mapfsm(function (tr)
if tr.likely and type(tr.likely) ~= 'function' then
error("ERROR: invalid 'likely' attribute on " ..
tostring(tr)..". Should be a function")
end
end, fsm, rfsm.is_trans)
rfsm.mapfsm(function (s)
if s.prepare and type(s.prepare) ~= 'function' then
error("ERROR: invalid 'prepare' attribute of " ..
fsm._fqn .. ". Should be a function")
end
end, fsm, rfsm.is_state)
rfsm.post_step_hook_add(fsm, do_preview)
end
-- install setup_preview as preproc hook
rfsm.preproc[#rfsm.preproc+1] = setup_preview