/
flow.dylan
195 lines (143 loc) · 5.37 KB
/
flow.dylan
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
Module: flow
author: Andreas Bogk and Hannes Mehnert
copyright: 2005-2011 Andreas Bogk and Hannes Mehnert. All rights reserved.
license: see license.txt in this distribution
define class <graph> (<object>)
slot nodes :: <stretchy-vector> = make(<stretchy-vector>);
end;
define abstract class <node> (<object>)
slot graph :: <graph> = *global-flow*, init-keyword: graph:;
end;
define open generic toplevel (node :: <node>);
define thread variable *global-flow* = make(<graph>);
define method initialize(node :: <node>, #rest rest, #key, #all-keys)
next-method();
add!(node.graph.nodes, node);
end;
define class <input> (<object>)
slot node :: <node>, required-init-keyword: node:;
slot connected-output :: false-or(<output>) = #f;
end;
define open class <push-input> (<input>)
end;
define open class <pull-input> (<input>)
end;
define class <output> (<object>)
slot node :: <node>, required-init-keyword: node:;
slot connected-input :: false-or(<input>) = #f;
end;
define open class <push-output> (<output>)
end;
define open class <pull-output> (<output>)
end;
define generic process (node :: <node>) => ();
define open generic get-inputs (node :: <node>) => (inputs);
define open generic get-outputs (node :: <node>) => (outputs);
define open generic output-label (output :: <output>) => (res);
define method output-label (output :: <output>) => (res);
""
end;
define open generic connect (output, input);
define open generic disconnect (output, input);
define method connect (output :: <push-output>, input :: <push-input>)
output.connected-input := input;
input.connected-output := output;
end;
define method disconnect (output :: <push-output>, input :: <push-input>)
output.connected-input := #f;
input.connected-output := #f;
end;
define method connect (output :: <pull-output>, input :: <pull-input>)
output.connected-input := input;
input.connected-output := output;
end;
define method disconnect (output :: <pull-output>, input :: <pull-input>)
output.connected-input := #f;
input.connected-output := #f;
end;
define open generic pull-data-aux (output :: <pull-output>, node :: <node>);
define method pull-data (input :: <pull-input>) => (data)
input.connected-output
& pull-data-aux(input.connected-output, input.connected-output.node)
end;
define open generic push-data-aux (input :: <push-input>, node :: <node>, data);
define method push-data (output :: <push-output>, data)
output.connected-input
& push-data-aux(output.connected-input, output.connected-input.node, data)
end;
define abstract class <single-input-node> (<node>)
slot the-input :: <input>, init-keyword: input:;
end;
define open abstract class <single-push-input-node> (<single-input-node>)
end;
define method initialize(node :: <single-push-input-node>, #rest rest, #key, #all-keys)
next-method();
node.the-input := make(<push-input>, node: node)
end;
define method get-inputs (node :: <single-input-node>) => (inputs)
list(node.the-input)
end;
define open abstract class <single-pull-input-node> (<single-input-node>)
end;
define method initialize(node :: <single-pull-input-node>, #rest rest, #key, #all-keys)
next-method();
node.the-input := make(<pull-input>, node: node)
end;
define abstract class <single-output-node> (<node>)
slot the-output :: <push-output>, init-keyword: output:;
end;
define open abstract class <single-push-output-node> (<single-output-node>)
end;
define method initialize(node :: <single-push-output-node>, #rest rest, #key, #all-keys)
next-method();
node.the-output := make(<push-output>, node: node)
end;
define method get-outputs (node :: <single-output-node>) => (outputs)
list(node.the-output)
end;
define open abstract class <single-pull-output-node> (<single-output-node>)
end;
define method initialize(node :: <single-pull-output-node>, #rest rest, #key, #all-keys)
next-method();
node.the-output := make(<pull-output>, node: node)
end;
define method connect (node :: <single-output-node>, input :: <input>)
connect(node.the-output, input)
end;
define method disconnect (node :: <single-output-node>, input :: <input>)
disconnect(node.the-output, input);
end;
define method connect (output :: <output>, node :: <single-input-node>)
connect(output, node.the-input)
end;
define method disconnect (output :: <output>, node :: <single-input-node>)
disconnect(output, node.the-input);
end;
define method connect (output :: <single-output-node>, input :: <single-input-node>)
connect(output.the-output, input.the-input)
end;
define method disconnect (output :: <single-output-node>, input :: <single-input-node>)
disconnect(output.the-output, input.the-input);
end;
define open abstract class <filter> (<single-push-input-node>, <single-push-output-node>)
end;
define class <closure-node> (<filter>)
constant slot closure :: <function>, required-init-keyword: closure:;
end;
define method push-data-aux (input :: <push-input>, node :: <closure-node>, data) => ()
node.closure(data);
end;
define class <queue> (<single-push-input-node>, <single-pull-output-node>)
slot queue :: <deque> = make(<deque>);
slot lock :: <lock> = make(<lock>);
end;
define method push-input-aux (input :: <push-input>, node :: <queue>, data)
with-lock(node.lock)
push-last(node.queue, data)
end
end;
define method pull-output-aux (output :: <pull-output>, node :: <queue>) => (data)
with-lock(node.lock)
node.queue.size > 0 & node.queue.pop
end
end;