-
Notifications
You must be signed in to change notification settings - Fork 59
/
tc_meta.rb
219 lines (185 loc) · 5.33 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
require 'test_common'
require 'bud/graphs.rb'
require 'bud/viz_util.rb'
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, :next, :cost]
table :shortest, [:from, :to] => [:next, :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 TestMeta < Test::Unit::TestCase
def test_paths
program = LocalShortestPaths.new
assert_equal(5, program.strata.length)
tally = 0
program.t_depends.each do |dep|
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_raise(Bud::CompileError) { KTest3.new }
end
def scratch_dir
dir = '/tmp/' + Time.new.to_f.to_s
Dir.mkdir(dir)
return dir
end
def test_11visualization
program = KTest2.new(:trace => true, :dump_rewrite => true)
File.delete("KTest2_rewritten.txt")
`rm -r DBM_KTest2*`
dir = scratch_dir
program.run_bg
program.sync_do{}
write_graphs({}, program.t_cycle, program.t_depends, program.t_rules, "#{dir}/test_viz", dir, :dot, false, nil, 1, {})
program.stop_bg
end
def test_11plotting
program = KTest2.new(:output => :dot)
dep = DepAnalysis.new
program.run_bg
program.sync_do
program.sync_do
program.sync_do
program.t_depends_tc.each {|d| dep.depends_tc << d}
program.t_provides.each {|p| dep.providing << p}
dep.tick
dir = scratch_dir
graph_from_instance(program, "#{dir}/test_graphing", dir, true, :dot)
fp = File.open("#{dir}/test_graphing.svg", "r")
content = ''
while (s = fp.gets)
content += s
end
fp.close
assert_match(/upd -> \"interm, mystate\" \[label=\" \+\/\-\",.+?arrowhead=veeodot/, content)
assert_match("S -> upd", content)
assert_match("S -> req", content)
assert_match("sinkhole -> \"??\"", content)
assert_no_match(/upd -> \"\?\?\"/, content)
assert_no_match(/req -> \"\?\?\"/, content)
`rm -r #{dir}`
program.stop_bg
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.strata.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
end