forked from kmarkus/rFSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfsmpp.lua
143 lines (126 loc) · 4.73 KB
/
rfsmpp.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
--
-- This file is part of rFSM.
--
-- (C) 2010,2011 Markus Klotzbuecher, [email protected],
-- Department of Mechanical Engineering, Katholieke Universiteit
-- Leuven, Belgium.
--
-- You may redistribute this software and/or modify it under either
-- the terms of the GNU Lesser General Public License version 2.1
-- (LGPLv2.1 <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>)
-- or (at your discretion) of the Modified BSD License: Redistribution
-- and use in source and binary forms, with or without modification,
-- are permitted provided that the following conditions are met:
-- 1. Redistributions of source code must retain the above copyright
-- notice, this list of conditions and the following disclaimer.
-- 2. Redistributions in binary form must reproduce the above
-- copyright notice, this list of conditions and the following
-- disclaimer in the documentation and/or other materials provided
-- with the distribution.
-- 3. The name of the author may not be used to endorse or promote
-- products derived from this software without specific prior
-- written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
-- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
-- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--
--
-- Various pretty printing functions to make life easier
--
require("ansicolors")
require("utils")
require("rfsm")
local unpack, print, type, pairs, assert = unpack, print, type, pairs, assert
local table = table
local utils = utils
local string = string
local ac = ansicolors
local rfsm = rfsm
-- some shortcuts
local is_meta = rfsm.is_meta
local is_state = rfsm.is_state
local is_leaf = rfsm.is_leaf
local is_composite = rfsm.is_composite
local sta_mode = rfsm.sta_mode
local fsmobj_tochar = rfsm.fsmobj_tochar
module("rfsmpp")
local pad = 20
-- pretty print fsm
function fsm2str(fsm, ind)
local ind = ind or 1
local indstr = ' '
local res = {}
function __2colstr(s)
assert(s, "s not a state")
if s._mode == 'active' then
if is_leaf(s) then return ac.green .. ac.bright .. s._id .. ac.reset
else return ac.green .. s._id .. ac.reset end
elseif s._mode == 'done' then return ac.yellow .. s._id .. ac.reset
else return ac.red .. s._id .. ac.reset end
end
function __fsm_tostring(tab, res, ind)
for name,state in pairs(tab) do
if not is_meta(name) and is_state(state) then
res[#res+1] = string.rep(indstr, ind) .. __2colstr(state) .. '[' .. fsmobj_tochar(state) .. ']'
if is_leaf(state) then res[#res+1] = '\n' end
if is_composite(state) then
res[#res+1] = '\n'
__fsm_tostring(state, res, ind+1)
end
end
end
end
res[#res+1] = __2colstr(fsm) .. '\n'
__fsm_tostring(fsm, res, ind)
return table.concat(res, '')
end
--- Debug message colors.
local ctab = {
STATE_ENTER = ac.green,
STATE_EXIT = ac.red,
EFFECT = ac.yellow,
DOO = ac.blue,
EXEC_PATH = ac.cyan,
ERROR = ac.red .. ac.bright,
HIBERNATING = ac.magenta,
RAISED = ac.white .. ac.bright,
TIMEEVENT = ac.yellow .. ac.bright
}
--- Colorized fsm.dbg hook replacement.
function dbgcolor(name, ...)
local str = ""
local args = { ... }
if name then str = ac.cyan .. ac.bright .. name .. ":" .. ac.reset .. '\t' end
-- convert nested tables to strings
ptab = utils.map(utils.tab2str, args)
col = ctab[ptab[1]]
if col ~= nil then
str = str.. col .. utils.rpad(ptab[1], pad) .. ac.reset .. table.concat(ptab, ' ', 2)
else
str = str .. utils.rpad(ptab[1], pad) .. table.concat(ptab, ' ', 2)
end
print(str)
end
--- Generate a configurable dbgcolor function.
-- @param name string name to prepend to printed message.
-- @param ftab table of the dbg ids to print.
-- @param defshow if false fields not mentioned in ftab are not shown. If true they are.
function gen_dbgcolor(name, ftab, defshow)
name = name or "<unnamed SM>"
ftab = ftab or {}
if defshow == nil then defshow = true end
return function (tag, ...)
if ftab[tag] == true then dbgcolor(name, tag, ...)
elseif ftab[tag] == false then return
else if defshow then dbgcolor(name, tag, ...) end end
end
end