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
@@ -0,0 +1,910 @@
1
+ #
2
+ # test/porolog/instantiation_test.rb - Test Suite for Porolog::Instantiation
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 'Instantiation' do
17
+
18
+ let(:goal1) { new_goal :generic, :m, :n }
19
+ let(:goal2) { new_goal :specifc, :p, :q }
20
+ let(:goal3) { new_goal :other, :x, :y }
21
+ let(:variable1) { goal1.variable(:x) }
22
+ let(:variable2) { goal2.variable(:y) }
23
+ let(:variable3) { goal3.variable(:z) }
24
+ let(:variable4) { goal3.variable(:v) }
25
+ let(:instantiation) { Instantiation.new variable1, nil, variable2, nil }
26
+
27
+ describe '.reset' do
28
+
29
+ it 'should delete/unregister all instantiations' do
30
+ assert_equal 0, Instantiation.instantiations.size
31
+
32
+ Instantiation.new variable1, nil, variable2, nil
33
+ Instantiation.new variable1, nil, variable2, 2
34
+ Instantiation.new variable1, nil, variable3, nil
35
+ Instantiation.new variable1, 1, variable3, nil
36
+ Instantiation.new variable1, nil, variable2, :head
37
+
38
+ assert_equal 5, Instantiation.instantiations.size
39
+
40
+ assert Instantiation.reset, name
41
+
42
+ assert_equal 0, Instantiation.instantiations.size
43
+ end
44
+
45
+ end
46
+
47
+ describe '.instantiations' do
48
+
49
+ it 'should be empty to start with' do
50
+ assert_equal({}, Instantiation.instantiations)
51
+ end
52
+
53
+ it 'should return all registered instantiations' do
54
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
55
+ instantiation2 = Instantiation.new variable1, nil, variable2, 2
56
+ instantiation3 = Instantiation.new variable1, nil, variable3, nil
57
+ instantiation4 = Instantiation.new variable1, 1, variable3, nil
58
+ instantiation5 = Instantiation.new variable2, nil, variable3, nil
59
+
60
+ assert_equal(
61
+ {
62
+ instantiation1.signature => instantiation1,
63
+ instantiation2.signature => instantiation2,
64
+ instantiation3.signature => instantiation3,
65
+ instantiation4.signature => instantiation4,
66
+ instantiation5.signature => instantiation5,
67
+ },
68
+ Instantiation.instantiations
69
+ )
70
+ end
71
+
72
+ end
73
+
74
+ describe '.new' do
75
+
76
+ it 'should create a new instantiation' do
77
+ instantiation = Instantiation.new variable1, nil, variable2, nil
78
+
79
+ assert_instance_of Instantiation, instantiation
80
+ end
81
+
82
+ it 'should not create duplicate instantiations' do
83
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
84
+ instantiation2 = Instantiation.new variable1, nil, variable2, 2
85
+ instantiation3 = Instantiation.new variable1, nil, variable3, nil
86
+ instantiation4 = Instantiation.new variable1, 1, variable3, nil
87
+ instantiation5 = Instantiation.new variable1, nil, variable2, nil
88
+
89
+ assert_equal instantiation1, instantiation5
90
+ assert_equal(
91
+ {
92
+ [variable1, nil, variable2, nil] => instantiation1,
93
+ [variable1, nil, variable2, 2 ] => instantiation2,
94
+ [variable1, 1, variable3, nil] => instantiation4,
95
+ [variable1, nil, variable3, nil] => instantiation3,
96
+ },
97
+ Instantiation.instantiations
98
+ )
99
+ end
100
+
101
+ end
102
+
103
+ describe '#initialize' do
104
+
105
+ it 'should initialize variable1, variable2, index1, and index2' do
106
+ instantiation = Instantiation.new variable1, nil, variable2, nil
107
+
108
+ assert_Instantiation instantiation, variable1, variable2, nil, nil
109
+ end
110
+
111
+ it 'should append itself to its variables' do
112
+ instantiation = Instantiation.new variable1, nil, variable2, nil
113
+
114
+ assert_equal [instantiation], variable1.instantiations
115
+ assert_equal [instantiation], variable2.instantiations
116
+ end
117
+
118
+ it 'should register the instantiation' do
119
+ instantiation = Instantiation.new variable1, nil, variable2, nil
120
+
121
+ assert_equal [instantiation], Instantiation.instantiations.values
122
+ assert_equal [[variable1, nil, variable2, nil]], Instantiation.instantiations.keys
123
+ assert_equal(
124
+ {
125
+ [variable1, nil, variable2, nil] => instantiation
126
+ },
127
+ Instantiation.instantiations
128
+ )
129
+ end
130
+
131
+ end
132
+
133
+ describe '#signature' do
134
+
135
+ it 'should return the signature of the instantiation' do
136
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
137
+ instantiation2 = Instantiation.new variable1, 4, variable2, :head
138
+
139
+ assert_equal [variable1, nil, variable2, nil ], instantiation1.signature
140
+ assert_equal [variable1, 4, variable2, :head], instantiation2.signature
141
+ end
142
+
143
+ end
144
+
145
+ describe '#inspect' do
146
+
147
+ it 'should show its variables and their indices' do
148
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
149
+ instantiation2 = Instantiation.new variable1, 4, variable2, :head
150
+
151
+ assert_equal 'Goal1.:x = Goal2.:y', instantiation1.inspect
152
+ assert_equal 'Goal1.:x[4] = Goal2.:y[:head]', instantiation2.inspect
153
+ end
154
+
155
+ end
156
+
157
+ describe '#remove' do
158
+
159
+ it 'should remove itself from instantiations of both variables' do
160
+ instantiation = Instantiation.new variable1, nil, variable2, nil
161
+
162
+ assert_equal [instantiation], variable1.instantiations
163
+ assert_equal [instantiation], variable2.instantiations
164
+
165
+ instantiation.remove
166
+
167
+ assert_equal [], variable1.instantiations
168
+ assert_equal [], variable2.instantiations
169
+ end
170
+
171
+ it 'should be marked as deleted' do
172
+ instantiation = Instantiation.new variable1, nil, variable2, nil
173
+
174
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
175
+
176
+ instantiation.remove
177
+
178
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
179
+ end
180
+
181
+ it 'should unregister itself' do
182
+ assert_equal [], Instantiation.instantiations.values
183
+
184
+ instantiation = Instantiation.new variable1, nil, variable2, nil
185
+
186
+ assert_equal [instantiation], Instantiation.instantiations.values
187
+ assert_equal [[variable1, nil, variable2, nil]], Instantiation.instantiations.keys
188
+ assert_equal(
189
+ {
190
+ [variable1, nil, variable2, nil] => instantiation
191
+ },
192
+ Instantiation.instantiations
193
+ )
194
+
195
+ instantiation.remove
196
+
197
+ assert_equal [], Instantiation.instantiations.values
198
+ assert_equal [], Instantiation.instantiations.keys
199
+ assert_equal(
200
+ {
201
+ },
202
+ Instantiation.instantiations
203
+ )
204
+ end
205
+
206
+ end
207
+
208
+ describe '#other_goal_to' do
209
+
210
+ it 'should return the goal of the other variable' do
211
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
212
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
213
+
214
+ assert_equal goal2, instantiation1.other_goal_to(variable1)
215
+ assert_equal goal1, instantiation1.other_goal_to(variable2)
216
+ assert_nil instantiation1.other_goal_to(variable3)
217
+ assert_nil instantiation2.other_goal_to(variable1)
218
+ assert_equal goal3, instantiation2.other_goal_to(variable2)
219
+ assert_equal goal2, instantiation2.other_goal_to(variable3)
220
+ end
221
+
222
+ end
223
+
224
+ describe '#goals' do
225
+
226
+ it 'should return the goals' do
227
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
228
+ instantiation2 = Instantiation.new variable1, 4, variable3, :head
229
+
230
+ assert_equal [goal1, goal2], instantiation1.goals
231
+ assert_equal [goal1, goal3], instantiation2.goals
232
+ end
233
+
234
+ end
235
+
236
+ describe '#variables' do
237
+
238
+ it 'should return both variables' do
239
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
240
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
241
+
242
+ assert_equal [variable1,variable2], instantiation1.variables
243
+ assert_equal [variable2,variable3], instantiation2.variables
244
+ end
245
+
246
+ end
247
+
248
+ describe '#values' do
249
+
250
+ it 'should return all values instantiated to its variables' do
251
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
252
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
253
+
254
+ assert_equal [], instantiation1.values
255
+ assert_equal [], instantiation2.values
256
+
257
+ variable3.instantiate Value.new('word',goal3)
258
+
259
+ assert_equal ['word'], instantiation1.values
260
+ assert_equal ['word'], instantiation2.values
261
+ end
262
+
263
+ it 'should return all indexed values instantiated to its variables' do
264
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
265
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
266
+
267
+ assert_equal [], instantiation1.values
268
+ assert_equal [], instantiation2.values
269
+
270
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
271
+
272
+ assert_equal 7, variable1.value
273
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
274
+ assert_equal 11, variable3.value
275
+
276
+ assert_equal [7], instantiation1.values
277
+ assert_equal [11], instantiation2.values
278
+
279
+ variable2.uninstantiate goal2
280
+
281
+ assert_equal variable1, variable1.value
282
+ assert_equal variable2, variable2.value
283
+ assert_equal variable3, variable3.value
284
+
285
+ assert_equal [], instantiation1.values
286
+ assert_equal [], instantiation2.values
287
+
288
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
289
+ variable2.instantiate Value.new(words, goal2)
290
+
291
+ assert_equal 'seven', variable1.value
292
+ assert_equal words, variable2.value
293
+ assert_equal 'eleven', variable3.value
294
+
295
+ assert_equal ['seven'], instantiation1.values
296
+ assert_equal ['eleven'], instantiation2.values
297
+ end
298
+
299
+ end
300
+
301
+ describe '#value_indexed' do
302
+
303
+ it 'should return unknown tail when value has an unknown tail and the index is greater than the fixed portion of the list' do
304
+ assert_equal UNKNOWN_TAIL, instantiation.value_indexed([2,3,5,7,UNKNOWN_TAIL],99)
305
+ end
306
+
307
+ it 'should return indexed element when value has an unknown tail and the index is less than the fixed portion of the list' do
308
+ assert_equal 5, instantiation.value_indexed([2,3,5,7,UNKNOWN_TAIL],2)
309
+ end
310
+
311
+ it 'should return the component when value is a Variable and the index is a Symbol' do
312
+ value = goal1.value([2,3,5,7,UNKNOWN_TAIL])
313
+
314
+ assert_equal 2, instantiation.value_indexed(value,:head)
315
+ assert_equal [3,5,7,UNKNOWN_TAIL], instantiation.value_indexed(value,:tail)
316
+ end
317
+
318
+ it 'should return nil when value is nil' do
319
+ assert_nil instantiation.value_indexed(nil,nil)
320
+ assert_nil instantiation.value_indexed(nil,4)
321
+ assert_nil instantiation.value_indexed(nil,:head)
322
+ assert_nil instantiation.value_indexed(nil,:tail)
323
+ end
324
+
325
+ it 'should return nil when value is a Variable' do
326
+ assert_nil instantiation.value_indexed(variable3,nil)
327
+ assert_nil instantiation.value_indexed(variable3,4)
328
+ assert_nil instantiation.value_indexed(variable3,:head)
329
+ assert_nil instantiation.value_indexed(variable3,:tail)
330
+ end
331
+
332
+ it 'should return nil when value is a Symbol' do
333
+ assert_nil instantiation.value_indexed(:z,nil)
334
+ assert_nil instantiation.value_indexed(:z,4)
335
+ assert_nil instantiation.value_indexed(:z,:head)
336
+ assert_nil instantiation.value_indexed(:z,:tail)
337
+ end
338
+
339
+ it 'should return nil when value is an empty Array and index is :tail' do
340
+ assert_nil instantiation.value_indexed([],:tail)
341
+ end
342
+
343
+ it 'should return the indexed value when index is an Integer' do
344
+ assert_equal 7, instantiation.value_indexed([2,3,5,7,11],3)
345
+ assert_equal 14, instantiation.value_indexed(14,3)
346
+ assert_equal 'd', instantiation.value_indexed('word',3)
347
+ assert_equal 6.4, instantiation.value_indexed(6.4,3)
348
+ end
349
+
350
+ it 'should return the component when index is a Symbol' do
351
+ assert_equal 5, instantiation.value_indexed([2,3,5,7,11],:length)
352
+ assert_equal 11, instantiation.value_indexed([2,3,5,7,11],:last)
353
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],:nothing)
354
+ end
355
+
356
+ it 'should return the dynamically sized head when index is an Array' do
357
+ assert_equal [3,5,7,11], instantiation.value_indexed([2,3,5,7,11],[])
358
+ assert_equal [], instantiation.value_indexed([2,3,5,7,11],[0])
359
+ assert_equal [2], instantiation.value_indexed([2,3,5,7,11],[1])
360
+ assert_equal [2,3], instantiation.value_indexed([2,3,5,7,11],[2])
361
+ assert_equal [2], instantiation.value_indexed([2,3,5,7,11],[1,1])
362
+ end
363
+
364
+ it 'should raise an error when index is unexpected' do
365
+ assert_raises Instantiation::UnhandledIndexError do
366
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],12.34)
367
+ end
368
+ assert_raises Instantiation::UnhandledIndexError do
369
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],'hello')
370
+ end
371
+ assert_raises Instantiation::UnhandledIndexError do
372
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],Object)
373
+ end
374
+ object = Object.new
375
+ assert_raises Instantiation::UnhandledIndexError do
376
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],object)
377
+ end
378
+ end
379
+
380
+ end
381
+
382
+ describe '#values_for' do
383
+
384
+ it 'should return all values instantiated to the specified variable' do
385
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
386
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
387
+
388
+ assert_equal [], instantiation1.values_for(variable1)
389
+ assert_equal [], instantiation1.values_for(variable2)
390
+ assert_equal [], instantiation1.values_for(variable3)
391
+ assert_equal [], instantiation2.values_for(variable1)
392
+ assert_equal [], instantiation2.values_for(variable2)
393
+ assert_equal [], instantiation2.values_for(variable3)
394
+
395
+ variable3.instantiate Value.new('word',goal3)
396
+
397
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
398
+ 'Goal1.:m',
399
+ 'Goal1.:n',
400
+ 'Goal1.:x',
401
+ ' Goal2.:y',
402
+ ' Goal3.:z',
403
+ ' Goal3."word"',
404
+ ].join("\n")
405
+ assert_Goal_variables goal2, { p: nil, q: nil, y: 'word' }, [
406
+ 'Goal2.:p',
407
+ 'Goal2.:q',
408
+ 'Goal2.:y',
409
+ ' Goal1.:x',
410
+ ' Goal3.:z',
411
+ ' Goal3."word"',
412
+ ].join("\n")
413
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'word' }, [
414
+ 'Goal3.:x',
415
+ 'Goal3.:y',
416
+ 'Goal3.:z',
417
+ ' Goal2.:y',
418
+ ' Goal1.:x',
419
+ ' Goal3."word"',
420
+ ].join("\n")
421
+
422
+ assert_equal ['word'], instantiation1.values_for(variable1)
423
+ assert_equal [], instantiation1.values_for(variable2)
424
+ assert_equal [], instantiation1.values_for(variable3)
425
+ assert_equal [], instantiation2.values_for(variable1)
426
+ assert_equal ['word'], instantiation2.values_for(variable2)
427
+ assert_equal [], instantiation2.values_for(variable3)
428
+ end
429
+
430
+ it 'should return all indexed values instantiated to its variables' do
431
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
432
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
433
+
434
+ assert_equal [], instantiation1.values_for(variable1)
435
+ assert_equal [], instantiation1.values_for(variable2)
436
+ assert_equal [], instantiation2.values_for(variable2)
437
+ assert_equal [], instantiation2.values_for(variable3)
438
+
439
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
440
+
441
+ assert_equal 7, variable1.value
442
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
443
+ assert_equal 11, variable3.value
444
+
445
+ assert_equal [7], instantiation1.values_for(variable1)
446
+ assert_equal [], instantiation1.values_for(variable2)
447
+ assert_equal [], instantiation2.values_for(variable2)
448
+ assert_equal [11], instantiation2.values_for(variable3)
449
+
450
+ variable2.uninstantiate goal2
451
+
452
+ assert_equal variable1, variable1.value
453
+ assert_equal variable2, variable2.value
454
+ assert_equal variable3, variable3.value
455
+
456
+ assert_equal [], instantiation1.values
457
+ assert_equal [], instantiation2.values
458
+
459
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
460
+ variable2.instantiate Value.new(words, goal2)
461
+
462
+ assert_equal 'seven', variable1.value
463
+ assert_equal words, variable2.value
464
+ assert_equal 'eleven', variable3.value
465
+
466
+ assert_equal ['seven'], instantiation1.values
467
+ assert_equal ['eleven'], instantiation2.values
468
+ end
469
+
470
+ it 'should not infinitely recurse' do
471
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
472
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
473
+ instantiation3 = Instantiation.new variable3, nil, variable1, nil
474
+
475
+ assert_equal [], instantiation1.values_for(variable1)
476
+ assert_equal [], instantiation1.values_for(variable2)
477
+ assert_equal [], instantiation2.values_for(variable2)
478
+ assert_equal [], instantiation2.values_for(variable3)
479
+ assert_equal [], instantiation3.values_for(variable3)
480
+ assert_equal [], instantiation3.values_for(variable1)
481
+ end
482
+
483
+ it 'should return an uninstantiated tail when the head is known only' do
484
+ instantiation1 = Instantiation.new variable1, :head, variable2, nil
485
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
486
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
487
+
488
+ goal3.instantiate :v, 'head', goal3
489
+
490
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [goal3.value('head'), UNKNOWN_TAIL] }, [
491
+ 'Goal1.:m',
492
+ 'Goal1.:n',
493
+ 'Goal1.:x',
494
+ ' [:head]Goal2.:y',
495
+ ' Goal3.:z',
496
+ ' Goal3.:v',
497
+ ' Goal3."head"',
498
+ ].join("\n")
499
+
500
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'head', v: 'head' }, [
501
+ 'Goal3.:x',
502
+ 'Goal3.:y',
503
+ 'Goal3.:z',
504
+ ' Goal2.:y',
505
+ ' Goal1.:x[:head]',
506
+ ' Goal3.:v',
507
+ ' Goal3."head"',
508
+ 'Goal3.:v',
509
+ ' Goal3.:z',
510
+ ' Goal2.:y',
511
+ ' Goal1.:x[:head]',
512
+ ' Goal3."head"',
513
+ ].join("\n")
514
+
515
+ assert_equal [['head', UNKNOWN_TAIL]], instantiation1.values_for(variable1)
516
+ assert_equal [], instantiation1.values_for(variable2)
517
+ assert_equal ['head'], instantiation2.values_for(variable2)
518
+ assert_equal [], instantiation2.values_for(variable3)
519
+ assert_equal ['head'], instantiation3.values_for(variable3)
520
+ assert_equal [], instantiation3.values_for(variable4)
521
+ end
522
+
523
+ it 'should return an uninstantiated head when the tail is known only' do
524
+ instantiation1 = Instantiation.new variable1, :tail, variable2, nil
525
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
526
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
527
+
528
+ goal3.instantiate :v, ['this','is','the','tail'], goal3
529
+
530
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [nil, 'this','is','the','tail'] }, [
531
+ 'Goal1.:m',
532
+ 'Goal1.:n',
533
+ 'Goal1.:x',
534
+ ' [:tail]Goal2.:y',
535
+ ' Goal3.:z',
536
+ ' Goal3.:v',
537
+ ' Goal3.["this", "is", "the", "tail"]',
538
+ ].join("\n")
539
+
540
+ assert_Goal_variables goal3, { x: nil, y: nil, z: ['this','is','the','tail'], v: ['this','is','the','tail'] }, [
541
+ 'Goal3.:x',
542
+ 'Goal3.:y',
543
+ 'Goal3.:z',
544
+ ' Goal2.:y',
545
+ ' Goal1.:x[:tail]',
546
+ ' Goal3.:v',
547
+ ' Goal3.["this", "is", "the", "tail"]',
548
+ 'Goal3.:v',
549
+ ' Goal3.:z',
550
+ ' Goal2.:y',
551
+ ' Goal1.:x[:tail]',
552
+ ' Goal3.["this", "is", "the", "tail"]',
553
+ ].join("\n")
554
+
555
+ assert_equal [[nil, 'this','is','the','tail']], instantiation1.values_for(variable1)
556
+ assert_equal [], instantiation1.values_for(variable2)
557
+ assert_equal [goal3.value(['this','is','the','tail'])], instantiation2.values_for(variable2)
558
+ assert_equal [], instantiation2.values_for(variable3)
559
+ assert_equal [goal3.value(['this','is','the','tail'])], instantiation3.values_for(variable3)
560
+ assert_equal [], instantiation3.values_for(variable4)
561
+ end
562
+
563
+ it 'should return a list with uninstantiated elements and tail with the value at the index' do
564
+ instantiation1 = Instantiation.new variable1, 3, variable2, nil
565
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
566
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
567
+
568
+ goal3.instantiate :v, 45.9, goal3
569
+
570
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [nil, nil, nil, goal3.value(45.9), UNKNOWN_TAIL] }, [
571
+ 'Goal1.:m',
572
+ 'Goal1.:n',
573
+ 'Goal1.:x',
574
+ ' [3]Goal2.:y',
575
+ ' Goal3.:z',
576
+ ' Goal3.:v',
577
+ ' Goal3.45.9',
578
+ ].join("\n")
579
+
580
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 45.9, v: 45.9 }, [
581
+ 'Goal3.:x',
582
+ 'Goal3.:y',
583
+ 'Goal3.:z',
584
+ ' Goal2.:y',
585
+ ' Goal1.:x[3]',
586
+ ' Goal3.:v',
587
+ ' Goal3.45.9',
588
+ 'Goal3.:v',
589
+ ' Goal3.:z',
590
+ ' Goal2.:y',
591
+ ' Goal1.:x[3]',
592
+ ' Goal3.45.9',
593
+ ].join("\n")
594
+
595
+ assert_equal [[nil, nil, nil, 45.9, UNKNOWN_TAIL]], instantiation1.values_for(variable1)
596
+ assert_equal [], instantiation1.values_for(variable2)
597
+ assert_equal [goal3.value(45.9)], instantiation2.values_for(variable2)
598
+ assert_equal [], instantiation2.values_for(variable3)
599
+ assert_equal [goal3.value(45.9)], instantiation3.values_for(variable3)
600
+ assert_equal [], instantiation3.values_for(variable4)
601
+ end
602
+
603
+ it 'should return the value of variable1 when the specified variable is variable2 and variable1 is not a Variable' do
604
+ value1 = goal1.value('titanium')
605
+
606
+ instantiation1 = Instantiation.new value1, nil, variable3, nil
607
+
608
+ assert_equal [value1], instantiation1.values_for(variable3)
609
+ end
610
+
611
+ it 'should return the value of variable1 given the instantiation of the flathead of variable1' do
612
+ value1 = goal1.value([1,2,3,4])
613
+
614
+ instantiation1 = variable1.instantiate variable2, nil, :flathead
615
+ variable2.instantiate value1
616
+
617
+ assert_equal [[1,2,3,4,UNKNOWN_TAIL]], instantiation1.values_for(variable1)
618
+ end
619
+
620
+ it 'should return the value of variable1 given the instantiation of the flathead of variable2' do
621
+ value1 = goal1.value([1,2,3,4])
622
+
623
+ instantiation1 = variable2.instantiate variable1, :flathead
624
+ variable2.instantiate value1
625
+
626
+ assert_equal [[1,2,3,4,UNKNOWN_TAIL]], instantiation1.values_for(variable1)
627
+ end
628
+
629
+ it 'should return the value of variable1 given the instantiation of the flattail of variable1' do
630
+ value1 = goal1.value([1,2,3,4])
631
+
632
+ instantiation1 = variable1.instantiate variable2, nil, :flattail
633
+ variable2.instantiate value1
634
+
635
+ assert_equal [[UNKNOWN_TAIL,1,2,3,4]], instantiation1.values_for(variable1)
636
+ end
637
+
638
+ it 'should return the value of variable1 given the instantiation of the flattail of variable2' do
639
+ value1 = goal1.value([1,2,3,4])
640
+
641
+ instantiation1 = variable2.instantiate variable1, :flattail
642
+ variable2.instantiate value1
643
+
644
+ assert_equal [[UNKNOWN_TAIL,1,2,3,4]], instantiation1.values_for(variable1)
645
+ end
646
+
647
+ end
648
+
649
+ describe '#value_at_index' do
650
+
651
+ describe 'when index is nil' do
652
+
653
+ it 'should return nil for a nil value' do
654
+ assert_nil instantiation.value_at_index(nil,nil)
655
+ end
656
+
657
+ it 'should return the value for a non-nil value' do
658
+ assert_equal 'orange', instantiation.value_at_index('orange',nil)
659
+ end
660
+
661
+ end
662
+
663
+ describe 'when index is Integer' do
664
+
665
+ it 'should return nil for nil value' do
666
+ assert_nil instantiation.value_at_index(nil,5)
667
+ end
668
+
669
+ it 'should return an Array for an Integer indexed value' do
670
+ assert_equal [nil,nil,nil,nil,nil,'word',UNKNOWN_TAIL], instantiation.value_at_index('word',5)
671
+ end
672
+
673
+ end
674
+
675
+ describe 'when index is Symbol' do
676
+
677
+ it 'should return an Array for a head indexed value' do
678
+ assert_equal [['word','from','list'],UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],:head)
679
+ end
680
+
681
+ it 'should return an Array for a tail indexed value' do
682
+ assert_equal [nil,'word','from','list'], instantiation.value_at_index(['word','from','list'],:tail)
683
+ end
684
+
685
+ it 'should return an error for an unrecognised symbol indexed value' do
686
+ assert_raises Instantiation::UnhandledIndexError do
687
+ assert_equal :error, instantiation.value_at_index(['word','from','list'],:size)
688
+ end
689
+ end
690
+
691
+ end
692
+
693
+ describe 'when index is Array' do
694
+
695
+ it 'should return an Array for a tail indexed value' do
696
+ assert_equal [nil,'word','from','list'], instantiation.value_at_index(['word','from','list'],[])
697
+ end
698
+
699
+ it 'should return an Array for a tail indexed value' do
700
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[1])
701
+ end
702
+
703
+ it 'should return an Array for a tail indexed value' do
704
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[2])
705
+ end
706
+
707
+ it 'should return an Array for a tail indexed value' do
708
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[-2])
709
+ end
710
+
711
+ end
712
+
713
+ describe 'when index is Float' do
714
+
715
+ it 'should return an error for an unrecognised symbol indexed value' do
716
+ assert_raises Instantiation::UnhandledIndexError do
717
+ assert_equal ['word','from','list'], instantiation.value_at_index(['word','from','list'],2.3)
718
+ end
719
+ end
720
+
721
+ end
722
+
723
+ it 'should return all values instantiated to the specified variable' do
724
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
725
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
726
+
727
+ assert_equal [], instantiation1.values_for(variable1)
728
+ assert_equal [], instantiation1.values_for(variable2)
729
+ assert_equal [], instantiation1.values_for(variable3)
730
+ assert_equal [], instantiation2.values_for(variable1)
731
+ assert_equal [], instantiation2.values_for(variable2)
732
+ assert_equal [], instantiation2.values_for(variable3)
733
+
734
+ variable3.instantiate Value.new('word',goal3)
735
+
736
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
737
+ 'Goal1.:m',
738
+ 'Goal1.:n',
739
+ 'Goal1.:x',
740
+ ' Goal2.:y',
741
+ ' Goal3.:z',
742
+ ' Goal3."word"',
743
+ ].join("\n")
744
+
745
+ assert_Goal_variables goal2, { p: nil, q: nil, y: 'word' }, [
746
+ 'Goal2.:p',
747
+ 'Goal2.:q',
748
+ 'Goal2.:y',
749
+ ' Goal1.:x',
750
+ ' Goal3.:z',
751
+ ' Goal3."word"',
752
+ ].join("\n")
753
+
754
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'word' }, [
755
+ 'Goal3.:x',
756
+ 'Goal3.:y',
757
+ 'Goal3.:z',
758
+ ' Goal2.:y',
759
+ ' Goal1.:x',
760
+ ' Goal3."word"',
761
+ ].join("\n")
762
+
763
+ assert_equal ['word'], instantiation1.values_for(variable1)
764
+ assert_equal [], instantiation1.values_for(variable2)
765
+ assert_equal [], instantiation1.values_for(variable3)
766
+ assert_equal [], instantiation2.values_for(variable1)
767
+ assert_equal ['word'], instantiation2.values_for(variable2)
768
+ assert_equal [], instantiation2.values_for(variable3)
769
+ end
770
+
771
+ it 'should return all indexed values instantiated to its variables' do
772
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
773
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
774
+
775
+ assert_equal [], instantiation1.values_for(variable1)
776
+ assert_equal [], instantiation1.values_for(variable2)
777
+ assert_equal [], instantiation2.values_for(variable2)
778
+ assert_equal [], instantiation2.values_for(variable3)
779
+
780
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
781
+
782
+ assert_equal 7, variable1.value
783
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
784
+ assert_equal 11, variable3.value
785
+
786
+ assert_equal [7], instantiation1.values_for(variable1)
787
+ assert_equal [], instantiation1.values_for(variable2)
788
+ assert_equal [], instantiation2.values_for(variable2)
789
+ assert_equal [11], instantiation2.values_for(variable3)
790
+
791
+ variable2.uninstantiate goal2
792
+
793
+ assert_equal variable1, variable1.value
794
+ assert_equal variable2, variable2.value
795
+ assert_equal variable3, variable3.value
796
+
797
+ assert_equal [], instantiation1.values
798
+ assert_equal [], instantiation2.values
799
+
800
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
801
+ variable2.instantiate Value.new(words, goal2)
802
+
803
+ assert_equal 'seven', variable1.value
804
+ assert_equal words, variable2.value
805
+ assert_equal 'eleven', variable3.value
806
+
807
+ assert_equal ['seven'], instantiation1.values
808
+ assert_equal ['eleven'], instantiation2.values
809
+ end
810
+
811
+ it 'should not infinitely recurse' do
812
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
813
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
814
+ instantiation3 = Instantiation.new variable3, nil, variable1, nil
815
+
816
+ assert_equal [], instantiation1.values_for(variable1)
817
+ assert_equal [], instantiation1.values_for(variable2)
818
+ assert_equal [], instantiation2.values_for(variable2)
819
+ assert_equal [], instantiation2.values_for(variable3)
820
+ assert_equal [], instantiation3.values_for(variable3)
821
+ assert_equal [], instantiation3.values_for(variable1)
822
+ end
823
+
824
+ end
825
+
826
+ describe '#without_index_on' do
827
+
828
+ it 'should return true for a variable without an index in the instantiation' do
829
+ instantiation = Instantiation.new variable1, nil, variable2, nil
830
+
831
+ assert_equal true, instantiation.without_index_on?(variable1)
832
+ assert_equal true, instantiation.without_index_on?(variable2)
833
+ end
834
+
835
+ it 'should return correctly when the instantiation has one index' do
836
+ instantiation = Instantiation.new variable1, 3, variable2, nil
837
+
838
+ assert_equal false, instantiation.without_index_on?(variable1)
839
+ assert_equal true, instantiation.without_index_on?(variable2)
840
+ end
841
+
842
+ it 'should return correctly when the instantiation has one index (other one)' do
843
+ instantiation = Instantiation.new variable1, nil, variable2, 3
844
+
845
+ assert_equal true, instantiation.without_index_on?(variable1)
846
+ assert_equal false, instantiation.without_index_on?(variable2)
847
+ end
848
+
849
+ it 'should return false when the instantiation has two indexes' do
850
+ instantiation = Instantiation.new variable1, 3, variable2, 3
851
+
852
+ assert_equal false, instantiation.without_index_on?(variable1)
853
+ assert_equal false, instantiation.without_index_on?(variable2)
854
+ end
855
+
856
+ it 'should return false for a variable not in the instantiation' do
857
+ instantiation = Instantiation.new variable1, nil, variable2, nil
858
+
859
+ assert_equal false, instantiation.without_index_on?(variable3)
860
+ end
861
+
862
+ end
863
+
864
+ describe '#deleted?' do
865
+
866
+ it 'should return true when removed' do
867
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
868
+
869
+ instantiation.remove
870
+
871
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
872
+ end
873
+
874
+ it 'should return true when the goal of variable1 is deleted' do
875
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
876
+
877
+ Goal.goals.delete(goal1)
878
+
879
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
880
+ end
881
+
882
+ it 'should return true when the goal of variable2 is deleted' do
883
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
884
+
885
+ Goal.goals.delete(goal2)
886
+
887
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
888
+ end
889
+
890
+ end
891
+
892
+ describe '#belongs_to?' do
893
+
894
+ it 'should correctly determine the relationship between an instantition and a goal' do
895
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
896
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
897
+
898
+ assert instantiation1.belongs_to?(goal1), 'instantiation1 should belong to goal1'
899
+ assert instantiation1.belongs_to?(goal2), 'instantiation1 should belong to goal2'
900
+ refute instantiation1.belongs_to?(goal3), 'instantiation1 should not belong to goal3'
901
+ refute instantiation2.belongs_to?(goal1), 'instantiation2 should not belong to goal1'
902
+ assert instantiation2.belongs_to?(goal2), 'instantiation2 should belong to goal2'
903
+ assert instantiation2.belongs_to?(goal3), 'instantiation2 should belong to goal3'
904
+ end
905
+
906
+ end
907
+
908
+ end
909
+
910
+ end