-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgraph.py
164 lines (120 loc) · 4.83 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
"""The implementation of dataflow graph edge, node, and graph objects, used to run a dataflow program."""
from collections import deque
from enum import Enum
class MessageType(Enum):
DATA = 1
FRONTIER = 2
class DifferenceStreamReader:
"""A read handle to a dataflow edge that receives data and frontier updates from a writer.
The data received over this edge are pairs of (version, Collection) and the frontier
updates are either integers (in the one dimensional case) or Antichains (in the general
case).
"""
def __init__(self, queue):
self._queue = queue
def drain(self):
out = []
while len(self._queue) > 0:
out.append(self._queue.pop())
return out
def is_empty(self):
return len(self._queue) == 0
def probe_frontier_less_than(self, frontier):
for (typ, msg) in self._queue:
if typ == MessageType.FRONTIER:
received_frontier = msg
if frontier.less_equal(received_frontier):
return False
return True
class DifferenceStreamWriter:
"""A write handle to a dataflow edge that is allowed to publish data and send
frontier updates.
"""
def __init__(self):
self._queues = []
self.frontier = None
def send_data(self, version, collection):
if isinstance(version, int):
assert self.frontier is None or self.frontier <= version
else:
assert self.frontier is None or self.frontier.less_equal_version(version)
for q in self._queues:
q.appendleft((MessageType.DATA, (version, collection)))
def send_frontier(self, frontier):
if isinstance(frontier, int):
assert self.frontier is None or self.frontier <= frontier
else:
assert self.frontier is None or self.frontier.less_equal(frontier)
self.frontier = frontier
for q in self._queues:
q.appendleft((MessageType.FRONTIER, frontier))
def _new_reader(self):
q = deque()
self._queues.append(q)
return DifferenceStreamReader(q)
class Operator:
"""A generic implementation of a dataflow operator (node) that has multiple incoming edges (read handles) and
one outgoing edge (write handle).
"""
def __init__(self, inputs, output, f, initial_frontier):
self.inputs = inputs
self.output = output
self.f = f
self.pending_work = False
self.input_frontiers = [initial_frontier for _ in self.inputs]
self.output_frontier = initial_frontier
def run(self):
self.f()
def pending_work(self):
if self.pending_work is True:
return True
for input_listener in self.inputs:
if input_listener.is_empty() is False:
return True
return False
def frontiers(self):
return (self.input_frontiers, self.output_frontier)
class UnaryOperator(Operator):
"""A convenience implementation of a dataflow operator that has a handle to one
incoming stream of data, and one handle to an outgoing stream of data.
"""
def __init__(self, input_a, output, f, initial_frontier):
super().__init__([input_a], output, f, initial_frontier)
def input_messages(self):
return self.inputs[0].drain()
def input_frontier(self):
return self.input_frontiers[0]
def set_input_frontier(self, frontier):
self.input_frontiers[0] = frontier
class BinaryOperator(Operator):
"""A convenience implementation of a dataflow operator that has a handle to two
incoming streams of data, and one handle to an outgoing stream of data.
"""
def __init__(self, input_a, input_b, output, f, initial_frontier):
super().__init__([input_a, input_b], output, f, initial_frontier)
def input_a_messages(self):
return self.inputs[0].drain()
def input_a_frontier(self):
return self.input_frontiers[0]
def set_input_a_frontier(self, frontier):
self.input_frontiers[0] = frontier
def input_b_messages(self):
return self.inputs[1].drain()
def input_b_frontier(self):
return self.input_frontiers[1]
def set_input_b_frontier(self, frontier):
self.input_frontiers[1] = frontier
class Graph:
"""An implementation of a dataflow graph.
This implementation needs to keep the entire set of nodes so that they
may be run, and only keeps a set of read handles to all edges for debugging
purposes. Calling this a graph instead of a 'bag of nodes' is misleading, because
this object does not actually know anything about the connections between the
various nodes.
"""
def __init__(self, streams, operators):
self.streams = streams
self.operators = operators
def step(self):
for op in self.operators:
op.run()