porolog 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -13
- data/coverage/index.html +4564 -4552
- data/doc/Array.html +9 -9
- data/doc/Object.html +1 -1
- data/doc/Porolog.html +77 -67
- data/doc/Symbol.html +1 -1
- data/doc/_index.html +1 -1
- data/doc/file.README.html +22 -14
- data/doc/index.html +22 -14
- data/doc/top-level-namespace.html +1 -1
- data/lib/porolog.rb +4 -2
- data/lib/porolog/core_ext.rb +8 -8
- data/lib/porolog/goal.rb +2 -2
- data/lib/porolog/predicate/builtin.rb +34 -34
- data/lib/porolog/rule.rb +1 -1
- data/lib/porolog/variable.rb +7 -7
- data/test/porolog/arguments_test.rb +73 -73
- data/test/porolog/core_ext_test.rb +39 -39
- data/test/porolog/goal_test.rb +72 -72
- data/test/porolog/instantiation_test.rb +153 -153
- data/test/porolog/porolog_test.rb +272 -270
- data/test/porolog/predicate/builtin_test.rb +166 -166
- data/test/porolog/predicate_test.rb +85 -85
- data/test/porolog/rule_test.rb +69 -69
- data/test/porolog/scope_test.rb +89 -89
- data/test/porolog/tail_test.rb +23 -23
- data/test/porolog/value_test.rb +32 -34
- data/test/porolog/variable_test.rb +120 -120
- data/test/samples_test.rb +11 -11
- data/test/test_helper.rb +56 -61
- metadata +2 -2
@@ -16,53 +16,53 @@ describe 'Porolog' do
|
|
16
16
|
describe 'Predicate' do
|
17
17
|
|
18
18
|
it 'should default to creating predicates in the default scope' do
|
19
|
-
assert_equal :default, Predicate.scope.name
|
19
|
+
assert_equal :default, Porolog::Predicate.scope.name
|
20
20
|
|
21
|
-
test = Predicate.new 'test'
|
21
|
+
test = Porolog::Predicate.new 'test'
|
22
22
|
|
23
|
-
assert_includes Scope[:default].predicates, test
|
23
|
+
assert_includes Porolog::Scope[:default].predicates, test
|
24
24
|
end
|
25
25
|
|
26
26
|
describe '.scope' do
|
27
27
|
|
28
28
|
it 'should return the current scope for new predicates' do
|
29
|
-
assert_equal Scope[:default],
|
29
|
+
assert_equal Porolog::Scope[:default], Porolog::Predicate.scope
|
30
30
|
|
31
|
-
alpha = Predicate.new 'alpha', :new_scope
|
31
|
+
alpha = Porolog::Predicate.new 'alpha', :new_scope
|
32
32
|
|
33
|
-
assert_equal Scope[:default],
|
33
|
+
assert_equal Porolog::Scope[:default], Porolog::Predicate.scope
|
34
34
|
|
35
|
-
Predicate.scope :new_scope
|
35
|
+
Porolog::Predicate.scope :new_scope
|
36
36
|
|
37
|
-
assert_equal Scope[:new_scope],
|
37
|
+
assert_equal Porolog::Scope[:new_scope], Porolog::Predicate.scope
|
38
38
|
|
39
|
-
bravo = Predicate.new 'bravo'
|
39
|
+
bravo = Porolog::Predicate.new 'bravo'
|
40
40
|
|
41
|
-
assert_equal [],
|
42
|
-
assert_equal [alpha,bravo],
|
41
|
+
assert_equal [], Porolog::Scope[:default ].predicates
|
42
|
+
assert_equal [alpha,bravo], Porolog::Scope[:new_scope].predicates
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should allow predicate scope to be created and set' do
|
46
|
-
Predicate.scope :internal
|
47
|
-
assert_equal
|
46
|
+
Porolog::Predicate.scope :internal
|
47
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
48
48
|
|
49
|
-
Predicate.new 'alpha'
|
49
|
+
Porolog::Predicate.new 'alpha'
|
50
50
|
|
51
|
-
Predicate.scope :external
|
52
|
-
assert_equal
|
51
|
+
Porolog::Predicate.scope :external
|
52
|
+
assert_equal :external, Porolog::Predicate.scope.name
|
53
53
|
|
54
|
-
Predicate.new 'bravo'
|
54
|
+
Porolog::Predicate.new 'bravo'
|
55
55
|
|
56
|
-
Predicate.scope :internal
|
57
|
-
assert_equal
|
56
|
+
Porolog::Predicate.scope :internal
|
57
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
58
58
|
|
59
|
-
Predicate.new 'carly'
|
60
|
-
Predicate.new 'delta', :external
|
59
|
+
Porolog::Predicate.new 'carly'
|
60
|
+
Porolog::Predicate.new 'delta', :external
|
61
61
|
|
62
|
-
assert_equal
|
62
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
63
63
|
|
64
|
-
assert_equal [:alpha,:carly],
|
65
|
-
assert_equal [:bravo,:delta],
|
64
|
+
assert_equal [:alpha,:carly], Porolog::Scope[:internal].predicates.map(&:name)
|
65
|
+
assert_equal [:bravo,:delta], Porolog::Scope[:external].predicates.map(&:name)
|
66
66
|
end
|
67
67
|
|
68
68
|
end
|
@@ -70,26 +70,26 @@ describe 'Porolog' do
|
|
70
70
|
describe '.scope=' do
|
71
71
|
|
72
72
|
it 'should also allow predicate scope to be assigned' do
|
73
|
-
Predicate.scope = :internal
|
74
|
-
assert_equal
|
73
|
+
Porolog::Predicate.scope = :internal
|
74
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
75
75
|
|
76
|
-
alpha = Predicate.new 'alpha'
|
76
|
+
alpha = Porolog::Predicate.new 'alpha'
|
77
77
|
|
78
|
-
Predicate.scope = :external
|
79
|
-
assert_equal
|
78
|
+
Porolog::Predicate.scope = :external
|
79
|
+
assert_equal :external, Porolog::Predicate.scope.name
|
80
80
|
|
81
|
-
bravo = Predicate.new 'bravo'
|
81
|
+
bravo = Porolog::Predicate.new 'bravo'
|
82
82
|
|
83
|
-
Predicate.scope = :internal
|
84
|
-
assert_equal
|
83
|
+
Porolog::Predicate.scope = :internal
|
84
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
85
85
|
|
86
|
-
carly = Predicate.new 'carly'
|
87
|
-
delta = Predicate.new 'delta', :external
|
86
|
+
carly = Porolog::Predicate.new 'carly'
|
87
|
+
delta = Porolog::Predicate.new 'delta', :external
|
88
88
|
|
89
|
-
assert_equal
|
89
|
+
assert_equal :internal, Porolog::Predicate.scope.name
|
90
90
|
|
91
|
-
assert_equal [alpha,carly],
|
92
|
-
assert_equal [bravo,delta],
|
91
|
+
assert_equal [alpha,carly], Porolog::Scope[:internal].predicates
|
92
|
+
assert_equal [bravo,delta], Porolog::Scope[:external].predicates
|
93
93
|
end
|
94
94
|
|
95
95
|
end
|
@@ -97,25 +97,25 @@ describe 'Porolog' do
|
|
97
97
|
describe '.reset' do
|
98
98
|
|
99
99
|
it 'should revert to the default scope when reset' do
|
100
|
-
Predicate.scope = :other
|
101
|
-
assert_equal :other, Predicate.scope.name
|
100
|
+
Porolog::Predicate.scope = :other
|
101
|
+
assert_equal :other, Porolog::Predicate.scope.name
|
102
102
|
|
103
|
-
Predicate.reset
|
104
|
-
assert_equal :default, Predicate.scope.name
|
103
|
+
Porolog::Predicate.reset
|
104
|
+
assert_equal :default, Porolog::Predicate.scope.name
|
105
105
|
|
106
|
-
Predicate.scope :temporary
|
106
|
+
Porolog::Predicate.scope :temporary
|
107
107
|
|
108
|
-
alpha = Predicate.new :alpha
|
108
|
+
alpha = Porolog::Predicate.new :alpha
|
109
109
|
|
110
|
-
assert_equal :temporary, Predicate.scope.name
|
111
|
-
assert_equal [alpha], Predicate.scope.predicates
|
110
|
+
assert_equal :temporary, Porolog::Predicate.scope.name
|
111
|
+
assert_equal [alpha], Porolog::Predicate.scope.predicates
|
112
112
|
|
113
|
-
Predicate.reset
|
113
|
+
Porolog::Predicate.reset
|
114
114
|
|
115
|
-
bravo = Predicate.new :bravo
|
115
|
+
bravo = Porolog::Predicate.new :bravo
|
116
116
|
|
117
|
-
assert_equal :default, Predicate.scope.name
|
118
|
-
assert_equal [bravo], Predicate.scope.predicates
|
117
|
+
assert_equal :default, Porolog::Predicate.scope.name
|
118
|
+
assert_equal [bravo], Porolog::Predicate.scope.predicates
|
119
119
|
end
|
120
120
|
|
121
121
|
end
|
@@ -123,9 +123,9 @@ describe 'Porolog' do
|
|
123
123
|
describe '.[]' do
|
124
124
|
|
125
125
|
it 'should return a predicate by name' do
|
126
|
-
alpha = Predicate.new
|
126
|
+
alpha = Porolog::Predicate.new :alpha
|
127
127
|
|
128
|
-
assert_equal alpha, Predicate[:alpha]
|
128
|
+
assert_equal alpha, Porolog::Predicate[:alpha]
|
129
129
|
end
|
130
130
|
|
131
131
|
end
|
@@ -133,19 +133,19 @@ describe 'Porolog' do
|
|
133
133
|
describe '.new' do
|
134
134
|
|
135
135
|
it 'should create a new predicate' do
|
136
|
-
alpha = Predicate.new :alpha
|
136
|
+
alpha = Porolog::Predicate.new :alpha
|
137
137
|
|
138
|
-
assert_instance_of Predicate, alpha
|
138
|
+
assert_instance_of Porolog::Predicate, alpha
|
139
139
|
end
|
140
140
|
|
141
141
|
it 'should not create a predicate if a predicate with the same name already exists in the scope but instead return the existing one' do
|
142
|
-
predicate1 = Predicate.new 'predicate1', :left
|
143
|
-
predicate2 = Predicate.new 'predicate1', :right
|
142
|
+
predicate1 = Porolog::Predicate.new 'predicate1', :left
|
143
|
+
predicate2 = Porolog::Predicate.new 'predicate1', :right
|
144
144
|
|
145
145
|
refute_equal predicate1, predicate2
|
146
146
|
|
147
|
-
Predicate.scope :left
|
148
|
-
predicate3 = Predicate.new :predicate1
|
147
|
+
Porolog::Predicate.scope :left
|
148
|
+
predicate3 = Porolog::Predicate.new :predicate1
|
149
149
|
|
150
150
|
assert_equal predicate1, predicate3
|
151
151
|
end
|
@@ -155,39 +155,39 @@ describe 'Porolog' do
|
|
155
155
|
describe '#initialize' do
|
156
156
|
|
157
157
|
it 'can create predicates in different scopes' do
|
158
|
-
left = Scope.new :left
|
158
|
+
left = Porolog::Scope.new :left
|
159
159
|
#right = Scope.new :right # Not explicitly creating scope :right
|
160
160
|
|
161
|
-
assert_equal left, Predicate.scope(:left)
|
162
|
-
assert_equal :left, Predicate.scope.name
|
161
|
+
assert_equal left, Porolog::Predicate.scope(:left)
|
162
|
+
assert_equal :left, Porolog::Predicate.scope.name
|
163
163
|
|
164
|
-
left_test = Predicate.new 'left_test'
|
164
|
+
left_test = Porolog::Predicate.new 'left_test'
|
165
165
|
|
166
|
-
assert_equal :left, Predicate.scope.name
|
166
|
+
assert_equal :left, Porolog::Predicate.scope.name
|
167
167
|
refute_empty left.predicates
|
168
168
|
assert_includes left.predicates, left_test
|
169
169
|
|
170
|
-
right_test = Predicate.new 'right_test', :right
|
170
|
+
right_test = Porolog::Predicate.new 'right_test', :right
|
171
171
|
|
172
|
-
assert_includes Scope[:right].predicates, right_test
|
173
|
-
assert_equal :left, Predicate.scope.name
|
172
|
+
assert_includes Porolog::Scope[:right].predicates, right_test
|
173
|
+
assert_equal :left, Porolog::Predicate.scope.name
|
174
174
|
|
175
|
-
assert_equal [left_test], Scope[:left ].predicates
|
176
|
-
assert_equal [right_test], Scope[:right].predicates
|
175
|
+
assert_equal [left_test], Porolog::Scope[:left ].predicates
|
176
|
+
assert_equal [right_test], Porolog::Scope[:right].predicates
|
177
177
|
end
|
178
178
|
|
179
179
|
it 'should not create a Predicate named "predicate"' do
|
180
180
|
assert_raises Porolog::Predicate::NameError do
|
181
|
-
predicate :predicate
|
181
|
+
Porolog::predicate :predicate
|
182
182
|
end
|
183
183
|
assert_raises Porolog::Predicate::NameError do
|
184
|
-
predicate 'predicate'
|
184
|
+
Porolog::predicate 'predicate'
|
185
185
|
end
|
186
186
|
assert_raises Porolog::Predicate::NameError do
|
187
|
-
Predicate.new :predicate
|
187
|
+
Porolog::Predicate.new :predicate
|
188
188
|
end
|
189
189
|
assert_raises Porolog::Predicate::NameError do
|
190
|
-
Predicate.new 'predicate'
|
190
|
+
Porolog::Predicate.new 'predicate'
|
191
191
|
end
|
192
192
|
end
|
193
193
|
|
@@ -196,11 +196,11 @@ describe 'Porolog' do
|
|
196
196
|
describe '#call' do
|
197
197
|
|
198
198
|
it 'should create an Arguments when "called"' do
|
199
|
-
p = Predicate.new :p
|
199
|
+
p = Porolog::Predicate.new :p
|
200
200
|
|
201
201
|
arguments = p.(1,2,3)
|
202
202
|
|
203
|
-
assert_instance_of Arguments,
|
203
|
+
assert_instance_of Porolog::Arguments, arguments
|
204
204
|
assert_equal [1,2,3], arguments.arguments
|
205
205
|
assert_equal 'p(1,2,3)', arguments.inspect
|
206
206
|
end
|
@@ -210,11 +210,11 @@ describe 'Porolog' do
|
|
210
210
|
describe '#arguments' do
|
211
211
|
|
212
212
|
it 'should provide a convenience method to create an Arguments' do
|
213
|
-
p = Predicate.new :p
|
213
|
+
p = Porolog::Predicate.new :p
|
214
214
|
|
215
215
|
arguments = p.arguments(1,2,3)
|
216
216
|
|
217
|
-
assert_instance_of Arguments,
|
217
|
+
assert_instance_of Porolog::Arguments, arguments
|
218
218
|
assert_equal [1,2,3], arguments.arguments
|
219
219
|
assert_equal 'p(1,2,3)', arguments.inspect
|
220
220
|
end
|
@@ -224,7 +224,7 @@ describe 'Porolog' do
|
|
224
224
|
describe '#name' do
|
225
225
|
|
226
226
|
it 'should create predicates with a name attribute, which is converted to a Symbol' do
|
227
|
-
test_predicate = Predicate.new('test_predicate_name')
|
227
|
+
test_predicate = Porolog::Predicate.new('test_predicate_name')
|
228
228
|
|
229
229
|
assert_respond_to test_predicate, :name
|
230
230
|
assert_equal :test_predicate_name, test_predicate.name
|
@@ -235,14 +235,14 @@ describe 'Porolog' do
|
|
235
235
|
describe '#rules' do
|
236
236
|
|
237
237
|
it 'should create predicates with an empty set of rules' do
|
238
|
-
test_predicate = Predicate.new('test_predicate_name')
|
238
|
+
test_predicate = Porolog::Predicate.new('test_predicate_name')
|
239
239
|
|
240
240
|
assert_respond_to test_predicate, :rules
|
241
241
|
assert_equal [], test_predicate.rules
|
242
242
|
end
|
243
243
|
|
244
244
|
it 'should add new facts to a predicate' do
|
245
|
-
alpha = Predicate.new 'alpha'
|
245
|
+
alpha = Porolog::Predicate.new 'alpha'
|
246
246
|
|
247
247
|
alpha.('p','q').fact!
|
248
248
|
|
@@ -255,7 +255,7 @@ describe 'Porolog' do
|
|
255
255
|
end
|
256
256
|
|
257
257
|
it 'should add new fallacies to a predicate' do
|
258
|
-
alpha = Predicate.new 'alpha'
|
258
|
+
alpha = Porolog::Predicate.new 'alpha'
|
259
259
|
|
260
260
|
alpha.('p','q').fallacy!
|
261
261
|
|
@@ -272,13 +272,13 @@ describe 'Porolog' do
|
|
272
272
|
describe '#inspect' do
|
273
273
|
|
274
274
|
it 'should return a summary of the predicate' do
|
275
|
-
alpha = Predicate.new 'alpha'
|
275
|
+
alpha = Porolog::Predicate.new 'alpha'
|
276
276
|
|
277
277
|
assert_equal 'alpha:-', alpha.inspect
|
278
278
|
end
|
279
279
|
|
280
280
|
it 'should return a summary of the predicate with rules' do
|
281
|
-
alpha = Predicate.new 'alpha'
|
281
|
+
alpha = Porolog::Predicate.new 'alpha'
|
282
282
|
|
283
283
|
alpha.(:x,:y) << [
|
284
284
|
alpha.(:x,:y),
|
@@ -294,7 +294,7 @@ describe 'Porolog' do
|
|
294
294
|
describe '#<<' do
|
295
295
|
|
296
296
|
it 'should add new rules to a predicate' do
|
297
|
-
alpha = Predicate.new 'alpha'
|
297
|
+
alpha = Porolog::Predicate.new 'alpha'
|
298
298
|
|
299
299
|
alpha.(:P,:Q) << [
|
300
300
|
alpha.(:P,:Q),
|
@@ -327,13 +327,13 @@ describe 'Porolog' do
|
|
327
327
|
describe '#builtin?' do
|
328
328
|
|
329
329
|
it 'should return false for normal predicates' do
|
330
|
-
p = predicate :normal
|
330
|
+
p = Porolog::predicate :normal
|
331
331
|
|
332
332
|
assert_equal false, p.builtin?
|
333
333
|
end
|
334
334
|
|
335
335
|
it 'should return true for builtin predicates' do
|
336
|
-
p = builtin :append
|
336
|
+
p = Porolog::builtin :append
|
337
337
|
|
338
338
|
assert_equal true, p.builtin?
|
339
339
|
end
|
@@ -353,7 +353,7 @@ describe 'Porolog' do
|
|
353
353
|
end
|
354
354
|
end
|
355
355
|
|
356
|
-
let(:predicate1) { Predicate.new :user_defined, builtin: true }
|
356
|
+
let(:predicate1) { Porolog::Predicate.new :user_defined, builtin: true }
|
357
357
|
let(:arguments1) { predicate1.arguments(:m,:n) }
|
358
358
|
let(:goal) { arguments1.goal }
|
359
359
|
|
data/test/porolog/rule_test.rb
CHANGED
@@ -19,21 +19,21 @@ describe 'Porolog' do
|
|
19
19
|
describe '.reset' do
|
20
20
|
|
21
21
|
it 'should delete/unregister all rules' do
|
22
|
-
pred = Predicate.new :pred
|
23
|
-
args = Arguments.new pred, [1,2,3]
|
22
|
+
pred = Porolog::Predicate.new :pred
|
23
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
24
24
|
|
25
|
-
rule1 = Rule.new args, [1,2]
|
26
|
-
rule2 = Rule.new args, [3,4,5]
|
27
|
-
rule3 = Rule.new args, [6]
|
25
|
+
rule1 = Porolog::Rule.new args, [1,2]
|
26
|
+
rule2 = Porolog::Rule.new args, [3,4,5]
|
27
|
+
rule3 = Porolog::Rule.new args, [6]
|
28
28
|
|
29
29
|
assert_equal 'Rule1', rule1.myid
|
30
30
|
assert_equal 'Rule2', rule2.myid
|
31
31
|
assert_equal 'Rule3', rule3.myid
|
32
32
|
|
33
|
-
Rule.reset
|
33
|
+
Porolog::Rule.reset
|
34
34
|
|
35
|
-
rule4 = Rule.new args, [7,8,9,0]
|
36
|
-
rule5 = Rule.new args, [:CUT,false]
|
35
|
+
rule4 = Porolog::Rule.new args, [7,8,9,0]
|
36
|
+
rule5 = Porolog::Rule.new args, [:CUT,false]
|
37
37
|
|
38
38
|
assert_equal 'Rule-999', rule1.myid
|
39
39
|
assert_equal 'Rule-999', rule2.myid
|
@@ -47,10 +47,10 @@ describe 'Porolog' do
|
|
47
47
|
describe '.new' do
|
48
48
|
|
49
49
|
it 'should create a new rule' do
|
50
|
-
pred = Predicate.new :pred
|
51
|
-
args = Arguments.new pred, [1,2,3]
|
50
|
+
pred = Porolog::Predicate.new :pred
|
51
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
52
52
|
defn = [true]
|
53
|
-
rule = Rule.new args, defn
|
53
|
+
rule = Porolog::Rule.new args, defn
|
54
54
|
|
55
55
|
assert_Rule rule, :pred, [1,2,3], [true]
|
56
56
|
end
|
@@ -60,10 +60,10 @@ describe 'Porolog' do
|
|
60
60
|
describe '#initialize' do
|
61
61
|
|
62
62
|
it 'should initialize arguments and definition' do
|
63
|
-
pred = Predicate.new :pred
|
64
|
-
args = Arguments.new pred, [1,2,3]
|
63
|
+
pred = Porolog::Predicate.new :pred
|
64
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
65
65
|
defn = [:CUT,false]
|
66
|
-
rule = Rule.new args, defn
|
66
|
+
rule = Porolog::Rule.new args, defn
|
67
67
|
|
68
68
|
assert_Rule rule, :pred, [1,2,3], [:CUT,false]
|
69
69
|
assert_equal args, rule.arguments
|
@@ -75,21 +75,21 @@ describe 'Porolog' do
|
|
75
75
|
describe '#myid' do
|
76
76
|
|
77
77
|
it 'should show the rule index' do
|
78
|
-
pred = Predicate.new :pred
|
79
|
-
args = Arguments.new pred, [1,2,3]
|
78
|
+
pred = Porolog::Predicate.new :pred
|
79
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
80
80
|
defn = [:CUT,false]
|
81
|
-
rule = Rule.new args, defn
|
81
|
+
rule = Porolog::Rule.new args, defn
|
82
82
|
|
83
83
|
assert_equal 'Rule1', rule.myid
|
84
84
|
end
|
85
85
|
|
86
86
|
it 'should show -999 when deleted/unregistered' do
|
87
|
-
pred = Predicate.new :pred
|
88
|
-
args = Arguments.new pred, [1,2,3]
|
87
|
+
pred = Porolog::Predicate.new :pred
|
88
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
89
89
|
defn = [:CUT,false]
|
90
|
-
rule = Rule.new args, defn
|
90
|
+
rule = Porolog::Rule.new args, defn
|
91
91
|
|
92
|
-
Rule.reset
|
92
|
+
Porolog::Rule.reset
|
93
93
|
|
94
94
|
assert_equal 'Rule-999', rule.myid
|
95
95
|
end
|
@@ -99,10 +99,10 @@ describe 'Porolog' do
|
|
99
99
|
describe '#inspect' do
|
100
100
|
|
101
101
|
it 'should show the predicate, arguments, and definition' do
|
102
|
-
pred = Predicate.new :pred
|
103
|
-
args = Arguments.new pred, [1,2,3]
|
102
|
+
pred = Porolog::Predicate.new :pred
|
103
|
+
args = Porolog::Arguments.new pred, [1,2,3]
|
104
104
|
defn = [:CUT,false]
|
105
|
-
rule = Rule.new args, defn
|
105
|
+
rule = Porolog::Rule.new args, defn
|
106
106
|
|
107
107
|
assert_equal ' pred(1,2,3):- [:CUT, false]', rule.inspect
|
108
108
|
end
|
@@ -112,10 +112,10 @@ describe 'Porolog' do
|
|
112
112
|
describe '#satisfy' do
|
113
113
|
|
114
114
|
it 'should create a subgoal and unify with the goal' do
|
115
|
-
pred = Predicate.new :pred
|
116
|
-
args = Arguments.new pred, [:a,:b,:c]
|
115
|
+
pred = Porolog::Predicate.new :pred
|
116
|
+
args = Porolog::Arguments.new pred, [:a,:b,:c]
|
117
117
|
defn = [true]
|
118
|
-
rule = Rule.new args, defn
|
118
|
+
rule = Porolog::Rule.new args, defn
|
119
119
|
|
120
120
|
goal = new_goal :pred, :x, :y, :z
|
121
121
|
|
@@ -140,16 +140,16 @@ describe 'Porolog' do
|
|
140
140
|
end
|
141
141
|
|
142
142
|
it 'should delete the subgoal even if it does not unify with the goal' do
|
143
|
-
pred = Predicate.new :alpha
|
144
|
-
args = Arguments.new pred, [1,2,3,4]
|
143
|
+
pred = Porolog::Predicate.new :alpha
|
144
|
+
args = Porolog::Arguments.new pred, [1,2,3,4]
|
145
145
|
defn = [true]
|
146
|
-
rule = Rule.new args, defn
|
146
|
+
rule = Porolog::Rule.new args, defn
|
147
147
|
|
148
148
|
goal = new_goal :beta, :x, :y
|
149
149
|
|
150
150
|
subgoal = new_goal :gamma, :x, :y
|
151
151
|
subgoal.expects(:delete!).with().times(1)
|
152
|
-
subgoal_spy = Spy.on(Goal, :new).and_return(subgoal)
|
152
|
+
subgoal_spy = Spy.on(Porolog::Goal, :new).and_return(subgoal)
|
153
153
|
called = false
|
154
154
|
|
155
155
|
rule.satisfy(goal) do |solution_goal|
|
@@ -166,9 +166,9 @@ describe 'Porolog' do
|
|
166
166
|
describe '#satisfy_definition' do
|
167
167
|
|
168
168
|
it 'should call block with subgoal and return true when the definition is true' do
|
169
|
-
pred = Predicate.new :normal
|
170
|
-
args = Arguments.new pred, [12]
|
171
|
-
rule = Rule.new args, true
|
169
|
+
pred = Porolog::Predicate.new :normal
|
170
|
+
args = Porolog::Arguments.new pred, [12]
|
171
|
+
rule = Porolog::Rule.new args, true
|
172
172
|
|
173
173
|
check = 56
|
174
174
|
result = rule.satisfy_definition(12, 34) do |subgoal|
|
@@ -180,9 +180,9 @@ describe 'Porolog' do
|
|
180
180
|
end
|
181
181
|
|
182
182
|
it 'should not call block but return false when the definition is false' do
|
183
|
-
pred = Predicate.new :negative
|
184
|
-
args = Arguments.new pred, [12]
|
185
|
-
rule = Rule.new args, false
|
183
|
+
pred = Porolog::Predicate.new :negative
|
184
|
+
args = Porolog::Arguments.new pred, [12]
|
185
|
+
rule = Porolog::Rule.new args, false
|
186
186
|
|
187
187
|
check = 56
|
188
188
|
result = rule.satisfy_definition(12, 34) do |subgoal|
|
@@ -195,13 +195,13 @@ describe 'Porolog' do
|
|
195
195
|
end
|
196
196
|
|
197
197
|
it 'should call block with subgoal when the definition is an Array' do
|
198
|
-
pred1 = Predicate.new :success
|
199
|
-
args1 = Arguments.new pred1, [:any]
|
200
|
-
Rule.new args1, true
|
198
|
+
pred1 = Porolog::Predicate.new :success
|
199
|
+
args1 = Porolog::Arguments.new pred1, [:any]
|
200
|
+
Porolog::Rule.new args1, true
|
201
201
|
|
202
|
-
pred2 = Predicate.new :conjunction
|
203
|
-
args2 = Arguments.new pred2, [12]
|
204
|
-
rule2 = Rule.new args2, [true,true,true]
|
202
|
+
pred2 = Porolog::Predicate.new :conjunction
|
203
|
+
args2 = Porolog::Arguments.new pred2, [12]
|
204
|
+
rule2 = Porolog::Rule.new args2, [true,true,true]
|
205
205
|
|
206
206
|
check = 56
|
207
207
|
result = rule2.satisfy_definition(args1.goal, args2.goal) do |subgoal|
|
@@ -212,11 +212,11 @@ describe 'Porolog' do
|
|
212
212
|
end
|
213
213
|
|
214
214
|
it 'should raise an exception when the definition is unknown' do
|
215
|
-
predicate :strange
|
215
|
+
Porolog::predicate :strange
|
216
216
|
|
217
217
|
strange(12) << 3.4
|
218
218
|
|
219
|
-
exception = assert_raises Rule::DefinitionError do
|
219
|
+
exception = assert_raises Porolog::Rule::DefinitionError do
|
220
220
|
strange(:X).solve
|
221
221
|
end
|
222
222
|
assert_equal 'UNEXPECTED TYPE OF DEFINITION: 3.4 (Float)', exception.message
|
@@ -226,15 +226,15 @@ describe 'Porolog' do
|
|
226
226
|
|
227
227
|
describe '#satisfy_conjunction' do
|
228
228
|
|
229
|
-
let(:pred1 ) { Predicate.new :parent }
|
230
|
-
let(:pred2 ) { Predicate.new :child }
|
231
|
-
let(:args1 ) { Arguments.new pred1, [1,2,3,4] }
|
232
|
-
let(:args2 ) { Arguments.new pred2, [:x,:y] }
|
233
|
-
let(:goal ) { Goal.new args1, nil }
|
234
|
-
let(:subgoal) { Goal.new args2, goal }
|
229
|
+
let(:pred1 ) { Porolog::Predicate.new :parent }
|
230
|
+
let(:pred2 ) { Porolog::Predicate.new :child }
|
231
|
+
let(:args1 ) { Porolog::Arguments.new pred1, [1,2,3,4] }
|
232
|
+
let(:args2 ) { Porolog::Arguments.new pred2, [:x,:y] }
|
233
|
+
let(:goal ) { Porolog::Goal.new args1, nil }
|
234
|
+
let(:subgoal) { Porolog::Goal.new args2, goal }
|
235
235
|
|
236
236
|
it 'should handle CUT expression' do
|
237
|
-
rule = Rule.new args1, [:CUT,true]
|
237
|
+
rule = Porolog::Rule.new args1, [:CUT,true]
|
238
238
|
|
239
239
|
goal.expects(:terminate!).with().times(1)
|
240
240
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -252,7 +252,7 @@ describe 'Porolog' do
|
|
252
252
|
end
|
253
253
|
|
254
254
|
it 'should handle CUT expression at the end of a conjunction' do
|
255
|
-
rule = Rule.new args1, [:CUT]
|
255
|
+
rule = Porolog::Rule.new args1, [:CUT]
|
256
256
|
|
257
257
|
goal.expects(:terminate!).with().times(1)
|
258
258
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -269,7 +269,7 @@ describe 'Porolog' do
|
|
269
269
|
end
|
270
270
|
|
271
271
|
it 'should handle true expression' do
|
272
|
-
rule = Rule.new args1, [true]
|
272
|
+
rule = Porolog::Rule.new args1, [true]
|
273
273
|
|
274
274
|
goal.expects(:terminate!).with().times(0)
|
275
275
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -286,7 +286,7 @@ describe 'Porolog' do
|
|
286
286
|
end
|
287
287
|
|
288
288
|
it 'should handle false expression' do
|
289
|
-
rule = Rule.new args1, [false]
|
289
|
+
rule = Porolog::Rule.new args1, [false]
|
290
290
|
|
291
291
|
goal.expects(:terminate!).with().times(0)
|
292
292
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -305,7 +305,7 @@ describe 'Porolog' do
|
|
305
305
|
end
|
306
306
|
|
307
307
|
it 'should handle nil expression' do
|
308
|
-
rule = Rule.new args1, []
|
308
|
+
rule = Porolog::Rule.new args1, []
|
309
309
|
|
310
310
|
goal.expects(:terminate!).with().times(0)
|
311
311
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -325,7 +325,7 @@ describe 'Porolog' do
|
|
325
325
|
|
326
326
|
it 'should evaluate conjunctions until a fail' do
|
327
327
|
conjunction = [true, true, true, false, true, :CUT, true]
|
328
|
-
rule = Rule.new args1, conjunction
|
328
|
+
rule = Porolog::Rule.new args1, conjunction
|
329
329
|
|
330
330
|
goal.expects(:terminate!).with().times(0)
|
331
331
|
rule_spy = Spy.on(rule, :satisfy_conjunction).and_call_through
|
@@ -347,18 +347,18 @@ describe 'Porolog' do
|
|
347
347
|
end
|
348
348
|
|
349
349
|
it 'should unify and instantiate variables with a subgoal and satisfy the subgoal and uninstantiate the instantiations' do
|
350
|
-
goal = Goal.new args1, nil
|
351
|
-
subgoal = Goal.new args2, goal
|
350
|
+
goal = Porolog::Goal.new args1, nil
|
351
|
+
subgoal = Porolog::Goal.new args2, goal
|
352
352
|
|
353
|
-
predicate :gamma
|
353
|
+
Porolog::predicate :gamma
|
354
354
|
|
355
355
|
gamma(8,5).fact!
|
356
356
|
|
357
|
-
rule = Rule.new args1, [gamma(:j,:k)]
|
357
|
+
rule = Porolog::Rule.new args1, [gamma(:j,:k)]
|
358
358
|
|
359
359
|
subsubgoal = new_goal :gamma, :y, :z
|
360
|
-
subsubgoal_spy = Spy.on(Goal, :new).and_return do |*args|
|
361
|
-
Spy.off(Goal, :new)
|
360
|
+
subsubgoal_spy = Spy.on(Porolog::Goal, :new).and_return do |*args|
|
361
|
+
Spy.off(Porolog::Goal, :new)
|
362
362
|
subsubgoal
|
363
363
|
end
|
364
364
|
|
@@ -425,8 +425,8 @@ describe 'Porolog' do
|
|
425
425
|
|
426
426
|
it 'should not unify and not instantiate variables with a subgoal nor satisfy the subgoal when it cannot be unified' do
|
427
427
|
# -- Create Goals --
|
428
|
-
goal = Goal.new args1, nil
|
429
|
-
subgoal = Goal.new args2, goal
|
428
|
+
goal = Porolog::Goal.new args1, nil
|
429
|
+
subgoal = Porolog::Goal.new args2, goal
|
430
430
|
subgoal.instantiate :y, 7
|
431
431
|
|
432
432
|
# -- Create goal for satisfy_conjunction() --
|
@@ -454,15 +454,15 @@ describe 'Porolog' do
|
|
454
454
|
].join("\n")
|
455
455
|
|
456
456
|
# -- Define Predicate --
|
457
|
-
predicate :gamma
|
457
|
+
Porolog::predicate :gamma
|
458
458
|
|
459
459
|
gamma(8,5).fact!
|
460
460
|
|
461
|
-
rule = Rule.new args1, [gamma(:j,:k)]
|
461
|
+
rule = Porolog::Rule.new args1, [gamma(:j,:k)]
|
462
462
|
|
463
463
|
# -- Prepare Checks for satisfy_conjunction() --
|
464
|
-
subsubgoal_spy = Spy.on(Goal, :new).and_return do |*args|
|
465
|
-
Spy.off(Goal, :new)
|
464
|
+
subsubgoal_spy = Spy.on(Porolog::Goal, :new).and_return do |*args|
|
465
|
+
Spy.off(Porolog::Goal, :new)
|
466
466
|
subsubgoal
|
467
467
|
end
|
468
468
|
|