porolog 0.0.4 → 1.0.0

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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +30 -5
  3. data/Rakefile +7 -2
  4. data/bin/porolog +58 -1
  5. data/coverage/badge.svg +1 -1
  6. data/coverage/index.html +76733 -2638
  7. data/doc/Array.html +1066 -0
  8. data/doc/Object.html +674 -0
  9. data/doc/Porolog.html +4153 -74
  10. data/doc/Symbol.html +501 -0
  11. data/doc/_index.html +280 -6
  12. data/doc/class_list.html +1 -1
  13. data/doc/file.README.html +34 -39
  14. data/doc/index.html +34 -39
  15. data/doc/method_list.html +1337 -57
  16. data/doc/top-level-namespace.html +4 -2
  17. data/lib/porolog.rb +1144 -4
  18. data/lib/porolog/arguments.rb +28 -24
  19. data/lib/porolog/core_ext.rb +188 -0
  20. data/lib/porolog/error.rb +9 -0
  21. data/lib/porolog/goal.rb +357 -0
  22. data/lib/porolog/instantiation.rb +346 -0
  23. data/lib/porolog/predicate.rb +74 -31
  24. data/lib/porolog/predicate/builtin.rb +825 -0
  25. data/lib/porolog/rule.rb +162 -0
  26. data/lib/porolog/scope.rb +4 -4
  27. data/lib/porolog/tail.rb +57 -0
  28. data/lib/porolog/value.rb +105 -0
  29. data/lib/porolog/variable.rb +325 -0
  30. data/test/porolog/arguments_test.rb +244 -195
  31. data/test/porolog/core_ext_test.rb +290 -0
  32. data/test/porolog/goal_test.rb +891 -0
  33. data/test/porolog/instantiation_test.rb +910 -0
  34. data/test/porolog/porolog_test.rb +2376 -13
  35. data/test/porolog/predicate/builtin_test.rb +1340 -0
  36. data/test/porolog/predicate_test.rb +84 -30
  37. data/test/porolog/rule_test.rb +527 -0
  38. data/test/porolog/scope_test.rb +0 -2
  39. data/test/porolog/tail_test.rb +127 -0
  40. data/test/porolog/value_test.rb +315 -0
  41. data/test/porolog/variable_test.rb +1614 -0
  42. data/test/samples_test.rb +277 -0
  43. data/test/test_helper.rb +115 -0
  44. metadata +34 -7
@@ -23,8 +23,6 @@ describe 'Porolog' do
23
23
  end
24
24
 
25
25
  it 'should allow predicates with the same name to coexist in different scopes' do
26
- skip 'until Rule added'
27
-
28
26
  prime = prime1 = Predicate.new :prime, :first
29
27
 
30
28
  prime.(2).fact!
@@ -0,0 +1,127 @@
1
+ #
2
+ # test/porolog/tail_test.rb - Test Suite for Porolog::Tail
3
+ #
4
+ # Luis Esteban 2 May 2018
5
+ # created
6
+ #
7
+
8
+ require_relative '../test_helper'
9
+
10
+ describe 'Porolog' do
11
+
12
+ before(:all) do
13
+ reset
14
+ end
15
+
16
+ describe 'Tail' do
17
+
18
+ describe '.new' do
19
+
20
+ it 'creates an unknown tail when no value is provided' do
21
+ tail = Tail.new
22
+
23
+ assert_Tail tail, '*...'
24
+ assert_equal UNKNOWN_TAIL, tail.value
25
+ end
26
+
27
+ it 'creates a tail with a value' do
28
+ tail = Tail.new [2,3,5,7,11,13]
29
+
30
+ assert_Tail tail, '*[2, 3, 5, 7, 11, 13]'
31
+ assert_equal [2,3,5,7,11,13], tail.value
32
+ end
33
+
34
+ it 'creates a tail with a variable' do
35
+ tail = Tail.new :x
36
+
37
+ assert_Tail tail, '*:x'
38
+ assert_equal :x, tail.value
39
+ end
40
+
41
+ end
42
+
43
+ describe '#value' do
44
+
45
+ let(:object) { Object.new }
46
+
47
+ before do
48
+ def object.inspect
49
+ super.gsub(/Object:0x[[:xdigit:]]+/,'Object:0xXXXXXX')
50
+ end
51
+ end
52
+
53
+ it 'returns its value' do
54
+ tail = Tail.new object
55
+
56
+ assert_Tail tail, '*#<Object:0xXXXXXX>'
57
+ assert_equal object, tail.value
58
+ end
59
+
60
+ end
61
+
62
+ describe '#inspect' do
63
+
64
+ it 'returns a string showing a splat operation is implied' do
65
+ tail = Tail.new [2,3,5,7,11,13]
66
+
67
+ assert_equal '*[2, 3, 5, 7, 11, 13]', tail.inspect
68
+ end
69
+
70
+ end
71
+
72
+ describe '#variables' do
73
+
74
+ it 'returns an empty Array when it was created without arguments' do
75
+ tail = Tail.new
76
+
77
+ assert_equal [], tail.variables
78
+ end
79
+
80
+ it 'returns an empty Array when it was created with an Array of atomics' do
81
+ tail = Tail.new [2,3,5,7,11,13]
82
+
83
+ assert_equal [], tail.variables
84
+ end
85
+
86
+ it 'returns an Array of Symbols when it was created with a Symbol' do
87
+ tail = Tail.new :t
88
+
89
+ assert_equal [:t], tail.variables
90
+ end
91
+
92
+ it 'returns an Array of Symbols when it was created with an Array with embedded Symbols' do
93
+ tail = Tail.new [2,3,:x,7,[:y,[:z]],13]
94
+
95
+ assert_equal [:x,:y,:z], tail.variables
96
+ end
97
+
98
+ end
99
+
100
+ describe '#==' do
101
+
102
+ it 'returns true for unknown tails' do
103
+ tail1 = Tail.new
104
+ tail2 = Tail.new
105
+
106
+ assert tail1 == tail2, name
107
+ end
108
+
109
+ it 'returns false for different symbols' do
110
+ tail1 = Tail.new :t
111
+ tail2 = Tail.new :x
112
+
113
+ refute tail1 == tail2, name
114
+ end
115
+
116
+ it 'returns true for equal values' do
117
+ tail1 = Tail.new 12.34
118
+ tail2 = Tail.new 12.34
119
+
120
+ assert tail1 == tail2, name
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -0,0 +1,315 @@
1
+ #
2
+ # test/porolog/value_test.rb - Test Suite for Porolog::Value
3
+ #
4
+ # Luis Esteban 2 May 2018
5
+ # created
6
+ #
7
+
8
+ require_relative '../test_helper'
9
+
10
+ describe 'Porolog' do
11
+
12
+ before(:all) do
13
+ reset
14
+ end
15
+
16
+ describe 'Value' do
17
+
18
+ let(:goal) { new_goal :generic, [:m, :n] }
19
+ let(:v) { Value.new 456.123, goal }
20
+
21
+ describe '.new' do
22
+
23
+ it 'should create a new value' do
24
+ assert_instance_of Value, v
25
+ end
26
+
27
+ end
28
+
29
+ describe '#initialize' do
30
+
31
+ it 'should initialize value and goal' do
32
+ assert_equal 456.123, v.value
33
+ assert_equal goal, v.goal
34
+ end
35
+
36
+ it 'should raise an error when a goal is not provided' do
37
+ assert_raises Value::GoalError do
38
+ Value.new 456.789, 'goal'
39
+ end
40
+ end
41
+
42
+ it 'should copy the value of another Value' do
43
+ other = Value.new 123.456, goal
44
+ v = Value.new other, goal
45
+
46
+ refute_instance_of Value, v.value
47
+ assert_equal 123.456, v.value
48
+ end
49
+
50
+ end
51
+
52
+ describe '#inspect' do
53
+
54
+ it 'should show the goal and the value' do
55
+ assert_equal 'Goal1.456.123', v.inspect
56
+ end
57
+
58
+ end
59
+
60
+ describe '#instantiations' do
61
+
62
+ it 'should return no instantiations' do
63
+ assert_equal [], v.instantiations
64
+ end
65
+
66
+ end
67
+
68
+ describe '#inspect_with_instantiations' do
69
+
70
+ let(:depth) { rand(0..10) }
71
+ let(:index) { rand(0..10) }
72
+
73
+ it 'should should show the goal and value' do
74
+ assert_equal 'Goal1.456.123', v.inspect_with_instantiations
75
+ end
76
+
77
+ it 'should should show the goal and indexed value' do
78
+ assert_equal "Goal1.456.123[#{index}]", v.inspect_with_instantiations(nil, 0, index)
79
+ end
80
+
81
+ it 'should should show the goal and value indentation showing instantiation depth' do
82
+ assert_equal "#{' ' * depth}Goal1.456.123", v.inspect_with_instantiations(nil, depth)
83
+ end
84
+
85
+ it 'should should show the goal and value indentation showing instantiation depth and indexed value' do
86
+ assert_equal "#{' ' * depth}Goal1.456.123[#{index}]", v.inspect_with_instantiations(nil, depth, index)
87
+ end
88
+
89
+ end
90
+
91
+ describe '#remove' do
92
+
93
+ let(:predicate1) { Predicate.new :removal }
94
+ let(:arguments1) { predicate1.arguments(:m,:n) }
95
+ let(:goal1) { arguments1.goal }
96
+ let(:goal2) { arguments1.goal }
97
+ let(:goal3) { arguments1.goal }
98
+ let(:goal4) { arguments1.goal }
99
+
100
+ let(:variable1) { Variable.new :x, goal1 }
101
+ let(:variable2) { Variable.new :y, goal2 }
102
+ let(:variable3) { Variable.new :z, goal3 }
103
+ let(:variable4) { Variable.new :a, goal1 }
104
+ let(:value1) { Value.new 'word', goal2 }
105
+ let(:value2) { Value.new 'draw', goal4 }
106
+
107
+ before do
108
+ reset
109
+
110
+ variable1.instantiate variable2
111
+ variable1.instantiate variable3
112
+ variable2.instantiate value1
113
+ variable4.instantiate value2
114
+
115
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word', a: 'draw' }, [
116
+ 'Goal1.:m',
117
+ 'Goal1.:n',
118
+ 'Goal1.:x',
119
+ ' Goal2.:y',
120
+ ' Goal2."word"',
121
+ ' Goal3.:z',
122
+ 'Goal1.:a',
123
+ ' Goal4."draw"',
124
+ ].join("\n")
125
+
126
+ assert_equal 2, variable1.instantiations.size
127
+ assert_equal 2, variable2.instantiations.size
128
+ assert_equal 1, variable3.instantiations.size
129
+ assert_equal 1, variable4.instantiations.size
130
+
131
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
132
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
133
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
134
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
135
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
136
+ assert_Instantiation variable4.instantiations[0], variable4, value2, nil, nil
137
+
138
+ assert_equal 'word', variable1.value
139
+ assert_equal 'word', variable2.value
140
+ assert_equal 'word', variable3.value
141
+ assert_equal 'draw', variable4.value
142
+ end
143
+
144
+ it 'should remove all instantiations case 1: remove value1' do
145
+
146
+ value1.remove
147
+
148
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil, a: 'draw' }, [
149
+ 'Goal1.:m',
150
+ 'Goal1.:n',
151
+ 'Goal1.:x',
152
+ ' Goal2.:y',
153
+ ' Goal3.:z',
154
+ 'Goal1.:a',
155
+ ' Goal4."draw"',
156
+ ].join("\n")
157
+
158
+ assert_equal 2, variable1.instantiations.size
159
+ assert_equal 1, variable2.instantiations.size
160
+ assert_equal 1, variable3.instantiations.size
161
+ assert_equal 1, variable4.instantiations.size
162
+
163
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
164
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
165
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
166
+ assert_nil variable2.instantiations[1]
167
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
168
+ assert_Instantiation variable4.instantiations[0], variable4, value2, nil, nil
169
+
170
+ assert_equal variable1, variable1.value
171
+ assert_equal variable2, variable2.value
172
+ assert_equal variable3, variable3.value
173
+ assert_equal 'draw', variable4.value
174
+ end
175
+
176
+ it 'should remove all instantiations case 2: remove value2' do
177
+
178
+ value2.remove
179
+
180
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word', a: nil }, [
181
+ 'Goal1.:m',
182
+ 'Goal1.:n',
183
+ 'Goal1.:x',
184
+ ' Goal2.:y',
185
+ ' Goal2."word"',
186
+ ' Goal3.:z',
187
+ 'Goal1.:a',
188
+ ].join("\n")
189
+
190
+ assert_equal 2, variable1.instantiations.size
191
+ assert_equal 2, variable2.instantiations.size
192
+ assert_equal 1, variable3.instantiations.size
193
+ assert_equal 0, variable4.instantiations.size
194
+
195
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
196
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
197
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
198
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
199
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
200
+ assert_nil variable4.instantiations[0]
201
+
202
+ assert_equal 'word', variable1.value
203
+ assert_equal 'word', variable2.value
204
+ assert_equal 'word', variable3.value
205
+ assert_equal variable4, variable4.value
206
+ end
207
+
208
+ end
209
+
210
+ describe '#method_missing' do
211
+
212
+ it 'should pass any unexpected methods to the value' do
213
+ assert_equal 456, v.to_i
214
+ assert_equal 466.123, v + 10
215
+ end
216
+
217
+ end
218
+
219
+ describe '#respond_to?' do
220
+
221
+ it 'should respond to Value methods and methods of the actual value' do
222
+ assert_equal true, v.respond_to?(:instantiations)
223
+ assert_equal true, v.respond_to?(:inspect_with_instantiations)
224
+ assert_equal true, v.respond_to?(:value)
225
+ assert_equal true, v.respond_to?(:to_i)
226
+ assert_equal true, v.respond_to?(:+)
227
+ end
228
+
229
+ end
230
+
231
+ describe '#value' do
232
+
233
+ it 'should take any number of arguments to be compatible with the value method of a variable' do
234
+ assert_equal 456.123, v.value
235
+ assert_equal 456.123, v.value(nil)
236
+ assert_equal 456.123, v.value(1,2,3)
237
+ assert_equal 456.123, v.value([])
238
+ end
239
+
240
+ end
241
+
242
+ describe '#type' do
243
+
244
+ let(:float) { Value.new 456.789, goal }
245
+ let(:integer) { Value.new 456, goal }
246
+ let(:string) { Value.new 'average', goal }
247
+ let(:array) { Value.new [1,2,3], goal }
248
+ let(:object) { Value.new Object.new, goal }
249
+
250
+ it 'should return atomic for Float' do
251
+ assert_equal :atomic, float.type
252
+ end
253
+
254
+ it 'should return atomic for Integer' do
255
+ assert_equal :atomic, integer.type
256
+ end
257
+
258
+ it 'should return atomic for String' do
259
+ assert_equal :atomic, string.type
260
+ end
261
+
262
+ it 'should return atomic for Array' do
263
+ assert_equal :array, array.type
264
+ end
265
+
266
+ it 'should return atomic for Object' do
267
+ assert_equal :atomic, object.type
268
+ end
269
+
270
+ end
271
+
272
+ describe '#==' do
273
+
274
+ it 'should return false for Values of different types' do
275
+ v1 = Value.new 456.789, goal
276
+ v2 = Value.new 'average', goal
277
+
278
+ refute v1 == v2, "#{name}: #{v1.value.inspect} == #{v2.value.inspect}"
279
+ end
280
+
281
+ it 'should return false for Values of the same type but different values' do
282
+ v1 = Value.new 456.789, goal
283
+ v2 = Value.new 123.456, goal
284
+
285
+ refute v1 == v2, "#{name}: #{v1.value.inspect} == #{v2.value.inspect}"
286
+ end
287
+
288
+ it 'should return true for Values of the same type and the same value' do
289
+ v1 = Value.new 456.789, goal
290
+ v2 = Value.new 456.789, goal
291
+
292
+ assert v1 == v2, "#{name}: #{v1.value.inspect} == #{v2.value.inspect}"
293
+ end
294
+
295
+ end
296
+
297
+ describe '#variables' do
298
+
299
+ it 'should return an empty Array for a Float value' do
300
+ float = Value.new 456.789, goal
301
+
302
+ assert_equal [], float.variables
303
+ end
304
+
305
+ it 'should return an Array of Symbols for embedded Variables' do
306
+ array = Value.new [1, :b, 3, ['four', :e, 6], [[:g, 8]]], goal
307
+
308
+ assert_equal [:b, :e, :g], array.variables
309
+ end
310
+
311
+ end
312
+
313
+ end
314
+
315
+ end