-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvis.py
More file actions
218 lines (179 loc) · 7.95 KB
/
vis.py
File metadata and controls
218 lines (179 loc) · 7.95 KB
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
'''
PM4Py – A Process Mining Library for Python
Copyright (C) 2024 Process Intelligence Solutions UG (haftungsbeschränkt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see this software project's root or
visit <https://www.gnu.org/licenses/>.
Website: https://processintelligence.solutions
Contact: info@processintelligence.solutions
'''
from pm4py.util import exec_utils
from enum import Enum
import tempfile
from graphviz import Digraph
from typing import Optional, Dict, Any
from bpmn import BPMN
from pm4py.visualization.common import gview
from pm4py.visualization.common import save as gsave
from pm4py.util import constants
import graphviz
class Parameters(Enum):
FORMAT = "format"
RANKDIR = "rankdir"
FONT_SIZE = "font_size"
BGCOLOR = "bgcolor"
ENABLE_SWIMLANES = "enable_swimlanes"
INCLUDE_NAME_IN_EVENTS = "include_name_in_events"
SWIMLANES_MARGIN = "swimlanes_margin"
ENABLE_GRAPH_TITLE = "enable_graph_title"
GRAPH_TITLE = "graph_title"
ENDPOINTS_SHAPE = "endpoints_shape"
def add_bpmn_node(graph, n, font_size, include_name_in_events, endpoints_shape):
n_id = str(id(n))
node_label = str(n.name) if include_name_in_events else ""
if isinstance(n, BPMN.Task):
graph.node(n_id, shape="box", label=n.get_name(), fontsize=font_size)
elif isinstance(n, BPMN.dataObjectReference):
graph.node(n_id, shape="box", label=n.get_name(), style="filled", fillcolor="pink", fontsize=font_size)
elif isinstance(n, BPMN.dataStoreReference):
graph.node(n_id, shape="box", label=n.get_name(), style="filled", fillcolor="gray", fontsize=font_size)
elif isinstance(n, BPMN.StartEvent):
graph.node(n_id, label="", shape=endpoints_shape, style="filled", fillcolor="green", fontsize=font_size)
elif isinstance(n, BPMN.EndEvent):
graph.node(n_id, label="", shape=endpoints_shape, style="filled", fillcolor="orange", fontsize=font_size)
elif isinstance(n, BPMN.Event):
graph.node(n_id, label=node_label, shape="underline", fontsize=font_size)
elif isinstance(n, BPMN.TextAnnotation):
graph.node(n_id, shape="box", label=n.text, fontsize=font_size)
elif isinstance(n, BPMN.ParallelGateway):
graph.node(n_id, label="+", shape="diamond", fontsize=font_size)
elif isinstance(n, BPMN.ExclusiveGateway):
graph.node(n_id, label="X", shape="diamond", fontsize=font_size)
elif isinstance(n, BPMN.EventBasedGateway):
graph.node(n_id, label="E", shape="diamond", fontsize=font_size)
elif isinstance(n, BPMN.InclusiveGateway):
graph.node(n_id, label="O", shape="diamond", fontsize=font_size)
else:
# do nothing here
return False
return True
def apply(bpmn_graph: BPMN, parameters: Optional[Dict[Any, Any]] = None) -> graphviz.Digraph:
"""
Visualize a BPMN graph
Parameters
-------------
bpmn_graph
BPMN graph
parameters
Parameters of the visualization, including:
- Parameters.FORMAT: the format of the visualization
- Parameters.RANKDIR: the direction of the representation (default: LR)
Returns
------------
gviz
Graphviz representation
"""
if parameters is None:
parameters = {}
from bpmn import BPMN
from pm4py.objects.bpmn.util.sorting import get_sorted_nodes_edges
image_format = exec_utils.get_param_value(Parameters.FORMAT, parameters, "png")
rankdir = exec_utils.get_param_value(Parameters.RANKDIR, parameters, constants.DEFAULT_RANKDIR_GVIZ)
font_size = exec_utils.get_param_value(Parameters.FONT_SIZE, parameters, 12)
font_size = str(font_size)
bgcolor = exec_utils.get_param_value(Parameters.BGCOLOR, parameters, constants.DEFAULT_BGCOLOR)
enable_swimlanes = exec_utils.get_param_value(Parameters.ENABLE_SWIMLANES, parameters, True)
include_name_in_events = exec_utils.get_param_value(Parameters.INCLUDE_NAME_IN_EVENTS, parameters, True)
swimlanes_margin = exec_utils.get_param_value(Parameters.SWIMLANES_MARGIN, parameters, 35)
swimlanes_margin = str(swimlanes_margin)
enable_graph_title = exec_utils.get_param_value(Parameters.ENABLE_GRAPH_TITLE, parameters, constants.DEFAULT_ENABLE_GRAPH_TITLES)
graph_title = exec_utils.get_param_value(Parameters.GRAPH_TITLE, parameters, "BPMN Diagram")
endpoints_shape = exec_utils.get_param_value(Parameters.ENDPOINTS_SHAPE, parameters, "circle")
filename = tempfile.NamedTemporaryFile(suffix='.gv')
filename.close()
viz = Digraph("", filename=filename.name, engine='dot', graph_attr={'bgcolor': bgcolor})
viz.graph_attr['rankdir'] = rankdir
nodes, edges = get_sorted_nodes_edges(bpmn_graph)
process_ids = []
for n in nodes:
if n.process not in process_ids:
process_ids.append(n.process)
process_ids_members = {n.process: list() for n in nodes}
for n in nodes:
process_ids_members[n.process].append(n)
participant_nodes = [n for n in nodes if isinstance(n, BPMN.Participant)]
pref_pname = {x.process_ref: x.name for x in participant_nodes}
pref_pid = {x.process_ref: str(id(x)) for x in participant_nodes}
added_nodes = set()
if len(participant_nodes) < 1 or not enable_swimlanes:
for n in nodes:
if add_bpmn_node(viz, n, font_size, include_name_in_events, endpoints_shape):
added_nodes.add(str(id(n)))
else:
# style='invis'
viz.node('@@anchorStart', style='invis')
viz.node('@@anchorEnd', style='invis')
for subp in process_ids:
this_added_nodes = []
if subp in pref_pname:
with viz.subgraph(name="cluster"+pref_pid[subp]) as c:
c.attr(label=pref_pname[subp])
c.attr(margin=swimlanes_margin)
for n in process_ids_members[subp]:
if add_bpmn_node(c, n, font_size, include_name_in_events, endpoints_shape):
added_nodes.add(str(id(n)))
this_added_nodes.append(str(id(n)))
#c.attr(rank='same')
if this_added_nodes:
viz.edge('@@anchorStart', this_added_nodes[0], style='invis')
viz.edge(this_added_nodes[-1], '@@anchorEnd', style='invis')
for e in edges:
n_id_1 = str(id(e[0]))
n_id_2 = str(id(e[1]))
if n_id_1 in added_nodes and n_id_2 in added_nodes:
viz.edge(n_id_1, n_id_2)
viz.attr(overlap='false')
viz.format = image_format.replace("html", "plain-ext")
return viz
def save(gviz: graphviz.Digraph, output_file_path: str, parameters=None):
"""
Save the diagram
Parameters
-----------
gviz
GraphViz diagram
output_file_path
Path where the GraphViz output should be saved
"""
gsave.save(gviz, output_file_path, parameters=parameters)
return ""
def view(gviz: graphviz.Digraph, parameters=None):
"""
View the diagram
Parameters
-----------
gviz
GraphViz diagram
"""
if constants.DEFAULT_ENABLE_VISUALIZATIONS_VIEW:
return gview.view(gviz, parameters=parameters)
def matplotlib_view(gviz: graphviz.Digraph, parameters=None):
"""
Views the diagram using Matplotlib
Parameters
---------------
gviz
Graphviz
"""
if constants.DEFAULT_ENABLE_VISUALIZATIONS_VIEW:
return gview.matplotlib_view(gviz, parameters=parameters)
def view_bpmn(bpmn_graph):
view(apply(bpmn_graph))