-
Notifications
You must be signed in to change notification settings - Fork 773
/
graph.py
337 lines (294 loc) · 11.7 KB
/
graph.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import inspect
import ast
import re
from .util import to_pod
def deindent_docstring(doc):
if doc:
# Find the indent to remove from the docstring. We consider the following possibilities:
# Option 1:
# """This is the first line
# This is the second line
# """
# Option 2:
# """
# This is the first line
# This is the second line
# """
# Option 3:
# """
# This is the first line
# This is the second line
# """
#
# In all cases, we can find the indent to remove by doing the following:
# - Check the first non-empty line, if it has an indent, use that as the base indent
# - If it does not have an indent and there is a second line, check the indent of the
# second line and use that
saw_first_line = False
matched_indent = None
for line in doc.splitlines():
if line:
matched_indent = re.match("[\t ]+", line)
if matched_indent is not None or saw_first_line:
break
saw_first_line = True
if matched_indent:
return re.sub(r"\n" + matched_indent.group(), "\n", doc).strip()
else:
return doc
else:
return ""
class DAGNode(object):
def __init__(self, func_ast, decos, doc):
self.name = func_ast.name
self.func_lineno = func_ast.lineno
self.decorators = decos
self.doc = deindent_docstring(doc)
self.parallel_step = any(getattr(deco, "IS_PARALLEL", False) for deco in decos)
# these attributes are populated by _parse
self.tail_next_lineno = 0
self.type = None
self.out_funcs = []
self.has_tail_next = False
self.invalid_tail_next = False
self.num_args = 0
self.foreach_param = None
self.num_parallel = 0
self.parallel_foreach = False
self._parse(func_ast)
# these attributes are populated by _traverse_graph
self.in_funcs = set()
self.split_parents = []
self.matching_join = None
# these attributes are populated by _postprocess
self.is_inside_foreach = False
def _expr_str(self, expr):
return "%s.%s" % (expr.value.id, expr.attr)
def _parse(self, func_ast):
self.num_args = len(func_ast.args.args)
tail = func_ast.body[-1]
# end doesn't need a transition
if self.name == "end":
# TYPE: end
self.type = "end"
# ensure that the tail an expression
if not isinstance(tail, ast.Expr):
return
# determine the type of self.next transition
try:
if not self._expr_str(tail.value.func) == "self.next":
return
self.has_tail_next = True
self.invalid_tail_next = True
self.tail_next_lineno = tail.lineno
self.out_funcs = [e.attr for e in tail.value.args]
keywords = dict(
(k.arg, getattr(k.value, "s", None)) for k in tail.value.keywords
)
if len(keywords) == 1:
if "foreach" in keywords:
# TYPE: foreach
self.type = "foreach"
if len(self.out_funcs) == 1:
self.foreach_param = keywords["foreach"]
self.invalid_tail_next = False
elif "num_parallel" in keywords:
self.type = "foreach"
self.parallel_foreach = True
if len(self.out_funcs) == 1:
self.num_parallel = keywords["num_parallel"]
self.invalid_tail_next = False
elif len(keywords) == 0:
if len(self.out_funcs) > 1:
# TYPE: split
self.type = "split"
self.invalid_tail_next = False
elif len(self.out_funcs) == 1:
# TYPE: linear
if self.name == "start":
self.type = "start"
elif self.num_args > 1:
self.type = "join"
else:
self.type = "linear"
self.invalid_tail_next = False
except AttributeError:
return
def __str__(self):
return """*[{0.name} {0.type} (line {0.func_lineno})]*
in_funcs={in_funcs}
out_funcs={out_funcs}
split_parents={parents}
matching_join={matching_join}
is_inside_foreach={is_inside_foreach}
decorators={decos}
num_args={0.num_args}
has_tail_next={0.has_tail_next} (line {0.tail_next_lineno})
invalid_tail_next={0.invalid_tail_next}
foreach_param={0.foreach_param}
parallel_step={0.parallel_step}
parallel_foreach={0.parallel_foreach}
-> {out}""".format(
self,
matching_join=self.matching_join and "[%s]" % self.matching_join,
is_inside_foreach=self.is_inside_foreach,
out_funcs=", ".join("[%s]" % x for x in self.out_funcs),
in_funcs=", ".join("[%s]" % x for x in self.in_funcs),
parents=", ".join("[%s]" % x for x in self.split_parents),
decos=" | ".join(map(str, self.decorators)),
out=", ".join("[%s]" % x for x in self.out_funcs),
)
class StepVisitor(ast.NodeVisitor):
def __init__(self, nodes, flow):
self.nodes = nodes
self.flow = flow
super(StepVisitor, self).__init__()
def visit_FunctionDef(self, node):
func = getattr(self.flow, node.name)
if hasattr(func, "is_step"):
self.nodes[node.name] = DAGNode(node, func.decorators, func.__doc__)
class FlowGraph(object):
def __init__(self, flow):
self.name = flow.__name__
self.nodes = self._create_nodes(flow)
self.doc = deindent_docstring(flow.__doc__)
# nodes sorted in topological order.
self.sorted_nodes = []
self._traverse_graph()
self._postprocess()
def _create_nodes(self, flow):
module = __import__(flow.__module__)
tree = ast.parse(inspect.getsource(module)).body
root = [n for n in tree if isinstance(n, ast.ClassDef) and n.name == self.name][
0
]
nodes = {}
StepVisitor(nodes, flow).visit(root)
return nodes
def _postprocess(self):
# any node who has a foreach as any of its split parents
# has is_inside_foreach=True *unless* all of those `foreach`s
# are joined by the node
for node in self.nodes.values():
foreaches = [
p for p in node.split_parents if self.nodes[p].type == "foreach"
]
if [f for f in foreaches if self.nodes[f].matching_join != node.name]:
node.is_inside_foreach = True
def _traverse_graph(self):
def traverse(node, seen, split_parents):
self.sorted_nodes.append(node.name)
if node.type in ("split", "foreach"):
node.split_parents = split_parents
split_parents = split_parents + [node.name]
elif node.type == "join":
# ignore joins without splits
if split_parents:
self[split_parents[-1]].matching_join = node.name
node.split_parents = split_parents
split_parents = split_parents[:-1]
else:
node.split_parents = split_parents
for n in node.out_funcs:
# graph may contain loops - ignore them
if n not in seen:
# graph may contain unknown transitions - ignore them
if n in self:
child = self[n]
child.in_funcs.add(node.name)
traverse(child, seen + [n], split_parents)
if "start" in self:
traverse(self["start"], [], [])
# fix the order of in_funcs
for node in self.nodes.values():
node.in_funcs = sorted(node.in_funcs)
def __getitem__(self, x):
return self.nodes[x]
def __contains__(self, x):
return x in self.nodes
def __iter__(self):
return iter(self.nodes.values())
def __str__(self):
return "\n".join(
str(n) for _, n in sorted((n.func_lineno, n) for n in self.nodes.values())
)
def output_dot(self):
def edge_specs():
for node in self.nodes.values():
for edge in node.out_funcs:
yield "%s -> %s;" % (node.name, edge)
def node_specs():
for node in self.nodes.values():
nodetype = "join" if node.num_args > 1 else node.type
yield '"{0.name}"' '[ label = <<b>{0.name}</b> | <font point-size="10">{type}</font>> ' ' fontname = "Helvetica" ' ' shape = "record" ];'.format(
node, type=nodetype
)
return (
"digraph {0.name} {{\n"
"{nodes}\n"
"{edges}\n"
"}}".format(
self, nodes="\n".join(node_specs()), edges="\n".join(edge_specs())
)
)
def output_steps(self):
steps_info = {}
graph_structure = []
def node_to_type(node):
if node.type in ["linear", "start", "end", "join"]:
return node.type
elif node.type == "split":
return "split-static"
elif node.type == "foreach":
if node.parallel_foreach:
return "split-parallel"
return "split-foreach"
return "unknown" # Should never happen
def node_to_dict(name, node):
d = {
"name": name,
"type": node_to_type(node),
"line": node.func_lineno,
"doc": node.doc,
"decorators": [
{
"name": deco.name,
"attributes": to_pod(deco.attributes),
"statically_defined": deco.statically_defined,
}
for deco in node.decorators
if not deco.name.startswith("_")
],
"next": node.out_funcs,
}
if d["type"] == "split-foreach":
d["foreach_artifact"] = node.foreach_param
elif d["type"] == "split-parallel":
d["num_parallel"] = node.num_parallel
if node.matching_join:
d["matching_join"] = node.matching_join
return d
def populate_block(start_name, end_name):
cur_name = start_name
resulting_list = []
while cur_name != end_name:
cur_node = self.nodes[cur_name]
node_dict = node_to_dict(cur_name, cur_node)
steps_info[cur_name] = node_dict
resulting_list.append(cur_name)
if cur_node.type not in ("start", "linear", "join"):
# We need to look at the different branches for this
resulting_list.append(
[
populate_block(s, cur_node.matching_join)
for s in cur_node.out_funcs
]
)
cur_name = cur_node.matching_join
else:
cur_name = cur_node.out_funcs[0]
return resulting_list
graph_structure = populate_block("start", "end")
steps_info["end"] = node_to_dict("end", self.nodes["end"])
graph_structure.append("end")
return steps_info, graph_structure