omf_rete 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,232 @@
1
+ require 'omf_rete/store'
2
+ require 'omf_rete/indexed_tuple_set'
3
+ require 'omf_rete/join_op'
4
+ require 'omf_rete/planner/plan_builder'
5
+ require 'stringio'
6
+
7
+ include OMF::Rete
8
+ include OMF::Rete::Planner
9
+
10
+ class TestPlanner < Test::Unit::TestCase
11
+
12
+ def test_create_plan_builder
13
+ plan = [[:x?, :b, :c]]
14
+ store = Store.create(3)
15
+ pb = PlanBuilder.new(plan, store)
16
+ end
17
+
18
+ def _test_plan(plan, storeSize, expected = nil, inTuples = nil, outTuples = nil, outPattern = nil)
19
+ store = Store.create(storeSize)
20
+
21
+ # pb = PlanBuilder.new(plan, store)
22
+ # pb.build
23
+ # #pb.describe
24
+ # resT = []
25
+ # result = pb.materialize(outPattern) do |t|
26
+ # resT << t
27
+ # end
28
+
29
+ resT = []
30
+ result = store.subscribe(:test, plan, outPattern) do |t|
31
+ resT << t
32
+ end
33
+
34
+
35
+ out = StringIO.new
36
+ #result.describe(out, 0, 0, '|')
37
+ result.describe(out)
38
+ assert_equal(expected, out.string) if expected
39
+
40
+ if (inTuples)
41
+ inTuples.each do |t|
42
+ store.addTuple(t)
43
+ end
44
+ assert_equal(outTuples, resT)
45
+ end
46
+ result
47
+ end
48
+
49
+ def test_build_simple_plan
50
+ plan = [[:x?, :b, :c]]
51
+ exp = %{\
52
+ out: [x?]
53
+ processing
54
+ }
55
+ inT = [[:a, :b, :c], [:d, :b, :e]]
56
+ resT = [[:a]]
57
+ _test_plan plan, 3, exp, inT, resT
58
+ end
59
+
60
+ def test_build_simple_plan_loaded
61
+ plan = [[:x?, :b, :c]]
62
+ exp = %{\
63
+ out: [x?]
64
+ processing
65
+ }
66
+ inT = [[:a, :b, :c], [:d, :b, :e]]
67
+ resT = [[:a]]
68
+ _test_plan plan, 3, exp, inT, resT
69
+ end
70
+
71
+
72
+ def test_project
73
+ plan = [[:x?, :b, :y?]]
74
+ exp = %{\
75
+ out: [y?]
76
+ processing
77
+ }
78
+ inT = [[:a, :b, :c], [:d, :b, :e]]
79
+ resT = [[:c], [:e]]
80
+ _test_plan plan, 3, exp, inT, resT, [:y?]
81
+ end
82
+
83
+ def test_simple_project
84
+ plan = [[:x?, :b, :y?]]
85
+ exp = %{\
86
+ out: [y?]
87
+ processing
88
+ }
89
+ inT = [[:a, :b, :c], [:d, :b, :e]]
90
+ resT = [[:c], [:e]]
91
+ _test_plan plan, 3, exp, inT, resT, [:y?]
92
+ end
93
+
94
+ def test_simple_project2
95
+ plan = [[:x?, :b, :y?]]
96
+ exp = %{\
97
+ out: [x?]
98
+ processing
99
+ }
100
+ inT = [[:a, :b, :c], [:d, :b, :e]]
101
+ resT = [[:a], [:d]]
102
+ _test_plan plan, 3, exp, inT, resT, [:x?]
103
+ end
104
+
105
+ def test_simple_project3
106
+ plan = [[:x?, :b, :y?]]
107
+ exp = %{\
108
+ out: [y?, x?]
109
+ processing
110
+ }
111
+ inT = [[:a, :b, :c], [:d, :b, :e]]
112
+ resT = [[:c, :a], [:e, :d]]
113
+ _test_plan plan, 3, exp, inT, resT, [:y?, :x?]
114
+ end
115
+
116
+ def test_simple
117
+ plan = [[:x?, :b, :c], [:d, :e, :f]]
118
+ exp = %{\
119
+ out: [x?]
120
+ processing
121
+ }
122
+ inT = [[:a, :b, :c], [:d, :b, :e]]
123
+ resT = [[:a]]
124
+ _test_plan plan, 3, exp, inT, resT
125
+ end
126
+
127
+ def test_build_single_join
128
+ plan = [[:x?, :b, :c], [:x?, :b, :d]]
129
+ exp = %{\
130
+ out: [x?]
131
+ join: [x?] => [x?]
132
+ ts: [x?, b, c] (index: [x?])
133
+ ts: [x?, b, d] (index: [x?])
134
+ }
135
+ inT = [[:a, :b, :c], [:e, :b, :d], [:a, :b, :d]]
136
+ resT = [[:a]]
137
+ _test_plan plan, 3, exp, inT, resT
138
+ end
139
+
140
+ def test_build_single_join_with_project
141
+ plan = [[:x?, :b, :y?], [:x?, :y?, :d]]
142
+ exp = %{\
143
+ out: [y?]
144
+ join: [x?, y?] => [y?]
145
+ ts: [x?, b, y?] (index: [x?, y?])
146
+ ts: [x?, y?, d] (index: [x?, y?])
147
+ }
148
+ inT = [[:a, :b, :c], [:e, :b, :d], [:a, :c, :d]]
149
+ resT = [[:c]]
150
+ _test_plan plan, 3, exp, inT, resT, [:y?]
151
+ end
152
+
153
+ def test_build_single_join_with_project2
154
+ plan = [[:x?, :b, :y?], [:x?, :y?, :d]]
155
+ exp = %{\
156
+ out: [y?, x?]
157
+ join: [x?, y?] => [x?, y?]
158
+ ts: [x?, b, y?] (index: [x?, y?])
159
+ ts: [x?, y?, d] (index: [x?, y?])
160
+ }
161
+ inT = [[:a, :b, :c], [:e, :b, :d], [:a, :c, :d]]
162
+ resT = [[:c, :a]]
163
+ _test_plan plan, 3, exp, inT, resT, [:y?, :x?]
164
+ end
165
+
166
+
167
+ def test_build_two_joins
168
+ plan = [[:x?, :b, :c],
169
+ [:y?, :d, nil],
170
+ [:x?, :e, :y?]]
171
+ exp = %{\
172
+ out: [x?, y?]
173
+ join: [y?] => [x?, y?]
174
+ ts: [y?, d, *] (index: [y?])
175
+ ts: [x?, y?] (index: [y?])
176
+ join: [x?] => [x?, y?]
177
+ ts: [x?, b, c] (index: [x?])
178
+ ts: [x?, e, y?] (index: [x?])
179
+ }
180
+ inT = [[:x, :b, :c], [:y, :d, :f], [:x, :e, :y]]
181
+ resT = [[:x, :y]]
182
+ _test_plan plan, 3, exp, inT, resT
183
+
184
+ end
185
+
186
+ def test_build_three_joins
187
+ plan = [[:x?, :a, :b],
188
+ [:y?, :c, :d],
189
+ [:x?, :e, :z?],
190
+ [:z?, :f, :y?]]
191
+ exp = %{\
192
+ out: [x?, y?, z?]
193
+ join: [z?] => [x?, y?, z?]
194
+ ts: [x?, z?] (index: [z?])
195
+ join: [x?] => [x?, z?]
196
+ ts: [x?, a, b] (index: [x?])
197
+ ts: [x?, e, z?] (index: [x?])
198
+ ts: [y?, z?] (index: [z?])
199
+ join: [y?] => [y?, z?]
200
+ ts: [y?, c, d] (index: [y?])
201
+ ts: [z?, f, y?] (index: [y?])
202
+ }
203
+ inT = [[:x, :a, :b], [:y, :c, :d], [:x, :e, :z], [:z, :f, :y]]
204
+ resT = [[:x, :y, :z]]
205
+ _test_plan plan, 3, exp, inT, resT
206
+ end
207
+
208
+
209
+ def test_siblings
210
+ store = Store.create(3)
211
+ store.addTuple([:a, :hasParent, :p])
212
+ store.addTuple([:b, :hasParent, :p])
213
+
214
+ resT = Set.new
215
+ store.query([[:x?, :sibling_of, :y?]]) do |t|
216
+ resT << t
217
+ end
218
+ assert_equal(Set.new, resT)
219
+
220
+ subscription = [
221
+ [:x?, :hasParent, :p?],
222
+ [:y?, :hasParent, :p?],
223
+ OMF::Rete.differ(:x?, :y?)
224
+ ]
225
+ store.query(subscription, [:x?, :y?]) do |x, y|
226
+ store.addTuple([x, :sibling_of, y])
227
+ end
228
+
229
+ assert_equal(Set.new([[:b, :a], [:a, :b]]), resT)
230
+
231
+ end
232
+ end
@@ -0,0 +1,157 @@
1
+ require 'omf_rete/store'
2
+
3
+ include OMF::Rete
4
+
5
+ class TestStore < Test::Unit::TestCase
6
+ def test_create_store
7
+ store = Store.create(3)
8
+ end
9
+
10
+ def test_add_tuple
11
+ store = Store.create(3)
12
+ store.addTuple [:a, :b, :c]
13
+ end
14
+
15
+ def test_add_tuple2
16
+ store = Store.create(3)
17
+ store.add :a, :b, :c
18
+ end
19
+
20
+ def test_find
21
+ t1 = [:a, :b, :c]
22
+ t2 = [t1[0], t1[1], :d]
23
+ store = Store.create(3)
24
+
25
+ store.addTuple t1
26
+ store.addTuple t2
27
+
28
+ r = store.find t1
29
+ assert_equal r, Set.new([t1])
30
+
31
+ r = store.find [t1[0], t1[1], nil]
32
+ assert_equal r, Set.new([t1, t2])
33
+
34
+ r = store.find [t1[0], nil, t1[2]]
35
+ assert_equal r, Set.new([t1])
36
+
37
+ r = store.find [nil, nil, nil]
38
+ assert_equal r, Set.new([t1, t2])
39
+
40
+ r = store.find [:b, nil, nil]
41
+ assert_equal r, Set.new()
42
+
43
+ end
44
+
45
+ def test_find_tuple2
46
+ store = Store.create(3)
47
+ store.add :a, :b, :c
48
+
49
+ t1 = [:a, :b, :c]
50
+ r = store.find t1
51
+ assert_equal r, Set.new([t1])
52
+ end
53
+
54
+ def test_tset_init
55
+ t1 = [:a, :b, :c]
56
+ store = Store.create(3)
57
+ store.addTuple t1
58
+
59
+ ts = MockTSet.new
60
+ store.registerTSet(ts, t1)
61
+ assert_equal ts.tuples, [t1]
62
+ end
63
+
64
+ def test_tset_add_full
65
+ t1 = [:a, :b, :c]
66
+
67
+ store = Store.create(3)
68
+
69
+ ts = MockTSet.new
70
+ store.registerTSet(ts, t1)
71
+ assert_equal nil, ts.tuples
72
+
73
+ store.addTuple t1
74
+ assert_equal [t1], ts.tuples
75
+ end
76
+
77
+ def test_tset_add_last_nil
78
+ t1 = [:a, :b, :c]
79
+
80
+ store = Store.create(3)
81
+
82
+ ts = MockTSet.new
83
+ store.registerTSet(ts, [t1[0], t1[1], nil])
84
+ assert_equal nil, ts.tuples
85
+
86
+ store.addTuple t1
87
+ assert_equal [t1], ts.tuples
88
+ end
89
+
90
+ def test_tset_add_second_nil
91
+ t1 = [:a, :b, :c]
92
+
93
+ store = Store.create(3)
94
+
95
+ ts = MockTSet.new
96
+ store.registerTSet(ts, [t1[0], nil, t1[2]])
97
+ assert_equal nil, ts.tuples
98
+
99
+ store.addTuple t1
100
+ assert_equal [t1], ts.tuples
101
+ end
102
+
103
+ def test_tset_add_first_nil
104
+ t1 = [:a, :b, :c]
105
+
106
+ store = Store.create(3)
107
+
108
+ ts = MockTSet.new
109
+ store.registerTSet(ts, [nil, t1[1], t1[2]])
110
+ assert_equal nil, ts.tuples
111
+
112
+ store.addTuple t1
113
+ assert_equal [t1], ts.tuples
114
+ end
115
+
116
+ def test_tset_add_multiple_nil
117
+ t1 = [:a, :b, :c]
118
+
119
+ store = Store.create(3)
120
+
121
+ ts = MockTSet.new
122
+ store.registerTSet(ts, [nil, t1[1], nil])
123
+ assert_equal nil, ts.tuples
124
+
125
+ store.addTuple t1
126
+ assert_equal [t1], ts.tuples
127
+
128
+ t2 = [:d, :b, :f]
129
+ store.addTuple t2
130
+ assert_equal [t1, t2], ts.tuples
131
+
132
+ t3 = [:g, :h, :j]
133
+ store.addTuple t3
134
+ assert_equal [t1, t2], ts.tuples
135
+ end
136
+
137
+
138
+
139
+
140
+
141
+ # assert_instance_of OEDLMissingArgumentException, ex
142
+ # assert_equal :name, ex.argName
143
+ end
144
+
145
+ class MockTSet
146
+ attr_reader :tuples
147
+
148
+ def addTuple(t)
149
+ (@tuples ||= []) << t
150
+ end
151
+
152
+ def reset()
153
+ @tuples = nil
154
+ end
155
+ end # MockTSet
156
+
157
+
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omf_rete
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - NICTA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Tuple store with query and filter functionality.
15
+ email:
16
+ - omf-user@lists.nicta.com.au
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - README.md
23
+ - Rakefile
24
+ - lib/omf_rete.rb
25
+ - lib/omf_rete/abstract_tuple_set.rb
26
+ - lib/omf_rete/indexed_tuple_set.rb
27
+ - lib/omf_rete/join_op.rb
28
+ - lib/omf_rete/planner/abstract_plan.rb
29
+ - lib/omf_rete/planner/filter_plan.rb
30
+ - lib/omf_rete/planner/join_plan.rb
31
+ - lib/omf_rete/planner/plan_builder.rb
32
+ - lib/omf_rete/planner/plan_level_builder.rb
33
+ - lib/omf_rete/planner/plan_set.rb
34
+ - lib/omf_rete/planner/source_plan.rb
35
+ - lib/omf_rete/store.rb
36
+ - lib/omf_rete/store/alpha/alpha_element.rb
37
+ - lib/omf_rete/store/alpha/alpha_inner_element.rb
38
+ - lib/omf_rete/store/alpha/alpha_leaf_element.rb
39
+ - lib/omf_rete/store/alpha/alpha_store.rb
40
+ - lib/omf_rete/tuple_stream.rb
41
+ - lib/omf_rete/version.rb
42
+ - omf_rete.gemspec
43
+ - tests/test.rb
44
+ - tests/test_backtracking.rb
45
+ - tests/test_filter.rb
46
+ - tests/test_indexed_tuple_set.rb
47
+ - tests/test_join_op.rb
48
+ - tests/test_planner.rb
49
+ - tests/test_store.rb
50
+ homepage: https://www.mytestbed.net
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project: omf_rete
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A Rete implementation.
74
+ test_files: []