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literals
|
2 |
+
|
3 |
+
from ._utils import get_hash, get_hash_int
|
4 |
+
from builtins import object
|
5 |
+
from collections import namedtuple
|
6 |
+
|
7 |
+
|
8 |
+
class DagNode(object):
|
9 |
+
"""Node in a directed-acyclic graph (DAG).
|
10 |
+
|
11 |
+
Edges:
|
12 |
+
DagNodes are connected by edges. An edge connects two nodes with a label for each side:
|
13 |
+
- ``upstream_node``: upstream/parent node
|
14 |
+
- ``upstream_label``: label on the outgoing side of the upstream node
|
15 |
+
- ``downstream_node``: downstream/child node
|
16 |
+
- ``downstream_label``: label on the incoming side of the downstream node
|
17 |
+
|
18 |
+
For example, DagNode A may be connected to DagNode B with an edge labelled "foo" on A's side, and "bar" on B's
|
19 |
+
side:
|
20 |
+
|
21 |
+
_____ _____
|
22 |
+
| | | |
|
23 |
+
| A >[foo]---[bar]> B |
|
24 |
+
|_____| |_____|
|
25 |
+
|
26 |
+
Edge labels may be integers or strings, and nodes cannot have more than one incoming edge with the same label.
|
27 |
+
|
28 |
+
DagNodes may have any number of incoming edges and any number of outgoing edges. DagNodes keep track only of
|
29 |
+
their incoming edges, but the entire graph structure can be inferred by looking at the furthest downstream
|
30 |
+
nodes and working backwards.
|
31 |
+
|
32 |
+
Hashing:
|
33 |
+
DagNodes must be hashable, and two nodes are considered to be equivalent if they have the same hash value.
|
34 |
+
|
35 |
+
Nodes are immutable, and the hash should remain constant as a result. If a node with new contents is required,
|
36 |
+
create a new node and throw the old one away.
|
37 |
+
|
38 |
+
String representation:
|
39 |
+
In order for graph visualization tools to show useful information, nodes must be representable as strings. The
|
40 |
+
``repr`` operator should provide a more or less "full" representation of the node, and the ``short_repr``
|
41 |
+
property should be a shortened, concise representation.
|
42 |
+
|
43 |
+
Again, because nodes are immutable, the string representations should remain constant.
|
44 |
+
"""
|
45 |
+
|
46 |
+
def __hash__(self):
|
47 |
+
"""Return an integer hash of the node."""
|
48 |
+
raise NotImplementedError()
|
49 |
+
|
50 |
+
def __eq__(self, other):
|
51 |
+
"""Compare two nodes; implementations should return True if (and only if) hashes match."""
|
52 |
+
raise NotImplementedError()
|
53 |
+
|
54 |
+
def __repr__(self, other):
|
55 |
+
"""Return a full string representation of the node."""
|
56 |
+
raise NotImplementedError()
|
57 |
+
|
58 |
+
@property
|
59 |
+
def short_repr(self):
|
60 |
+
"""Return a partial/concise representation of the node."""
|
61 |
+
raise NotImplementedError()
|
62 |
+
|
63 |
+
@property
|
64 |
+
def incoming_edge_map(self):
|
65 |
+
"""Provides information about all incoming edges that connect to this node.
|
66 |
+
|
67 |
+
The edge map is a dictionary that maps an ``incoming_label`` to ``(outgoing_node, outgoing_label)``. Note that
|
68 |
+
implicity, ``incoming_node`` is ``self``. See "Edges" section above.
|
69 |
+
"""
|
70 |
+
raise NotImplementedError()
|
71 |
+
|
72 |
+
|
73 |
+
DagEdge = namedtuple(
|
74 |
+
'DagEdge',
|
75 |
+
[
|
76 |
+
'downstream_node',
|
77 |
+
'downstream_label',
|
78 |
+
'upstream_node',
|
79 |
+
'upstream_label',
|
80 |
+
'upstream_selector',
|
81 |
+
],
|
82 |
+
)
|
83 |
+
|
84 |
+
|
85 |
+
def get_incoming_edges(downstream_node, incoming_edge_map):
|
86 |
+
edges = []
|
87 |
+
for downstream_label, upstream_info in list(incoming_edge_map.items()):
|
88 |
+
upstream_node, upstream_label, upstream_selector = upstream_info
|
89 |
+
edges += [
|
90 |
+
DagEdge(
|
91 |
+
downstream_node,
|
92 |
+
downstream_label,
|
93 |
+
upstream_node,
|
94 |
+
upstream_label,
|
95 |
+
upstream_selector,
|
96 |
+
)
|
97 |
+
]
|
98 |
+
return edges
|
99 |
+
|
100 |
+
|
101 |
+
def get_outgoing_edges(upstream_node, outgoing_edge_map):
|
102 |
+
edges = []
|
103 |
+
for upstream_label, downstream_infos in sorted(outgoing_edge_map.items()):
|
104 |
+
for downstream_info in downstream_infos:
|
105 |
+
downstream_node, downstream_label, downstream_selector = downstream_info
|
106 |
+
edges += [
|
107 |
+
DagEdge(
|
108 |
+
downstream_node,
|
109 |
+
downstream_label,
|
110 |
+
upstream_node,
|
111 |
+
upstream_label,
|
112 |
+
downstream_selector,
|
113 |
+
)
|
114 |
+
]
|
115 |
+
return edges
|
116 |
+
|
117 |
+
|
118 |
+
class KwargReprNode(DagNode):
|
119 |
+
"""A DagNode that can be represented as a set of args+kwargs.
|
120 |
+
"""
|
121 |
+
|
122 |
+
@property
|
123 |
+
def __upstream_hashes(self):
|
124 |
+
hashes = []
|
125 |
+
for downstream_label, upstream_info in list(self.incoming_edge_map.items()):
|
126 |
+
upstream_node, upstream_label, upstream_selector = upstream_info
|
127 |
+
hashes += [
|
128 |
+
hash(x)
|
129 |
+
for x in [
|
130 |
+
downstream_label,
|
131 |
+
upstream_node,
|
132 |
+
upstream_label,
|
133 |
+
upstream_selector,
|
134 |
+
]
|
135 |
+
]
|
136 |
+
return hashes
|
137 |
+
|
138 |
+
@property
|
139 |
+
def __inner_hash(self):
|
140 |
+
props = {'args': self.args, 'kwargs': self.kwargs}
|
141 |
+
return get_hash(props)
|
142 |
+
|
143 |
+
def __get_hash(self):
|
144 |
+
hashes = self.__upstream_hashes + [self.__inner_hash]
|
145 |
+
return get_hash_int(hashes)
|
146 |
+
|
147 |
+
def __init__(self, incoming_edge_map, name, args, kwargs):
|
148 |
+
self.__incoming_edge_map = incoming_edge_map
|
149 |
+
self.name = name
|
150 |
+
self.args = args
|
151 |
+
self.kwargs = kwargs
|
152 |
+
self.__hash = self.__get_hash()
|
153 |
+
|
154 |
+
def __hash__(self):
|
155 |
+
return self.__hash
|
156 |
+
|
157 |
+
def __eq__(self, other):
|
158 |
+
return hash(self) == hash(other)
|
159 |
+
|
160 |
+
@property
|
161 |
+
def short_hash(self):
|
162 |
+
return '{:x}'.format(abs(hash(self)))[:12]
|
163 |
+
|
164 |
+
def long_repr(self, include_hash=True):
|
165 |
+
formatted_props = ['{!r}'.format(arg) for arg in self.args]
|
166 |
+
formatted_props += [
|
167 |
+
'{}={!r}'.format(key, self.kwargs[key]) for key in sorted(self.kwargs)
|
168 |
+
]
|
169 |
+
out = '{}({})'.format(self.name, ', '.join(formatted_props))
|
170 |
+
if include_hash:
|
171 |
+
out += ' <{}>'.format(self.short_hash)
|
172 |
+
return out
|
173 |
+
|
174 |
+
def __repr__(self):
|
175 |
+
return self.long_repr()
|
176 |
+
|
177 |
+
@property
|
178 |
+
def incoming_edges(self):
|
179 |
+
return get_incoming_edges(self, self.incoming_edge_map)
|
180 |
+
|
181 |
+
@property
|
182 |
+
def incoming_edge_map(self):
|
183 |
+
return self.__incoming_edge_map
|
184 |
+
|
185 |
+
@property
|
186 |
+
def short_repr(self):
|
187 |
+
return self.name
|
188 |
+
|
189 |
+
|
190 |
+
def topo_sort(downstream_nodes):
|
191 |
+
marked_nodes = []
|
192 |
+
sorted_nodes = []
|
193 |
+
outgoing_edge_maps = {}
|
194 |
+
|
195 |
+
def visit(
|
196 |
+
upstream_node,
|
197 |
+
upstream_label,
|
198 |
+
downstream_node,
|
199 |
+
downstream_label,
|
200 |
+
downstream_selector=None,
|
201 |
+
):
|
202 |
+
if upstream_node in marked_nodes:
|
203 |
+
raise RuntimeError('Graph is not a DAG')
|
204 |
+
|
205 |
+
if downstream_node is not None:
|
206 |
+
outgoing_edge_map = outgoing_edge_maps.get(upstream_node, {})
|
207 |
+
outgoing_edge_infos = outgoing_edge_map.get(upstream_label, [])
|
208 |
+
outgoing_edge_infos += [
|
209 |
+
(downstream_node, downstream_label, downstream_selector)
|
210 |
+
]
|
211 |
+
outgoing_edge_map[upstream_label] = outgoing_edge_infos
|
212 |
+
outgoing_edge_maps[upstream_node] = outgoing_edge_map
|
213 |
+
|
214 |
+
if upstream_node not in sorted_nodes:
|
215 |
+
marked_nodes.append(upstream_node)
|
216 |
+
for edge in upstream_node.incoming_edges:
|
217 |
+
visit(
|
218 |
+
edge.upstream_node,
|
219 |
+
edge.upstream_label,
|
220 |
+
edge.downstream_node,
|
221 |
+
edge.downstream_label,
|
222 |
+
edge.upstream_selector,
|
223 |
+
)
|
224 |
+
marked_nodes.remove(upstream_node)
|
225 |
+
sorted_nodes.append(upstream_node)
|
226 |
+
|
227 |
+
unmarked_nodes = [(node, None) for node in downstream_nodes]
|
228 |
+
while unmarked_nodes:
|
229 |
+
upstream_node, upstream_label = unmarked_nodes.pop()
|
230 |
+
visit(upstream_node, upstream_label, None, None)
|
231 |
+
return sorted_nodes, outgoing_edge_maps
|