-
Notifications
You must be signed in to change notification settings - Fork 26
/
rfsm2tree.lua
148 lines (115 loc) · 3.68 KB
/
rfsm2tree.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
144
145
146
147
148
-- convert an rfsm model to a graphviz tree
--
-- (C) 2010-2013 Markus Klotzbuecher <markus.klotzbuecher@mech.kuleuven.be>
-- (C) 2014-2020 Markus Klotzbuecher <mk@mkio.de>
--
-- SPDX-License-Identifier: BSD-3-Clause
local gv = require("gv")
local rfsm = require("rfsm")
local utils = require("utils")
local pairs, ipairs, print, table, string, type, assert, gv, io, rfsm
= pairs, ipairs, print, table, string, type, assert, gv, io, rfsm
local M = {}
local param = {}
param.trfontsize = 7.0
param.show_fqn = false
param.and_color="green"
param.and_style="dashed"
param.hedge_color="blue"
param.hedge_style="dotted"
param.layout="dot"
param.err=print
param.dbg = function () return true end
-- overall state properties
local function set_sprops(nh)
gv.setv(nh, "style", "rounded")
gv.setv(nh, "shape", "box")
end
local function set_ini_sprops(nh)
gv.setv(nh, "shape", "point")
gv.setv(nh, "height", "0.15")
end
local function set_fini_sprops(nh)
gv.setv(nh, "shape", "doublecircle")
gv.setv(nh, "label", "")
gv.setv(nh, "height", "0.1")
end
local function set_hier_trans_props(eh)
gv.setv(eh, "arrowhead", "none")
gv.setv(eh, "style", param.hedge_style)
gv.setv(eh, "color", param.hedge_color)
end
local function set_trans_props(eh)
gv.setv(eh, "fontsize", param.trfontsize)
end
-- create new graph and add root node
local function new_graph(fsm)
local gh = gv.digraph("hierarchical chart: " .. fsm._id)
gv.setv(gh, "rankdir", "TD")
local nh = gv.node(gh, fsm._fqn)
set_sprops(nh)
return gh
end
-- add regular type of state
local function add_state(gh, parent, state)
local nh = gv.node(gh, state._fqn)
set_sprops(nh)
local eh = gv.edge(gh, parent._fqn, state._fqn)
set_hier_trans_props(eh)
if not param.show_fqn then
gv.setv(nh, "label", state._id)
end
end
-- add initial states
local function add_ini_state(gh, tr, parent)
local nh, eh
if tr.src._id == 'initial' then
nh = gv.node(gh, parent._fqn .. '.initial')
set_ini_sprops(nh)
eh = gv.edge(gh, parent._fqn, parent._fqn .. '.initial')
set_hier_trans_props(eh)
end
end
-- add final states
local function add_fini_state(gh, tr, parent)
local nh, eh
if tr.tgt._id == 'final' then
nh = gv.node(gh, parent._fqn .. '.final')
set_fini_sprops(nh)
eh = gv.edge(gh, parent._fqn, parent._fqn .. '.final')
set_hier_trans_props(eh)
end
end
-- add a transition from src to tgt
local function add_trans(gh, tr, parent)
local src, tgt, eh
if tr.src == 'initial' then src = parent._fqn .. '.initial'
else src = tr.src._fqn end
if tr.tgt == 'final' then tgt = parent._fqn .. '.final'
else tgt = tr.tgt._fqn end
eh = gv.edge(gh, src, tgt)
gv.setv(eh, "constraint", "false")
if tr.events then gv.setv(eh, "label", table.concat(tr.events, ', ')) end
set_trans_props(eh)
end
local function fsm2gh(fsm)
local gh = new_graph(fsm)
rfsm.mapfsm(function (tr, p) add_ini_state(gh, tr, p) end, fsm, rfsm.is_trans)
rfsm.mapfsm(function (s) add_state(gh, s._parent, s) end, fsm, rfsm.is_state)
rfsm.mapfsm(function (tr, p) add_fini_state(gh, tr, p) end, fsm, rfsm.is_trans)
rfsm.mapfsm(function (tr, p) add_trans(gh, tr, p) end, fsm, rfsm.is_trans)
return gh
end
-- convert fsm to
function M.rfsm2tree(fsm, format, outfile)
if not fsm._initialized then
param.err("rfsm2tree ERROR: fsm " .. (fsm._id or 'root') .. " uninitialized")
return false
end
local gh = fsm2gh(fsm)
gv.layout(gh, param.layout)
param.dbg("rfsm2tree: running " .. param.layout .. " layouter")
gv.render(gh, format, outfile)
param.dbg("rfsm2tree: rendering to " .. format .. ", written result to " .. outfile)
end
return M