-
Notifications
You must be signed in to change notification settings - Fork 59
/
tc_meta.rb
361 lines (304 loc) · 8.35 KB
/
tc_meta.rb
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
require './test_common'
require 'bud/graphs.rb'
require 'bud/viz_util.rb'
require 'bud/depanalysis'
require 'bud/meta_algebra'
include VizUtil
class LocalShortestPaths
include Bud
state do
table :link, [:from, :to, :cost]
table :link2, [:from, :to, :cost]
table :link3, [:from, :to, :cost]
table :empty, [:ident]
table :path, [:from, :to, :nxt, :cost]
table :shortest, [:from, :to] => [:nxt, :cost]
table :minz, [:cost]
table :minmaxsumcntavg, [:from, :to] => [:mincost, :maxcost, :sumcost, :cnt, :avgcost]
end
bloom do
link2 <= link.map {|l| l unless empty.include? [l.ident]}
path <= link2.map {|e| [e.from, e.to, e.to, e.cost]}
path <= (link2 * path).pairs do |l, p|
[l.from, p.to, p.from, l.cost+p.cost] if l.to == p.from
end
shortest <= path.argagg(:min, [path.from, path.to], path.cost)
minmaxsumcntavg <= path.group([path.from, path.to], min(path.cost), min(path.cost), sum(path.cost), count, avg(path.cost))
minz <= shortest.group(nil, min(shortest.cost))
link3 <= path.map {|p| [p.from, p.to, p.cost]}
link3 <- (link3 * shortest).lefts(:from => :from, :to => :to)
end
end
class Underspecified
include Bud
state do
interface input, :iin
interface output, :iout
end
end
class KTest
include Bud
state do
interface input, :sinkhole
interface input, :upd, [:datacol]
interface input, :req, [:ident]
interface output, :resp, [:ident, :datacol]
table :mystate, [:datacol]
end
bloom :update do
mystate <+ upd
mystate <- (upd * mystate).rights
end
bloom :respond do
resp <= (req * mystate).pairs {|r, s| [r.ident, s.datacol]}
end
end
class KTest2 < KTest
state do
# make sure :node isn't reserved
scratch :noder
end
bloom :update do
mystate <= upd
noder <= upd
temp :interm <= mystate
mystate <- (upd * interm).rights
end
end
class KTest3 < KTest
bloom :update do
mystate <= upd.map {|u| u unless mystate.include? u}
end
end
class TestStratTemporal
include Bud
state do
scratch :foo, [:loc, :a]
table :foo_persist, [:loc, :a]
scratch :foo_cnt, [:a] => [:cnt]
end
bootstrap do
foo <= [["xyz",1], ["xyz",2], ["xyz",3]]
end
bloom do
foo_persist <= foo
foo_cnt <= foo_persist.group([:loc], count)
foo_persist <- ((if foo_cnt[["xyz"]] and
foo_cnt[["xyz"]].cnt == 3
foo_persist
end) or [])
end
end
class Divergence
include Bud
include MetaAlgebra
include MetaReports
state do
interface input, :cli1, [:data]
channel :chan, [:@loc, :data]
table :serv1, chan.schema
scratch :agg, [:data, :cnt]
channel :outs, [:@loc, :data, :cnt]
interface output, :gone, outs.schema
end
bloom do
chan <~ cli1{|c| ["localhost:12345", c.data]}
serv1 <= chan
agg <= serv1.group([serv1.data], count)
outs <~ agg{|a| ["localhost:12345", a.data, a.cnt]}
gone <= outs
end
end
class TestMeta < MiniTest::Unit::TestCase
def test_paths
program = LocalShortestPaths.new
assert_equal(4, program.stratified_rules.length)
tally = 0
program.t_depends.each do |dep|
next if VizUtil.ma_tables.keys.include? dep.lhs.to_sym
if dep.lhs == "shortest" and dep.body == "path"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.lhs == "minz" and dep.body == "shortest"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.lhs == "minmaxsumcntavg" and dep.body == "path"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.lhs == "link2" and dep.body == "empty"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.lhs == "link3" and dep.body == "shortest"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.lhs == "link3" and dep.body == "link3"
assert(dep.nm, "NM rule")
tally += 1
elsif dep.body == "count"
# weird: count is now getting parsed as a table
else
assert(!dep.nm, "Monotonic rule marked NM: #{dep.inspect}")
end
end
assert_equal(6, tally)
end
def test_unstrat
assert_raises(Bud::CompileError) { KTest3.new }
end
def scratch_dir
dir = '/tmp/' + Time.new.to_f.to_s
Dir.mkdir(dir)
return dir
end
def test_visualization
program = KTest2.new(:trace => true, :dump_rewrite => true, :port => 54321)
File.delete("KTest2_rewritten.txt")
`rm -r DBM_KTest2*`
dir = scratch_dir
program.run_bg
program.sync_do
write_graphs({}, program.builtin_tables, program.t_cycle, program.t_depends, program.t_rules, "#{dir}/test_viz", dir, :dot, false, nil, 1, {})
program.stop
end
def get_content(file)
content = ''
fp = File.open(file, "r")
while (s = fp.gets)
content += s
end
fp.close
content
end
def str2struct(content)
ret = {}
content.split(";").each do |con|
if con =~ /([^\[]+)\s*\[([^\]]+)\]/
lhs, rhs = $1, $2
lhs = lhs.gsub(/\s+/, "")
ret[lhs] = {}
rhs.split(/\s*,\s*/).each do |pair|
k, v = pair.split(/\s*=\s*/)
ret[lhs][k] = v
end
end
end
ret
end
def test_plotting
$stdout = StringIO.new("", "w")
program = KTest2.new(:output => :dot)
dep = DepAnalysis.new
program.run_bg
program.sync_do
program.sync_do
program.sync_do
dep.providing <+ program.t_provides.to_a
dep.depends <+ program.t_depends.map{|d| [d.lhs, d.op, d.body, d.nm]}
dep.sync_do
dir = scratch_dir
graph_from_instance(program, "#{dir}/test_graphing", dir, true, :dot)
content = get_content("#{dir}/test_graphing.svg")
looks = str2struct(content)
assert_match(/upd -> \"interm, mystate\" \[.*label=\" \+\/\-\"/, content)
assert_match("S -> upd", content)
assert_match("S -> req", content)
assert_match("sinkhole -> \"\?\?\"", content)
refute_match(/upd -> \"\?\?\"/, content)
refute_match(/req -> \"\?\?\"/, content)
`rm -r #{dir}`
program.stop
$stdout = STDOUT
end
def test_labels
p = Divergence.new(:output => :dot)
p.run_bg
p.sync_do
p.sync_do
dir = scratch_dir
graph_from_instance(p, "#{dir}/test_labels", dir, true, :dot)
content = get_content("#{dir}/test_labels.svg")
looks = str2struct(content)
assert_equal("yellow", looks["serv1"]["color"])
assert_equal("rect", looks["serv1"]["shape"])
assert_equal("red", looks["agg"]["color"])
assert_equal("yellow", looks["chan"]["color"])
assert_equal("dashed", looks["cli1->chan"]["style"])
assert_equal("\"outs\\n(D)\"", looks["outs"]["label"])
assert_equal("\"chan\\n(A)\"", looks["chan"]["label"])
`rm -r #{dir}`
p.stop
end
def test_underspecified
u = Underspecified.new
assert_equal(2, u.t_underspecified.length)
u.t_underspecified.each do |u|
case u[0]
when "iin" then assert(u[1])
when "iout" then assert(!u[1])
end
end
end
def test_temporal_strat
t = TestStratTemporal.new
assert_equal(3, t.stratified_rules.length)
t.tick
assert_equal([["xyz", 1], ["xyz", 2], ["xyz", 3]], t.foo_persist.to_a.sort)
t.tick
assert_equal([], t.foo_persist.to_a.sort)
end
def test_spacetime
inp = [
[:ping, :a, :b, 1, 3],
[:pong, :b, :a, 4, 2],
[:ping, :a, :b, 3, 5],
[:pong, :b, :a, 6, 4]
];
do_spacetime(inp, "foofoo")
`rm foofoo.svg`
end
def test_spacetime2
inp = [
[:ping, :a, :b, 1, 3],
[:ping, :b, :c, 4, 11],
[:pong, :c, :b, 12, 5],
[:pong, :b, :a, 6, 2]
]
do_spacetime(inp, "foo2")
`rm foo2.svg`
end
def test_spacetime3
inp = [
[:ping1, :a, :b, 1, 3],
[:ping2, :a, :b, 1, 4],
[:ping3, :a, :b, 1, 5],
[:ping4, :a, :b, 1, 6]
]
do_spacetime(inp, "foo3")
`rm foo3.svg`
end
def do_spacetime(inp, out)
st = SpaceTime.new(inp)
st.process
st.finish(out)
end
end
class TestThetaMeta < MiniTest::Unit::TestCase
class ThetaMonotoneJoin
include Bud
state do
table :a
table :b
table :c
end
bloom do
c <= (a * b).pairs(a.key => b.key)
end
end
def test_theta
p = ThetaMonotoneJoin.new
p.t_depends.each do |dep|
next if VizUtil.ma_tables.keys.include? dep.lhs.to_sym
assert(!dep.nm, "this dependency should't be marked nonmonotonic: #{dep.inspect}")
end
end
end