-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_renderer.jl
314 lines (255 loc) · 9.11 KB
/
workflow_renderer.jl
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# =============================================================================
# Petri net viewer using GraphViz
# =============================================================================
"""
savefig(pnet::Workflow_PetriNet)
savefig(pnet::Workflow_PetriNet, format::Symbol)
savefig(pnet::Workflow_PetriNet, path::String)
savefig(pnet::Workflow_PetriNet, format::Symbol, path::String)
By default this method generates a PNG file after compiling the Petri net into an XML workflow and compiling the workflow.
If path is not given then the workflow image is stored in your home directory in the \"tmp/pnet\" folder.
Note: Supports all the file formats by GraphViz. For example, :png, :svg, :jpg, :pdf, :webp
For other formats, check GraphViz documentation:
https://graphviz.org/docs/outputs/
# Examples
```julia-repl
julia> # first generate a workflow in the form of a Petri net
julia> pn = Workflow_PetriNet("hello_julia")
A Petri net with name "hello_julia", having 0 ports, 0 places, and 0 transitions.
julia> p1 = place("input_file1")
Place "input_file1" created.
julia> p2 = place("input_file2")
Place "input_file2" created.
julia> p3 = place("output_file1")
Place "output_file1" created.
julia> p4 = place("output_file2")
Place "output_file2" created.
julia> t = transition("hello_jl")
Transition "hello_jl" created.
julia> connect(pn,[(p1, :in),(p2, :in),(p3, :out), (p4, :out)], t)
A Petri net with name "hello_julia", having 0 ports, 4 places, and 1 transitions.
julia> connect(pn, p1, :in)
A Petri net with name "hello_julia", having 1 ports, 4 places, and 1 transitions.
julia> connect(pn, p2, :in)
A Petri net with name "hello_julia", having 2 ports, 4 places, and 1 transitions.
julia> connect(pn, p3, :out)
A Petri net with name "hello_julia", having 3 ports, 4 places, and 1 transitions.
julia> connect(pn, p4, :out)
A Petri net with name "hello_julia", having 4 ports, 4 places, and 1 transitions.
julia> pn
A Petri net with name "hello_julia", having 4 ports, 4 places, and 1 transitions.
# now generate the workflow image
julia> savefig(pn, :jpg, "/home/pnet")
"An image of the workflow Petri net could be found in /home/pnet/hello_julia.jpg"
# the following takes the Petri net and the path for storage and generates a PNG file which is stored in the provided path.
julia> savefig(pn, "/home/pnet")
"An image of the workflow Petri net could be found in /home/pnet/hello_julia.png"
```
See also [`Workflow_PetriNet`](@ref), [`place`](@ref), [`transition`](@ref), [`arc`](@ref), [`port`](@ref), [`Workflow_PetriNet`](@ref), [`connect`](@ref), [`remove`](@ref), [`compile_workflow`](@ref), [`generate_workflow`](@ref).
"""
function savefig(pnet::Workflow_PetriNet, format::Symbol=:png, path::String="")
dot_str = _generate_dot(pnet)
path_dir = ""
if isempty(path)
path_dir = joinpath(ENV["HOME"], "tmp/pnet")
run(`mkdir -p $(path_dir)`)
else
path_dir = path
run(`mkdir -p $(path_dir)`)
end
store_location = ""
if format == :png
graph_viz = GraphViz.Graph(dot_str)
store_location = joinpath(path_dir, "$(pnet.name).png")
graph_gen = FileIO.save(store_location, graph_viz)
else
# Save DOT content to a file
dot_file = joinpath(path_dir, "$(pnet.name).dot")
open(dot_file, "w") do io
write(io, dot_str)
end
fmt = string(format)
# Convert DOT file to SVG using Graphviz
svg_file = "$(pnet.name).$(fmt)"
store_location = joinpath(path_dir, svg_file)
run(`dot -T$fmt $dot_file -o $store_location`)
run(`rm $dot_file`)
end
return "An image of the workflow Petri net could be found in $(store_location)"
end
function savefig(pnet::Workflow_PetriNet, path::String)
return savefig(pnet, :png, path)
end
"""
show_workflow(pnet::Workflow_PetriNet)
Converts the given Petri net object to an SVG string and displays it as HTML to the screen.
This functionality is meant to be used within environments like IJulia.jl or Pluto.jl, not the REPL.
# Arguments
- `pnet::Workflow_PetriNet`: Petri net object describing the workflow to visualize
"""
function show_workflow(pnet::Workflow_PetriNet)
dot_str = _generate_dot(pnet)
io = IOBuffer()
run(pipeline(`dot -Tsvg`, stdin=IOBuffer(dot_str), stdout=io))
result = String(take!(io))
Docs.HTML(result)
end
function _generate_dot(pnet::Workflow_PetriNet)
# collect ports based on their types in and out for shape
in_ports = Vector{Port}()
out_ports = Vector{Port}()
for p in pnet.ports
if p.type == :in
push!(in_ports, p)
elseif p.type == :out
push!(out_ports, p)
else
push!(in_ports, p)
push!(out_ports, p)
end
end
# create a string list that has information for the port nodes
nd_prt_in = """node [shape=house, style=filled, fillcolor=white]\n"""
instr_port_list = [nd_prt_in]
for p in in_ports
prt_st = """Prt_$(p.name) [label="$(p.name)"]\n"""
push!(instr_port_list, prt_st)
end
nd_prt_out = """node [shape=invhouse, style=filled, fillcolor=white]\n"""
outstr_port_list = [nd_prt_out]
for p in out_ports
prt_st = """Prt_$(p.name) [label="$(p.name)"]\n"""
push!(outstr_port_list, prt_st)
end
# create a string list that has information for the place nodes
nd_pl = """node [shape=ellipse]\n"""
plstr_list = [nd_pl]
P_st = ""
for i in 1:length(pnet.places)
p = pnet.places[i]
if p.type == :control_init
b = "\u2022"
P_st = """Pl_$(p.name) [label = "$(b)\n $(p.name)"]\n"""
elseif p.type == :control
P_st = """Pl_$(p.name) [label = "$(p.name)"]\n"""
else
P_st = """Pl_$(p.name) [label = "$(p.name)"]\n"""
end
push!(plstr_list, P_st)
end
# create a string list that has information for the transition nodes
nd_tr = """node [shape=box3d, style=filled, fillcolor=lightblue, margin=0.3, padding=0.2]\n"""
trstr_list = [nd_tr]
for t in pnet.transitions
T_str = """Tr_$(t.name) [label="$(t.name)"]\n"""
push!(trstr_list, T_str)
end
# collect list of different kinds of arcs
in_arcs = Vector{Arc}()
out_arcs = Vector{Arc}()
inout_arcs = Vector{Arc}()
read_arcs = Vector{Arc}()
out_many_arcs = Vector{Arc}()
for a in pnet.arcs
if a.type == :in
push!(in_arcs, a)
elseif a.type == :out
push!(out_arcs, a)
elseif a.type == :read
push!(read_arcs, a)
elseif a.type == :out_many
push!(out_many_arcs, a)
else
push!(inout_arcs, a)
end
end
title = """label = "$(pnet.name)";"""
pos = """labelloc = "t";"""
gen_str_init = """
digraph Workflow_PetriNet {
$title
$pos
subgraph cluster_init {
label=""
color="lightgrey"
style="filled"
fillcolor=lightgrey
margin=15
"""
in_ports_string = ""
for i in 1:length(instr_port_list)
in_ports_string = in_ports_string * instr_port_list[i]
end
out_ports_string = ""
for i in 1:length(outstr_port_list)
out_ports_string = out_ports_string * outstr_port_list[i]
end
sub_gh_begin = """
$(in_ports_string)
$(out_ports_string)
subgraph cluster_net {
label=""
style="rounded, filled"
color="black"
fillcolor=lightyellow
margin=25
"""
place_string = plstr_list[1]
for i in 2:length(plstr_list)
place_string = place_string * plstr_list[i]
end
transition_string = trstr_list[1]
for i in 2:length(trstr_list)
transition_string = transition_string * trstr_list[i]
end
# // place to transition no arrows
in_pl_trans_edge_str = ""
for a in inout_arcs
arc_str = """[dir="both"]\n"""
in_pl_trans_edge_str = in_pl_trans_edge_str * """
Pl_$(a.place.name) -> Tr_$(a.transition.name)\n"""
end
for a in in_arcs
arc_str = """[dir="forward"]\n"""
in_pl_trans_edge_str = in_pl_trans_edge_str * """
Pl_$(a.place.name) -> Tr_$(a.transition.name)\n"""
end
for a in read_arcs
arc_str = """[style="dashed", dir="forward"]\n"""
in_pl_trans_edge_str = in_pl_trans_edge_str * """
Pl_$(a.place.name) -> Tr_$(a.transition.name)\n"""
end
# // Define edges
prt_pl_edge_str = ""
for p in pnet.ports
if p.type in [:in, :inout]
prt_pl_edge_str = prt_pl_edge_str * """
Prt_$(p.name) -> Pl_$(p.place.name) [style="dotted", dir="none"]\n"""
else
prt_pl_edge_str = prt_pl_edge_str * """
Pl_$(p.place.name) -> Prt_$(p.name) [style="dotted", dir="none"]\n"""
end
end
out_pl_trans_edge_str = ""
for a in out_arcs
arc_str = """[dir="back"]\n"""
out_pl_trans_edge_str = out_pl_trans_edge_str * """
Tr_$(a.transition.name) -> Pl_$(a.place.name)\n"""
end
for a in out_many_arcs
arc_str = """[dir="back", color = "black:invis:black"]\n"""
out_pl_trans_edge_str = out_pl_trans_edge_str * """
Tr_$(a.transition.name) -> Pl_$(a.place.name)\n"""
end
sub_gh_end2 = """
border_node [shape=plaintext, label="", width=2, height=2, style=dotted, color=black];
}
"""
end_gen = """
border_node [shape=plaintext, label="", width=2, height=2, style=dotted, color=black];
}
graph [bgcolor=white, pad=0.3]
}
"""
return gen_str_init * sub_gh_begin * place_string * transition_string * in_pl_trans_edge_str * out_pl_trans_edge_str * sub_gh_end2 * prt_pl_edge_str * end_gen
end