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,1614 @@
1
+ #
2
+ # test/porolog/variable_test.rb - Test Suite for Porolog::Variable
3
+ #
4
+ # Luis Esteban 2 May 2018
5
+ # created
6
+ #
7
+
8
+ require_relative '../test_helper'
9
+
10
+ describe 'Porolog' do
11
+
12
+ describe 'Variable' do
13
+
14
+ let(:predicate1) { Predicate.new :generic }
15
+ let(:arguments1) { predicate1.arguments(:m,:n) }
16
+ let(:goal1) { arguments1.goal }
17
+ let(:goal2) { arguments1.goal }
18
+ let(:goal3) { arguments1.goal }
19
+
20
+ before do
21
+ reset
22
+ end
23
+
24
+ describe '.new' do
25
+
26
+ it 'should create a new variable in a goal' do
27
+ variable = Variable.new :x, goal1
28
+
29
+ assert_instance_of Variable, variable
30
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil }, [
31
+ 'Goal1.:m',
32
+ 'Goal1.:n',
33
+ 'Goal1.:x',
34
+ ].join("\n")
35
+ end
36
+
37
+ end
38
+
39
+ describe '#initialize' do
40
+
41
+ it 'should initialize name, goal, instantiations, and values' do
42
+ variable = Variable.new :x, goal1
43
+
44
+ assert_Variable variable, :x, goal1, [], []
45
+ end
46
+
47
+ it 'should convert a string name to a symbol name' do
48
+ variable = Variable.new 's', goal1
49
+
50
+ assert_equal :s, variable.name
51
+ end
52
+
53
+ it 'should convert a variable name to its name' do
54
+ other = Variable.new 'other', goal1
55
+ variable = Variable.new other, goal1
56
+
57
+ assert_equal :other, variable.name
58
+ assert_Goal_variables goal1, { m: nil, n: nil, other: nil }, [
59
+ 'Goal1.:m',
60
+ 'Goal1.:n',
61
+ 'Goal1.:other',
62
+ ].join("\n")
63
+ end
64
+
65
+ it 'should convert a value name to its name and initialize its values' do
66
+ value = Value.new 'other', goal1
67
+ variable = Variable.new value, goal1
68
+
69
+ assert_equal 'other', variable.name
70
+ assert_equal [value], variable.values
71
+
72
+ assert_equal 'other', variable.value
73
+ end
74
+
75
+ it 'should convert other types as a name to its value' do
76
+ # TECH-DEBT: Not super sure about this spec!
77
+ variable = Variable.new 0.875, goal1
78
+
79
+ assert_equal '0.875', variable.name
80
+ assert_instance_of Array, variable.values
81
+ assert_equal 1, variable.values.size
82
+
83
+ assert_Value variable.values.first, 0.875, goal1
84
+ assert_equal 0.875, variable.value
85
+ end
86
+
87
+ it 'should raise an error when a goal is not provided' do
88
+ assert_raises Variable::GoalError do
89
+ Variable.new :x, 'goal'
90
+ end
91
+ end
92
+
93
+ it 'should declare the variable in the goal' do
94
+ Variable.new :x, goal1
95
+
96
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil }, [
97
+ 'Goal1.:m',
98
+ 'Goal1.:n',
99
+ 'Goal1.:x',
100
+ ].join("\n")
101
+ end
102
+
103
+ end
104
+
105
+ describe '#to_sym' do
106
+
107
+ it 'should convert a variable to a Symbol' do
108
+ variable = Variable.new 'v', goal1
109
+
110
+ assert_equal :v, variable.to_sym
111
+ end
112
+
113
+ end
114
+
115
+ describe '#type' do
116
+
117
+ it 'should return variable from uninstantiated variables' do
118
+ variable = Variable.new 'v', goal1
119
+
120
+ assert_equal :variable, variable.type
121
+ end
122
+
123
+ it 'should return variable from instantiated variables' do
124
+ variable = Variable.new 'v', goal1
125
+ variable.instantiate 'string value'
126
+
127
+ assert_equal :variable, variable.type
128
+ end
129
+
130
+ end
131
+
132
+ describe '#inspect' do
133
+
134
+ it 'should show the goal and name' do
135
+ variable = Variable.new 'v', goal1
136
+
137
+ assert_equal 'Goal1.:v', variable.inspect
138
+ end
139
+
140
+ end
141
+
142
+ describe '#inspect_with_instantiations' do
143
+
144
+ it 'should show a variable without instantiations as inspect does' do
145
+ variable = Variable.new :x, goal1
146
+
147
+ assert_equal variable.inspect, variable.inspect_with_instantiations
148
+ end
149
+
150
+ it 'should return all instantiations of a variable with nested instantiations' do
151
+ variable1 = Variable.new :x, goal1
152
+ variable2 = Variable.new :y, goal1
153
+ variable3 = Variable.new :z, goal1
154
+
155
+ variable1.instantiate variable2
156
+ variable2.instantiate variable3
157
+
158
+ assert_equal 'Goal1.:x', variable1.inspect
159
+ assert_equal 'Goal1.:y', variable2.inspect
160
+ assert_equal 'Goal1.:z', variable3.inspect
161
+
162
+ assert_instance_of Array, variable1.instantiations
163
+ assert_instance_of Array, variable2.instantiations
164
+ assert_instance_of Array, variable3.instantiations
165
+
166
+ assert_equal 1, variable1.instantiations.size
167
+ assert_equal 2, variable2.instantiations.size
168
+ assert_equal 1, variable3.instantiations.size
169
+
170
+ assert_instance_of Instantiation, variable1.instantiations.first
171
+ assert_equal 'Goal1.:x = Goal1.:y', variable1.instantiations.first.inspect
172
+
173
+ assert_instance_of Instantiation, variable2.instantiations[0]
174
+ assert_equal 'Goal1.:x = Goal1.:y', variable2.instantiations[0].inspect
175
+
176
+ assert_instance_of Instantiation, variable2.instantiations[1]
177
+ assert_equal 'Goal1.:y = Goal1.:z', variable2.instantiations[1].inspect
178
+
179
+ assert_instance_of Instantiation, variable3.instantiations.first
180
+ assert_equal 'Goal1.:y = Goal1.:z', variable3.instantiations.first.inspect
181
+
182
+ assert_equal variable1.instantiations + variable3.instantiations, variable2.instantiations
183
+
184
+ assert_equal(
185
+ [
186
+ 'Goal1.:x',
187
+ ' Goal1.:y',
188
+ ' Goal1.:z',
189
+ 'Goal1.:y',
190
+ ' Goal1.:x',
191
+ ' Goal1.:z',
192
+ 'Goal1.:z',
193
+ ' Goal1.:y',
194
+ ' Goal1.:x',
195
+ ].join("\n"),
196
+ [
197
+ variable1.inspect_with_instantiations,
198
+ variable2.inspect_with_instantiations,
199
+ variable3.inspect_with_instantiations,
200
+ ].join("\n")
201
+ )
202
+ end
203
+
204
+ end
205
+
206
+ describe '#value' do
207
+
208
+ it 'should return the variable when no value has been instantiated' do
209
+ variable1 = Variable.new :x, goal1
210
+ variable2 = Variable.new :y, goal1
211
+
212
+ variable1.instantiate variable2
213
+
214
+ assert_equal variable1, variable1.value
215
+ end
216
+
217
+ it 'should return the indirect value through instantiations' do
218
+ variable1 = Variable.new :x, goal1
219
+ variable2 = Variable.new :y, goal1
220
+ value = Value.new [1,2,3], goal1
221
+
222
+ variable1.instantiate variable2
223
+ variable2.instantiate value
224
+
225
+ assert_equal [1,2,3], variable1.value
226
+ end
227
+
228
+ it 'should not return unbound headtails' do
229
+ assert_Goal goal1, :generic, [:m, :n]
230
+
231
+ # -- Create Variables --
232
+ variable1 = Variable.new :x, goal1
233
+ variable2 = Variable.new :y, goal1
234
+ variable3 = Variable.new :z, goal1
235
+
236
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil, y: nil, z: nil }, [
237
+ 'Goal1.:m',
238
+ 'Goal1.:n',
239
+ 'Goal1.:x',
240
+ 'Goal1.:y',
241
+ 'Goal1.:z',
242
+ ].join("\n")
243
+
244
+ # -- Create Values --
245
+ headtail = goal1.variablise(:m/:n)
246
+ assert_Array_with_Tail headtail, [goal1.variable(:m)], '*Goal1.:n'
247
+
248
+ value1 = Value.new headtail, goal1
249
+ value2 = Value.new [1,2,3], goal1
250
+
251
+ assert_Value value1, headtail, goal1
252
+ assert_Value value2, [1,2,3], goal1
253
+
254
+ # -- Instantiate --
255
+ # Goal1.:x
256
+ # Goal1.:y
257
+ i1 = variable1.instantiate variable2
258
+
259
+ assert_Instantiation i1, variable1, variable2, nil, nil
260
+
261
+ # -- Assert No Values --
262
+ assert_equal variable1, variable1.value
263
+ assert_equal variable2, variable2.value
264
+ assert_equal variable3, variable3.value
265
+
266
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil, y: nil, z: nil }, [
267
+ 'Goal1.:m',
268
+ 'Goal1.:n',
269
+ 'Goal1.:x',
270
+ ' Goal1.:y',
271
+ 'Goal1.:y',
272
+ ' Goal1.:x',
273
+ 'Goal1.:z',
274
+ ].join("\n")
275
+
276
+ # -- Instantiate --
277
+ # Goal1.:x
278
+ # Goal1.:z
279
+ i2 = variable1.instantiate variable3
280
+
281
+ assert_Instantiation i2, variable1, variable3, nil, nil
282
+
283
+ # -- Assert No Values --
284
+ assert_equal variable1, variable1.value
285
+ assert_equal variable2, variable2.value
286
+ assert_equal variable3, variable3.value
287
+
288
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil, y: nil, z: nil }, [
289
+ 'Goal1.:m',
290
+ 'Goal1.:n',
291
+ 'Goal1.:x',
292
+ ' Goal1.:y',
293
+ ' Goal1.:z',
294
+ 'Goal1.:y',
295
+ ' Goal1.:x',
296
+ ' Goal1.:z',
297
+ 'Goal1.:z',
298
+ ' Goal1.:x',
299
+ ' Goal1.:y',
300
+ ].join("\n")
301
+
302
+ # -- Instantiate --
303
+ # Goal1.:y
304
+ # [Goal1.:m, *Goal1.:n]
305
+ i3 = variable2.instantiate value1
306
+
307
+ assert_Instantiation i3, variable2, value1, nil, nil
308
+ assert_equal headtail, variable1.value
309
+ assert_equal headtail, variable2.value
310
+ assert_equal headtail, variable3.value
311
+ assert_equal headtail, goal1.value_of(:x)
312
+ assert_equal headtail, goal1.value_of(:y)
313
+ assert_equal headtail, goal1.value_of(:z)
314
+ assert_Goal_variables goal1, {
315
+ m: nil,
316
+ n: nil,
317
+ x: [nil, UNKNOWN_TAIL],
318
+ y: [nil, UNKNOWN_TAIL],
319
+ z: [nil, UNKNOWN_TAIL]
320
+ }, [
321
+ 'Goal1.:m',
322
+ ' Goal1.:y[:head]',
323
+ ' Goal1.:x',
324
+ ' Goal1.:z',
325
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
326
+ ' [:tail]Goal1.:n',
327
+ 'Goal1.:n',
328
+ ' Goal1.:y[:tail]',
329
+ ' Goal1.:x',
330
+ ' Goal1.:z',
331
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
332
+ ' [:head]Goal1.:m',
333
+ 'Goal1.:x',
334
+ ' Goal1.:y',
335
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
336
+ ' [:head]Goal1.:m',
337
+ ' [:tail]Goal1.:n',
338
+ ' Goal1.:z',
339
+ 'Goal1.:y',
340
+ ' Goal1.:x',
341
+ ' Goal1.:z',
342
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
343
+ ' [:head]Goal1.:m',
344
+ ' [:tail]Goal1.:n',
345
+ 'Goal1.:z',
346
+ ' Goal1.:x',
347
+ ' Goal1.:y',
348
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
349
+ ' [:head]Goal1.:m',
350
+ ' [:tail]Goal1.:n',
351
+ ].join("\n")
352
+
353
+ # -- Instantiate --
354
+ i3 = variable3.instantiate value2
355
+
356
+ assert_Instantiation i3, variable3, value2, nil, nil
357
+ assert_equal [1,2,3], variable1.value
358
+ assert_equal [1,2,3], variable2.value
359
+ assert_equal [1,2,3], variable3.value
360
+
361
+ assert_Goal_variables goal1, { m: 1, n: [2,3], x: [1,2,3], y: [1,2,3], z: [1,2,3] }, [
362
+ 'Goal1.:m',
363
+ ' Goal1.:y[:head]',
364
+ ' Goal1.:x',
365
+ ' Goal1.:z',
366
+ ' Goal1.[1, 2, 3]',
367
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
368
+ ' [:tail]Goal1.:n',
369
+ 'Goal1.:n',
370
+ ' Goal1.:y[:tail]',
371
+ ' Goal1.:x',
372
+ ' Goal1.:z',
373
+ ' Goal1.[1, 2, 3]',
374
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
375
+ ' [:head]Goal1.:m',
376
+ 'Goal1.:x',
377
+ ' Goal1.:y',
378
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
379
+ ' [:head]Goal1.:m',
380
+ ' [:tail]Goal1.:n',
381
+ ' Goal1.:z',
382
+ ' Goal1.[1, 2, 3]',
383
+ 'Goal1.:y',
384
+ ' Goal1.:x',
385
+ ' Goal1.:z',
386
+ ' Goal1.[1, 2, 3]',
387
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
388
+ ' [:head]Goal1.:m',
389
+ ' [:tail]Goal1.:n',
390
+ 'Goal1.:z',
391
+ ' Goal1.:x',
392
+ ' Goal1.:y',
393
+ ' Goal1.[Goal1.:m, *Goal1.:n]',
394
+ ' [:head]Goal1.:m',
395
+ ' [:tail]Goal1.:n',
396
+ ' Goal1.[1, 2, 3]',
397
+ ].join("\n")
398
+ end
399
+
400
+ it 'should not return the indirect value through instantiations after uninstantiating' do
401
+ variable1 = Variable.new :x, goal1
402
+ variable2 = Variable.new :y, goal1
403
+ value = Value.new [1,2,3], goal1
404
+
405
+ variable1.instantiate variable2
406
+ variable2.instantiate value
407
+
408
+ assert_equal [1,2,3], variable1.value
409
+
410
+ variable2.uninstantiate goal1
411
+
412
+ assert_equal variable1, variable1.value
413
+ assert_equal variable2, variable2.value
414
+ end
415
+
416
+ it 'should not instantiate multiple unequal values' do
417
+ variable1 = Variable.new :x, goal1
418
+ variable2 = Variable.new :y, goal2
419
+ variable3 = Variable.new :z, goal3
420
+
421
+ value1 = Value.new [1,2,3], goal2
422
+ value2 = Value.new 'word', goal3
423
+
424
+ i1 = variable1.instantiate variable2
425
+ i2 = variable1.instantiate variable3
426
+ i3 = variable2.instantiate value1
427
+ i4 = variable3.instantiate value2
428
+
429
+ assert_Instantiation i1, variable1, variable2, nil, nil
430
+ assert_Instantiation i2, variable1, variable3, nil, nil
431
+ assert_Instantiation i3, variable2, value1, nil, nil
432
+ assert_nil i4
433
+
434
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [1,2,3] }, [
435
+ 'Goal1.:m',
436
+ 'Goal1.:n',
437
+ 'Goal1.:x',
438
+ ' Goal2.:y',
439
+ ' Goal2.[1, 2, 3]',
440
+ ' Goal3.:z',
441
+ ].join("\n")
442
+ assert_Goal_variables goal2, { m: nil, n: nil, y: [1,2,3] }, [
443
+ 'Goal2.:m',
444
+ 'Goal2.:n',
445
+ 'Goal2.:y',
446
+ ' Goal1.:x',
447
+ ' Goal3.:z',
448
+ ' Goal2.[1, 2, 3]',
449
+ ].join("\n")
450
+ assert_Goal_variables goal3, { m: nil, n: nil, z: [1,2,3] }, [
451
+ 'Goal3.:m',
452
+ 'Goal3.:n',
453
+ 'Goal3.:z',
454
+ ' Goal1.:x',
455
+ ' Goal2.:y',
456
+ ' Goal2.[1, 2, 3]',
457
+ ].join("\n")
458
+ end
459
+
460
+ it 'should not raise an exception when multiple values are equal' do
461
+ variable1 = Variable.new :x, goal1
462
+ variable2 = Variable.new :y, goal2
463
+ variable3 = Variable.new :z, goal3
464
+
465
+ value1 = Value.new 'word', goal2
466
+ value2 = Value.new 'word', goal3
467
+
468
+ variable1.instantiate variable2
469
+ variable1.instantiate variable3
470
+ variable2.instantiate value1
471
+ variable3.instantiate value2
472
+
473
+ [
474
+ value1,
475
+ value2,
476
+ variable1,
477
+ variable2,
478
+ variable3,
479
+ ].each do |v|
480
+ assert_equal 'word', v.value, "#{v.inspect} value should be 'word'"
481
+ end
482
+ end
483
+
484
+ it 'should raise an exception when the variable has multiple different non-array values' do
485
+ variable1 = Variable.new :x, goal1
486
+
487
+ variable1.values << 1
488
+ variable1.values << 'one'
489
+
490
+ error = assert_raises Porolog::Variable::MultipleValuesError do
491
+ variable1.value
492
+ end
493
+
494
+ assert_equal 'Multiple values detected for Goal1.:x: [1, "one"]', error.message
495
+ end
496
+
497
+ it 'should prioritise non-variables over variables' do
498
+ variable1 = Variable.new :x, goal1
499
+
500
+ assert_equal goal1[:x], variable1.value
501
+
502
+ variable1.values << :variable
503
+
504
+ assert_equal :variable, variable1.value
505
+
506
+ variable1.values << 'non-variable'
507
+ variable1.values << :variable
508
+
509
+ assert_equal 'non-variable', variable1.value
510
+ end
511
+
512
+ it 'should prioritise variables over an unknown array' do
513
+ variable1 = Variable.new :x, goal1
514
+
515
+ assert_equal goal1[:x], variable1.value
516
+
517
+ variable1.values << UNKNOWN_ARRAY
518
+
519
+ assert_equal UNKNOWN_ARRAY, variable1.value
520
+
521
+ variable1.values << :variable1
522
+ variable1.values << :variable2
523
+ variable1.values << UNKNOWN_ARRAY
524
+
525
+ assert_equal :variable1, variable1.value
526
+ end
527
+
528
+ it 'should unify a matching flathead and flattail pair' do
529
+ variable1 = Variable.new :x, goal1
530
+
531
+ variable1.values << [1,2,UNKNOWN_TAIL]
532
+ variable1.values << [UNKNOWN_TAIL,3,4]
533
+
534
+ assert_equal [1,2,3,4], variable1.value
535
+ end
536
+
537
+ it 'should unify a matching flattail and flathead pair' do
538
+ variable1 = Variable.new :x, goal1
539
+
540
+ variable1.values << [UNKNOWN_TAIL,3,4]
541
+ variable1.values << [1,2,UNKNOWN_TAIL]
542
+
543
+ assert_equal [1,2,3,4], variable1.value
544
+ end
545
+
546
+ it 'should return nil when the values are incompatible' do
547
+ variable1 = Variable.new :x, goal1
548
+
549
+ variable1.values << [1,3,4]
550
+ variable1.values << [1,2,UNKNOWN_TAIL]
551
+
552
+ assert_nil variable1.value
553
+ end
554
+
555
+ it 'should unify a special case' do
556
+ variable1 = Variable.new :x, goal1
557
+
558
+ variable1.values << [1,[3,4]]
559
+ variable1.values << goal1[:h]/goal1[:t]
560
+
561
+ assert_equal [1,[3,4]], variable1.value
562
+ end
563
+
564
+ end
565
+
566
+ describe '#instantiate' do
567
+
568
+ it 'should instantiate with another variable' do
569
+ variable1 = Variable.new :x, goal1
570
+ variable2 = Variable.new :y, goal2
571
+
572
+ variable1.instantiate variable2
573
+
574
+ assert_equal "Goal1.:x\n Goal2.:y", variable1.inspect_with_instantiations
575
+ assert_equal "Goal2.:y\n Goal1.:x", variable2.inspect_with_instantiations
576
+ end
577
+
578
+ it 'should instantiate with a value' do
579
+ variable1 = Variable.new :x, goal1
580
+ variable2 = Variable.new :y, goal2
581
+ value = Value.new 'word', goal2
582
+
583
+ variable1.instantiate variable2
584
+ variable2.instantiate value
585
+
586
+ assert_equal 'word', variable1.value
587
+ assert_equal "Goal1.:x\n Goal2.:y\n Goal2.\"word\"", variable1.inspect_with_instantiations
588
+ assert_equal "Goal2.:y\n Goal1.:x\n Goal2.\"word\"", variable2.inspect_with_instantiations
589
+ end
590
+
591
+ it 'should instantiate with an indexed variable' do
592
+ # -- Elements --
593
+ variable1 = Variable.new :x, goal1
594
+ variable2 = Variable.new :y, goal2
595
+ value = Value.new [7,11,13,23,29], goal2
596
+
597
+ # -- Make Instantiations --
598
+ #
599
+ # Goal1.:x == Goal2.y[2], Goal2.y == Goal2.[7,11,13,23,29]
600
+ #
601
+ variable1.instantiate variable2, 2
602
+ variable2.instantiate value
603
+
604
+ # -- Assert instantiations --
605
+ assert_equal 1, variable1.instantiations.size
606
+ assert_equal 2, variable2.instantiations.size
607
+
608
+ assert_equal 'Goal1.:x = Goal2.:y[2]', variable1.instantiations[0].inspect
609
+ assert_equal 'Goal1.:x = Goal2.:y[2]', variable2.instantiations[0].inspect
610
+ assert_equal 'Goal2.:y = Goal2.[7, 11, 13, 23, 29]', variable2.instantiations[1].inspect
611
+
612
+ assert_equal variable1.instantiations[0], variable2.instantiations[0]
613
+
614
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, 2
615
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, 2
616
+ assert_Instantiation variable2.instantiations[1], variable2, value, nil, nil
617
+
618
+ assert_Value variable2.instantiations[1].variable2, [7, 11, 13, 23, 29], goal2
619
+
620
+ # -- Assert Values --
621
+ assert_equal 13, variable1.value
622
+ assert_equal [7, 11, 13, 23, 29], variable2.value
623
+
624
+ # -- Assert inspects --
625
+ assert_equal "Goal1.:x\n Goal2.:y[2]\n Goal2.[7, 11, 13, 23, 29]", variable1.inspect_with_instantiations
626
+ assert_equal "Goal2.:y\n [2]Goal1.:x\n Goal2.[7, 11, 13, 23, 29]", variable2.inspect_with_instantiations
627
+ end
628
+
629
+ it 'should instantiate indexed with an indexed variable' do
630
+ # -- Elements --
631
+ variable1 = Variable.new :x, goal1
632
+ variable2 = Variable.new :y, goal2
633
+ variable3 = Variable.new :z, goal2
634
+ value = Value.new [[],[1,2,3],'word',[7,11,13,23,29]], goal2
635
+
636
+ assert_Value value, [[],[1,2,3],'word',[7,11,13,23,29]], goal2
637
+
638
+ # -- Assert goal variables --
639
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil }, [
640
+ 'Goal1.:m',
641
+ 'Goal1.:n',
642
+ 'Goal1.:x',
643
+ ].join("\n")
644
+ assert_Goal_variables goal2, { m: nil, n: nil, y: nil, z: nil }, [
645
+ 'Goal2.:m',
646
+ 'Goal2.:n',
647
+ 'Goal2.:y',
648
+ 'Goal2.:z',
649
+ ].join("\n")
650
+
651
+ # -- Make instantiations --
652
+ #
653
+ # Goal1.:x == Goal2.:y[2], Goal2.:y == [[],[1,2,3],'word',[7,11,13,23,29]][3]
654
+ #
655
+ variable1.instantiate variable2, 2
656
+
657
+ # Goal2.:y == [[],[1,2,3],'word',[7,11,13,23,29]][3]
658
+ # == [7,11,13,23,29]
659
+ variable2.instantiate value, 3
660
+
661
+ # -- Assert Values --
662
+ assert_equal 13, variable1.value
663
+ assert_equal [7,11,13,23,29], variable2.value
664
+ assert_equal variable3, variable3.value
665
+
666
+ # -- Assert instantiations --
667
+ assert_equal 1, variable1.instantiations.size
668
+ assert_equal 2, variable2.instantiations.size
669
+ assert_equal 0, variable3.instantiations.size
670
+
671
+ assert_equal 'Goal1.:x = Goal2.:y[2]', variable1.instantiations[0].inspect
672
+ assert_equal 'Goal1.:x = Goal2.:y[2]', variable2.instantiations[0].inspect
673
+ assert_equal 'Goal2.:y = Goal2.[[], [1, 2, 3], "word", [7, 11, 13, 23, 29]][3]', variable2.instantiations[1].inspect
674
+
675
+ assert_equal variable1.instantiations[0], variable2.instantiations[0]
676
+
677
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, 2
678
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, 2
679
+ assert_Instantiation variable2.instantiations[1], variable2, value, nil, 3
680
+
681
+ assert_Value variable2.instantiations[1].variable2, [[],[1,2,3],'word',[7,11,13,23,29]], goal2
682
+
683
+ # -- Assert overall instantiations --
684
+ assert_equal "Goal2.:y\n [2]Goal1.:x\n Goal2.[[], [1, 2, 3], \"word\", [7, 11, 13, 23, 29]][3]", variable2.inspect_with_instantiations
685
+ assert_equal "Goal1.:x\n Goal2.:y[2]\n Goal2.[[], [1, 2, 3], \"word\", [7, 11, 13, 23, 29]][3]", variable1.inspect_with_instantiations
686
+
687
+ # -- Assert values --
688
+ assert_equal [], variable1.values
689
+ assert_equal 0, variable2.values.size
690
+ assert_equal [], variable3.values
691
+
692
+ assert_equal [7,11,13,23,29], variable2.value
693
+ assert_equal 13, variable1.value
694
+ assert_equal [7, 11, 13, 23, 29], variable2.value
695
+
696
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 13 }, [
697
+ 'Goal1.:m',
698
+ 'Goal1.:n',
699
+ 'Goal1.:x',
700
+ ' Goal2.:y[2]',
701
+ ' Goal2.[[], [1, 2, 3], "word", [7, 11, 13, 23, 29]][3]',
702
+ ].join("\n")
703
+
704
+ assert_Goal_variables goal2, { m: nil, n: nil, y: [7, 11, 13, 23, 29], z: nil}, [
705
+ 'Goal2.:m',
706
+ 'Goal2.:n',
707
+ 'Goal2.:y',
708
+ ' [2]Goal1.:x',
709
+ ' Goal2.[[], [1, 2, 3], "word", [7, 11, 13, 23, 29]][3]',
710
+ 'Goal2.:z',
711
+ ].join("\n")
712
+
713
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 13 }, [
714
+ 'Goal1.:m',
715
+ 'Goal1.:n',
716
+ 'Goal1.:x',
717
+ ' Goal2.:y[2]',
718
+ ' Goal2.[[], [1, 2, 3], "word", [7, 11, 13, 23, 29]][3]'
719
+ ].join("\n")
720
+
721
+ assert_Goal_variables goal2, { m: nil, n: nil, y: [7, 11, 13, 23, 29], z: nil }, [
722
+ 'Goal2.:m',
723
+ 'Goal2.:n',
724
+ 'Goal2.:y',
725
+ ' [2]Goal1.:x',
726
+ ' Goal2.[[], [1, 2, 3], "word", [7, 11, 13, 23, 29]][3]',
727
+ 'Goal2.:z'
728
+ ].join("\n")
729
+ end
730
+
731
+ it 'should instantiate with an indexed value' do
732
+ variable1 = Variable.new :x, goal1
733
+ variable2 = Variable.new :y, goal2
734
+ value = Value.new [7,11,13,23,29], goal2
735
+
736
+ variable1.instantiate variable2
737
+ variable2.instantiate value, 3
738
+
739
+ assert_equal "Goal1.:x\n Goal2.:y\n Goal2.[7, 11, 13, 23, 29][3]", variable1.inspect_with_instantiations
740
+ assert_equal "Goal2.:y\n Goal1.:x\n Goal2.[7, 11, 13, 23, 29][3]", variable2.inspect_with_instantiations
741
+ end
742
+
743
+ it 'should instantiate with an indexed variable' do
744
+ variable1 = Variable.new :x, goal1
745
+ variable2 = Variable.new :y, goal2
746
+ value = Value.new [7,11,13,23,29], goal2
747
+
748
+ variable1.instantiate variable2, 2
749
+ variable2.instantiate value
750
+
751
+ assert_equal 13, variable1.value
752
+ assert_equal "Goal1.:x\n Goal2.:y[2]\n Goal2.[7, 11, 13, 23, 29]", variable1.inspect_with_instantiations
753
+ assert_equal "Goal2.:y\n [2]Goal1.:x\n Goal2.[7, 11, 13, 23, 29]", variable2.inspect_with_instantiations
754
+ end
755
+
756
+ it 'should instantiate with an indexed value' do
757
+ variable1 = Variable.new :x, goal1
758
+ variable2 = Variable.new :y, goal2
759
+ value = Value.new [7,11,13,23,29], goal2
760
+
761
+ variable1.instantiate variable2
762
+ variable2.instantiate value, 3
763
+
764
+ assert_equal "Goal1.:x\n Goal2.:y\n Goal2.[7, 11, 13, 23, 29][3]", variable1.inspect_with_instantiations
765
+ assert_equal "Goal2.:y\n Goal1.:x\n Goal2.[7, 11, 13, 23, 29][3]", variable2.inspect_with_instantiations
766
+ end
767
+
768
+ it 'should detect deep conflicting instantiations' do
769
+ # g1:a
770
+ # g2:b g3:c
771
+ # g4:d g5:e
772
+ # g6:f g7:g
773
+ # 8 9
774
+ reset
775
+ g1 = arguments1.goal
776
+ g2 = arguments1.goal
777
+ g3 = arguments1.goal
778
+ g4 = arguments1.goal
779
+ g5 = arguments1.goal
780
+ g6 = arguments1.goal
781
+ g7 = arguments1.goal
782
+
783
+ a = g1.variable :a
784
+ b = g2.variable :b
785
+ c = g3.variable :c
786
+ d = g4.variable :d
787
+ e = g5.variable :e
788
+ f = g6.variable :f
789
+ g = g7.variable :g
790
+
791
+ assert_instance_of Instantiation, f.instantiate(g6.value(8))
792
+ assert_instance_of Instantiation, g.instantiate(g7.value(9))
793
+
794
+ assert_instance_of Instantiation, a.instantiate(b)
795
+ assert_instance_of Instantiation, a.instantiate(c)
796
+ assert_instance_of Instantiation, b.instantiate(d)
797
+ assert_instance_of Instantiation, d.instantiate(f)
798
+ assert_instance_of Instantiation, c.instantiate(e)
799
+ assert_nil e.instantiate(g)
800
+
801
+ assert_Goal_variables g1, { m: nil, n: nil, a: 8 }, [
802
+ 'Goal1.:m',
803
+ 'Goal1.:n',
804
+ 'Goal1.:a',
805
+ ' Goal2.:b',
806
+ ' Goal4.:d',
807
+ ' Goal6.:f',
808
+ ' Goal6.8',
809
+ ' Goal3.:c',
810
+ ' Goal5.:e',
811
+ ].join("\n")
812
+ assert_Goal_variables g2, { m: nil, n: nil, b: 8 }, [
813
+ 'Goal2.:m',
814
+ 'Goal2.:n',
815
+ 'Goal2.:b',
816
+ ' Goal1.:a',
817
+ ' Goal3.:c',
818
+ ' Goal5.:e',
819
+ ' Goal4.:d',
820
+ ' Goal6.:f',
821
+ ' Goal6.8',
822
+ ].join("\n")
823
+ assert_Goal_variables g3, { m: nil, n: nil, c: 8 }, [
824
+ 'Goal3.:m',
825
+ 'Goal3.:n',
826
+ 'Goal3.:c',
827
+ ' Goal1.:a',
828
+ ' Goal2.:b',
829
+ ' Goal4.:d',
830
+ ' Goal6.:f',
831
+ ' Goal6.8',
832
+ ' Goal5.:e',
833
+ ].join("\n")
834
+ assert_Goal_variables g4, { m: nil, n: nil, d: 8 }, [
835
+ 'Goal4.:m',
836
+ 'Goal4.:n',
837
+ 'Goal4.:d',
838
+ ' Goal2.:b',
839
+ ' Goal1.:a',
840
+ ' Goal3.:c',
841
+ ' Goal5.:e',
842
+ ' Goal6.:f',
843
+ ' Goal6.8',
844
+ ].join("\n")
845
+ assert_Goal_variables g5, { m: nil, n: nil, e: 8 }, [
846
+ 'Goal5.:m',
847
+ 'Goal5.:n',
848
+ 'Goal5.:e',
849
+ ' Goal3.:c',
850
+ ' Goal1.:a',
851
+ ' Goal2.:b',
852
+ ' Goal4.:d',
853
+ ' Goal6.:f',
854
+ ' Goal6.8',
855
+ ].join("\n")
856
+ assert_Goal_variables g6, { m: nil, n: nil, f: 8 }, [
857
+ 'Goal6.:m',
858
+ 'Goal6.:n',
859
+ 'Goal6.:f',
860
+ ' Goal6.8',
861
+ ' Goal4.:d',
862
+ ' Goal2.:b',
863
+ ' Goal1.:a',
864
+ ' Goal3.:c',
865
+ ' Goal5.:e',
866
+ ].join("\n")
867
+ assert_Goal_variables g7, { m: nil, n: nil, g: 9 }, [
868
+ 'Goal7.:m',
869
+ 'Goal7.:n',
870
+ 'Goal7.:g',
871
+ ' Goal7.9',
872
+ ].join("\n")
873
+ end
874
+
875
+ it 'should return nil when the there are multiple different values' do
876
+ variable1 = Variable.new :x, goal1
877
+ value = Value.new 111, goal2
878
+
879
+ variable1.values << 112
880
+
881
+ instantiation = variable1.instantiate value
882
+
883
+ assert_nil instantiation, name
884
+ end
885
+
886
+ end
887
+
888
+ describe '#remove' do
889
+
890
+ let(:variable1) { Variable.new :x, goal1 }
891
+ let(:variable2) { Variable.new :y, goal2 }
892
+ let(:variable3) { Variable.new :z, goal3 }
893
+ let(:value1) { Value.new 'word', goal2 }
894
+ let(:value2) { Value.new 'word', goal3 }
895
+
896
+ before do
897
+ variable1.instantiate variable2
898
+ variable1.instantiate variable3
899
+ variable2.instantiate value1
900
+ variable3.instantiate value2
901
+
902
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
903
+ 'Goal1.:m',
904
+ 'Goal1.:n',
905
+ 'Goal1.:x',
906
+ ' Goal2.:y',
907
+ ' Goal2."word"',
908
+ ' Goal3.:z',
909
+ ' Goal3."word"',
910
+ ].join("\n")
911
+
912
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
913
+ 'Goal2.:m',
914
+ 'Goal2.:n',
915
+ 'Goal2.:y',
916
+ ' Goal1.:x',
917
+ ' Goal3.:z',
918
+ ' Goal3."word"',
919
+ ' Goal2."word"',
920
+ ].join("\n")
921
+
922
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
923
+ 'Goal3.:m',
924
+ 'Goal3.:n',
925
+ 'Goal3.:z',
926
+ ' Goal1.:x',
927
+ ' Goal2.:y',
928
+ ' Goal2."word"',
929
+ ' Goal3."word"',
930
+ ].join("\n")
931
+
932
+ assert_equal 2, variable1.instantiations.size
933
+ assert_equal 2, variable2.instantiations.size
934
+ assert_equal 2, variable3.instantiations.size
935
+
936
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
937
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
938
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
939
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
940
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
941
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
942
+
943
+ assert_equal 'word', variable1.value
944
+ assert_equal 'word', variable2.value
945
+ assert_equal 'word', variable3.value
946
+ end
947
+
948
+ it 'should remove all instantiations case 1: remove variable1' do
949
+
950
+ variable1.remove
951
+
952
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil }, [
953
+ 'Goal1.:m',
954
+ 'Goal1.:n',
955
+ 'Goal1.:x',
956
+ ].join("\n")
957
+
958
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
959
+ 'Goal2.:m',
960
+ 'Goal2.:n',
961
+ 'Goal2.:y',
962
+ ' Goal2."word"',
963
+ ].join("\n")
964
+
965
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
966
+ 'Goal3.:m',
967
+ 'Goal3.:n',
968
+ 'Goal3.:z',
969
+ ' Goal3."word"',
970
+ ].join("\n")
971
+
972
+ assert_equal 0, variable1.instantiations.size
973
+ assert_equal 1, variable2.instantiations.size
974
+ assert_equal 1, variable3.instantiations.size
975
+
976
+ assert_nil variable1.instantiations[0]
977
+ assert_nil variable1.instantiations[1]
978
+ assert_nil variable2.instantiations[1]
979
+ assert_Instantiation variable2.instantiations[0], variable2, value1, nil, nil
980
+ assert_nil variable3.instantiations[1]
981
+ assert_Instantiation variable3.instantiations[0], variable3, value2, nil, nil
982
+
983
+ assert_equal variable1, variable1.value
984
+ assert_equal 'word', variable2.value
985
+ assert_equal 'word', variable3.value
986
+ end
987
+
988
+ it 'should remove all instantiations case 2: remove variable2' do
989
+
990
+ variable2.remove
991
+
992
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
993
+ 'Goal1.:m',
994
+ 'Goal1.:n',
995
+ 'Goal1.:x',
996
+ ' Goal3.:z',
997
+ ' Goal3."word"',
998
+ ].join("\n")
999
+
1000
+ assert_Goal_variables goal2, { m: nil, n: nil, y: nil }, [
1001
+ 'Goal2.:m',
1002
+ 'Goal2.:n',
1003
+ 'Goal2.:y',
1004
+ ].join("\n")
1005
+
1006
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1007
+ 'Goal3.:m',
1008
+ 'Goal3.:n',
1009
+ 'Goal3.:z',
1010
+ ' Goal1.:x',
1011
+ ' Goal3."word"',
1012
+ ].join("\n")
1013
+
1014
+ assert_equal 1, variable1.instantiations.size
1015
+ assert_equal 0, variable2.instantiations.size
1016
+ assert_equal 2, variable3.instantiations.size
1017
+
1018
+ assert_nil variable1.instantiations[1]
1019
+ assert_Instantiation variable1.instantiations[0], variable1, variable3, nil, nil
1020
+ assert_nil variable2.instantiations[0]
1021
+ assert_nil variable2.instantiations[1]
1022
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1023
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1024
+
1025
+ assert_equal 'word', variable1.value
1026
+ assert_equal variable2, variable2.value
1027
+ assert_equal 'word', variable3.value
1028
+ end
1029
+
1030
+ it 'should remove all instantiations case 3: remove variable3' do
1031
+
1032
+ variable3.remove
1033
+
1034
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1035
+ 'Goal1.:m',
1036
+ 'Goal1.:n',
1037
+ 'Goal1.:x',
1038
+ ' Goal2.:y',
1039
+ ' Goal2."word"',
1040
+ ].join("\n")
1041
+
1042
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1043
+ 'Goal2.:m',
1044
+ 'Goal2.:n',
1045
+ 'Goal2.:y',
1046
+ ' Goal1.:x',
1047
+ ' Goal2."word"',
1048
+ ].join("\n")
1049
+
1050
+ assert_Goal_variables goal3, { m: nil, n: nil, z: nil }, [
1051
+ 'Goal3.:m',
1052
+ 'Goal3.:n',
1053
+ 'Goal3.:z',
1054
+ ].join("\n")
1055
+
1056
+ assert_equal 1, variable1.instantiations.size
1057
+ assert_equal 2, variable2.instantiations.size
1058
+ assert_equal 0, variable3.instantiations.size
1059
+
1060
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1061
+ assert_nil variable1.instantiations[1]
1062
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1063
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1064
+ assert_nil variable3.instantiations[0]
1065
+ assert_nil variable3.instantiations[1]
1066
+
1067
+ assert_equal 'word', variable1.value
1068
+ assert_equal 'word', variable2.value
1069
+ assert_equal variable3, variable3.value
1070
+ end
1071
+
1072
+ end
1073
+
1074
+ describe '#uninstantiate' do
1075
+
1076
+ let(:variable1) { Variable.new :x, goal1 }
1077
+ let(:variable2) { Variable.new :y, goal2 }
1078
+ let(:variable3) { Variable.new :z, goal3 }
1079
+ let(:value1) { Value.new 'word', goal2 }
1080
+ let(:value2) { Value.new 'word', goal3 }
1081
+
1082
+ before do
1083
+ variable1.instantiate variable2
1084
+ variable1.instantiate variable3
1085
+ variable2.instantiate value1
1086
+ variable3.instantiate value2
1087
+
1088
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1089
+ 'Goal1.:m',
1090
+ 'Goal1.:n',
1091
+ 'Goal1.:x',
1092
+ ' Goal2.:y',
1093
+ ' Goal2."word"',
1094
+ ' Goal3.:z',
1095
+ ' Goal3."word"',
1096
+ ].join("\n")
1097
+
1098
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1099
+ 'Goal2.:m',
1100
+ 'Goal2.:n',
1101
+ 'Goal2.:y',
1102
+ ' Goal1.:x',
1103
+ ' Goal3.:z',
1104
+ ' Goal3."word"',
1105
+ ' Goal2."word"',
1106
+ ].join("\n")
1107
+
1108
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1109
+ 'Goal3.:m',
1110
+ 'Goal3.:n',
1111
+ 'Goal3.:z',
1112
+ ' Goal1.:x',
1113
+ ' Goal2.:y',
1114
+ ' Goal2."word"',
1115
+ ' Goal3."word"',
1116
+ ].join("\n")
1117
+
1118
+ assert_equal 2, variable1.instantiations.size
1119
+ assert_equal 2, variable2.instantiations.size
1120
+ assert_equal 2, variable3.instantiations.size
1121
+
1122
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1123
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1124
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1125
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1126
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1127
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1128
+
1129
+ assert_equal 'word', variable1.value
1130
+ assert_equal 'word', variable2.value
1131
+ assert_equal 'word', variable3.value
1132
+ end
1133
+
1134
+ it 'should remove all instantiations and values case 1' do
1135
+
1136
+ variable1.uninstantiate(goal1)
1137
+
1138
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1139
+ 'Goal1.:m',
1140
+ 'Goal1.:n',
1141
+ 'Goal1.:x',
1142
+ ' Goal2.:y',
1143
+ ' Goal2."word"',
1144
+ ' Goal3.:z',
1145
+ ' Goal3."word"',
1146
+ ].join("\n")
1147
+
1148
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1149
+ 'Goal2.:m',
1150
+ 'Goal2.:n',
1151
+ 'Goal2.:y',
1152
+ ' Goal1.:x',
1153
+ ' Goal3.:z',
1154
+ ' Goal3."word"',
1155
+ ' Goal2."word"',
1156
+ ].join("\n")
1157
+
1158
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1159
+ 'Goal3.:m',
1160
+ 'Goal3.:n',
1161
+ 'Goal3.:z',
1162
+ ' Goal1.:x',
1163
+ ' Goal2.:y',
1164
+ ' Goal2."word"',
1165
+ ' Goal3."word"',
1166
+ ].join("\n")
1167
+
1168
+ assert_equal 2, variable1.instantiations.size
1169
+ assert_equal 2, variable2.instantiations.size
1170
+ assert_equal 2, variable3.instantiations.size
1171
+
1172
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1173
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1174
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1175
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1176
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1177
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1178
+
1179
+ assert_equal 'word', variable1.value
1180
+ assert_equal 'word', variable2.value
1181
+ assert_equal 'word', variable3.value
1182
+ end
1183
+
1184
+ it 'should remove all instantiations and values case 2' do
1185
+
1186
+ variable1.uninstantiate(goal2)
1187
+
1188
+ assert_Goal_variables goal1, { m: nil, n: nil, x: nil }, [
1189
+ 'Goal1.:m',
1190
+ 'Goal1.:n',
1191
+ 'Goal1.:x',
1192
+ ].join("\n")
1193
+
1194
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1195
+ 'Goal2.:m',
1196
+ 'Goal2.:n',
1197
+ 'Goal2.:y',
1198
+ ' Goal2."word"',
1199
+ ].join("\n")
1200
+
1201
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1202
+ 'Goal3.:m',
1203
+ 'Goal3.:n',
1204
+ 'Goal3.:z',
1205
+ ' Goal1.:x',
1206
+ ' Goal3."word"',
1207
+ ].join("\n")
1208
+
1209
+ assert_equal 0, variable1.instantiations.size
1210
+ assert_equal 1, variable2.instantiations.size
1211
+ assert_equal 2, variable3.instantiations.size
1212
+
1213
+ assert_nil variable1.instantiations[0]
1214
+ assert_nil variable1.instantiations[1]
1215
+ assert_nil variable2.instantiations[1]
1216
+ assert_Instantiation variable2.instantiations[0], variable2, value1, nil, nil
1217
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1218
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1219
+
1220
+ assert_equal variable1, variable1.value
1221
+ assert_equal 'word', variable2.value
1222
+ assert_equal 'word', variable3.value
1223
+ end
1224
+
1225
+ it 'should remove all instantiations and values case 3' do
1226
+
1227
+ variable1.uninstantiate(goal3)
1228
+
1229
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1230
+ 'Goal1.:m',
1231
+ 'Goal1.:n',
1232
+ 'Goal1.:x',
1233
+ ' Goal2.:y',
1234
+ ' Goal2."word"',
1235
+ ].join("\n")
1236
+
1237
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1238
+ 'Goal2.:m',
1239
+ 'Goal2.:n',
1240
+ 'Goal2.:y',
1241
+ ' Goal1.:x',
1242
+ ' Goal2."word"',
1243
+ ].join("\n")
1244
+
1245
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1246
+ 'Goal3.:m',
1247
+ 'Goal3.:n',
1248
+ 'Goal3.:z',
1249
+ ' Goal3."word"',
1250
+ ].join("\n")
1251
+
1252
+ assert_equal 1, variable1.instantiations.size
1253
+ assert_equal 2, variable2.instantiations.size
1254
+ assert_equal 1, variable3.instantiations.size
1255
+
1256
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1257
+ assert_nil variable1.instantiations[1]
1258
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1259
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1260
+ assert_nil variable3.instantiations[1]
1261
+ assert_Instantiation variable3.instantiations[0], variable3, value2, nil, nil
1262
+
1263
+ assert_equal 'word', variable1.value
1264
+ assert_equal 'word', variable2.value
1265
+ assert_equal 'word', variable3.value
1266
+ end
1267
+
1268
+ it 'should remove all instantiations and values case 4' do
1269
+
1270
+ variable2.uninstantiate(goal1)
1271
+
1272
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1273
+ 'Goal1.:m',
1274
+ 'Goal1.:n',
1275
+ 'Goal1.:x',
1276
+ ' Goal3.:z',
1277
+ ' Goal3."word"',
1278
+ ].join("\n")
1279
+
1280
+ assert_Goal_variables goal2, { m: nil, n: nil, y: nil }, [
1281
+ 'Goal2.:m',
1282
+ 'Goal2.:n',
1283
+ 'Goal2.:y',
1284
+ ].join("\n")
1285
+
1286
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1287
+ 'Goal3.:m',
1288
+ 'Goal3.:n',
1289
+ 'Goal3.:z',
1290
+ ' Goal1.:x',
1291
+ ' Goal3."word"',
1292
+ ].join("\n")
1293
+
1294
+ assert_equal 1, variable1.instantiations.size
1295
+ assert_equal 0, variable2.instantiations.size
1296
+ assert_equal 2, variable3.instantiations.size
1297
+
1298
+ assert_nil variable1.instantiations[1]
1299
+ assert_Instantiation variable1.instantiations[0], variable1, variable3, nil, nil
1300
+ assert_nil variable2.instantiations[0]
1301
+ assert_nil variable2.instantiations[1]
1302
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1303
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1304
+
1305
+ assert_equal 'word', variable1.value
1306
+ assert_equal variable2, variable2.value
1307
+ assert_equal 'word', variable3.value
1308
+ end
1309
+
1310
+ it 'should remove all instantiations and values case 5' do
1311
+
1312
+ variable2.uninstantiate(goal2)
1313
+
1314
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1315
+ 'Goal1.:m',
1316
+ 'Goal1.:n',
1317
+ 'Goal1.:x',
1318
+ ' Goal2.:y',
1319
+ ' Goal3.:z',
1320
+ ' Goal3."word"',
1321
+ ].join("\n")
1322
+
1323
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1324
+ 'Goal2.:m',
1325
+ 'Goal2.:n',
1326
+ 'Goal2.:y',
1327
+ ' Goal1.:x',
1328
+ ' Goal3.:z',
1329
+ ' Goal3."word"',
1330
+ ].join("\n")
1331
+
1332
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1333
+ 'Goal3.:m',
1334
+ 'Goal3.:n',
1335
+ 'Goal3.:z',
1336
+ ' Goal1.:x',
1337
+ ' Goal2.:y',
1338
+ ' Goal3."word"',
1339
+ ].join("\n")
1340
+
1341
+ assert_equal 2, variable1.instantiations.size
1342
+ assert_equal 1, variable2.instantiations.size
1343
+ assert_equal 2, variable3.instantiations.size
1344
+
1345
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1346
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1347
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1348
+ assert_nil variable2.instantiations[1]
1349
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1350
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1351
+
1352
+ assert_equal 'word', variable1.value
1353
+ assert_equal 'word', variable2.value
1354
+ assert_equal 'word', variable3.value
1355
+ end
1356
+
1357
+ it 'should remove all instantiations and values case 6' do
1358
+
1359
+ variable2.uninstantiate(goal3)
1360
+
1361
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1362
+ 'Goal1.:m',
1363
+ 'Goal1.:n',
1364
+ 'Goal1.:x',
1365
+ ' Goal2.:y',
1366
+ ' Goal2."word"',
1367
+ ' Goal3.:z',
1368
+ ' Goal3."word"',
1369
+ ].join("\n")
1370
+
1371
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1372
+ 'Goal2.:m',
1373
+ 'Goal2.:n',
1374
+ 'Goal2.:y',
1375
+ ' Goal1.:x',
1376
+ ' Goal3.:z',
1377
+ ' Goal3."word"',
1378
+ ' Goal2."word"',
1379
+ ].join("\n")
1380
+
1381
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1382
+ 'Goal3.:m',
1383
+ 'Goal3.:n',
1384
+ 'Goal3.:z',
1385
+ ' Goal1.:x',
1386
+ ' Goal2.:y',
1387
+ ' Goal2."word"',
1388
+ ' Goal3."word"',
1389
+ ].join("\n")
1390
+
1391
+ assert_equal 2, variable1.instantiations.size
1392
+ assert_equal 2, variable2.instantiations.size
1393
+ assert_equal 2, variable3.instantiations.size
1394
+
1395
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1396
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1397
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1398
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1399
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1400
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1401
+
1402
+ assert_equal 'word', variable1.value
1403
+ assert_equal 'word', variable2.value
1404
+ assert_equal 'word', variable3.value
1405
+ end
1406
+
1407
+ it 'should remove all instantiations and values case 7' do
1408
+
1409
+ variable3.uninstantiate(goal1)
1410
+
1411
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1412
+ 'Goal1.:m',
1413
+ 'Goal1.:n',
1414
+ 'Goal1.:x',
1415
+ ' Goal2.:y',
1416
+ ' Goal2."word"',
1417
+ ].join("\n")
1418
+
1419
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1420
+ 'Goal2.:m',
1421
+ 'Goal2.:n',
1422
+ 'Goal2.:y',
1423
+ ' Goal1.:x',
1424
+ ' Goal2."word"',
1425
+ ].join("\n")
1426
+
1427
+ assert_Goal_variables goal3, { m: nil, n: nil, z: nil }, [
1428
+ 'Goal3.:m',
1429
+ 'Goal3.:n',
1430
+ 'Goal3.:z',
1431
+ ].join("\n")
1432
+
1433
+ assert_equal 1, variable1.instantiations.size
1434
+ assert_equal 2, variable2.instantiations.size
1435
+ assert_equal 0, variable3.instantiations.size
1436
+
1437
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1438
+ assert_nil variable1.instantiations[1]
1439
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1440
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1441
+ assert_nil variable3.instantiations[0]
1442
+ assert_nil variable3.instantiations[1]
1443
+
1444
+ assert_equal 'word', variable1.value
1445
+ assert_equal 'word', variable2.value
1446
+ assert_equal variable3, variable3.value
1447
+ end
1448
+
1449
+ it 'should remove all instantiations and values case 8' do
1450
+
1451
+ variable3.uninstantiate(goal2)
1452
+
1453
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1454
+ 'Goal1.:m',
1455
+ 'Goal1.:n',
1456
+ 'Goal1.:x',
1457
+ ' Goal2.:y',
1458
+ ' Goal2."word"',
1459
+ ' Goal3.:z',
1460
+ ' Goal3."word"',
1461
+ ].join("\n")
1462
+
1463
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1464
+ 'Goal2.:m',
1465
+ 'Goal2.:n',
1466
+ 'Goal2.:y',
1467
+ ' Goal1.:x',
1468
+ ' Goal3.:z',
1469
+ ' Goal3."word"',
1470
+ ' Goal2."word"',
1471
+ ].join("\n")
1472
+
1473
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1474
+ 'Goal3.:m',
1475
+ 'Goal3.:n',
1476
+ 'Goal3.:z',
1477
+ ' Goal1.:x',
1478
+ ' Goal2.:y',
1479
+ ' Goal2."word"',
1480
+ ' Goal3."word"',
1481
+ ].join("\n")
1482
+
1483
+ assert_equal 2, variable1.instantiations.size
1484
+ assert_equal 2, variable2.instantiations.size
1485
+ assert_equal 2, variable3.instantiations.size
1486
+
1487
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1488
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1489
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1490
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1491
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1492
+ assert_Instantiation variable3.instantiations[1], variable3, value2, nil, nil
1493
+
1494
+ assert_equal 'word', variable1.value
1495
+ assert_equal 'word', variable2.value
1496
+ assert_equal 'word', variable3.value
1497
+ end
1498
+
1499
+ it 'should remove all instantiations and values case 9' do
1500
+
1501
+ variable3.uninstantiate(goal3)
1502
+
1503
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
1504
+ 'Goal1.:m',
1505
+ 'Goal1.:n',
1506
+ 'Goal1.:x',
1507
+ ' Goal2.:y',
1508
+ ' Goal2."word"',
1509
+ ' Goal3.:z',
1510
+ ].join("\n")
1511
+
1512
+ assert_Goal_variables goal2, { m: nil, n: nil, y: 'word' }, [
1513
+ 'Goal2.:m',
1514
+ 'Goal2.:n',
1515
+ 'Goal2.:y',
1516
+ ' Goal1.:x',
1517
+ ' Goal3.:z',
1518
+ ' Goal2."word"',
1519
+ ].join("\n")
1520
+
1521
+ assert_Goal_variables goal3, { m: nil, n: nil, z: 'word' }, [
1522
+ 'Goal3.:m',
1523
+ 'Goal3.:n',
1524
+ 'Goal3.:z',
1525
+ ' Goal1.:x',
1526
+ ' Goal2.:y',
1527
+ ' Goal2."word"',
1528
+ ].join("\n")
1529
+
1530
+ assert_equal 2, variable1.instantiations.size
1531
+ assert_equal 2, variable2.instantiations.size
1532
+ assert_equal 1, variable3.instantiations.size
1533
+
1534
+ assert_Instantiation variable1.instantiations[0], variable1, variable2, nil, nil
1535
+ assert_Instantiation variable1.instantiations[1], variable1, variable3, nil, nil
1536
+ assert_Instantiation variable2.instantiations[0], variable1, variable2, nil, nil
1537
+ assert_Instantiation variable2.instantiations[1], variable2, value1, nil, nil
1538
+ assert_Instantiation variable3.instantiations[0], variable1, variable3, nil, nil
1539
+ assert_nil variable3.instantiations[1]
1540
+
1541
+ assert_equal 'word', variable1.value
1542
+ assert_equal 'word', variable2.value
1543
+ assert_equal 'word', variable3.value
1544
+ end
1545
+
1546
+ end
1547
+
1548
+ describe '#[]' do
1549
+
1550
+ it 'should return the element at the specified index' do
1551
+ variable = Variable.new :x, goal1
1552
+ value = Value.new [3,5,7,11,13,17], goal2
1553
+
1554
+ variable.instantiate value
1555
+
1556
+ assert_equal [3,5,7,11,13,17], variable.value
1557
+ assert_equal 13, variable[4]
1558
+ end
1559
+
1560
+ it 'should return nil for an out of range index' do
1561
+ variable = Variable.new :x, goal1
1562
+ value = Value.new [3,5,7,11,13,17], goal2
1563
+
1564
+ variable.instantiate value
1565
+
1566
+ assert_equal [3,5,7,11,13,17], variable.value
1567
+ assert_nil variable[200]
1568
+ end
1569
+
1570
+ it 'should return nil when the value is not an array' do
1571
+ variable = Variable.new :x, goal1
1572
+ value = Value.new 12, goal2
1573
+
1574
+ variable.instantiate value
1575
+
1576
+ assert_equal 12, variable.value
1577
+ assert_nil variable[0]
1578
+ end
1579
+
1580
+ it 'should return the head' do
1581
+ variable = Variable.new :x, goal1
1582
+ value = Value.new [3,5,7,11,13,17], goal2
1583
+
1584
+ variable.instantiate value
1585
+
1586
+ assert_equal [3,5,7,11,13,17], variable.value
1587
+ assert_equal 3, variable[:head]
1588
+ end
1589
+
1590
+ it 'should return the tail' do
1591
+ variable = Variable.new :x, goal1
1592
+ value = Value.new [3,5,7,11,13,17], goal2
1593
+
1594
+ variable.instantiate value
1595
+
1596
+ assert_equal [3,5,7,11,13,17], variable.value
1597
+ assert_equal [5,7,11,13,17], variable[:tail]
1598
+ end
1599
+
1600
+ end
1601
+
1602
+ describe '#variables' do
1603
+
1604
+ it 'should return an Array of itself' do
1605
+ variable = Variable.new :x, goal1
1606
+
1607
+ assert_equal [variable], variable.variables
1608
+ end
1609
+
1610
+ end
1611
+
1612
+ end
1613
+
1614
+ end