porolog 0.0.7 → 0.0.8

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