-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathPptSlice.java.jpp
More file actions
861 lines (783 loc) · 31.1 KB
/
PptSlice.java.jpp
File metadata and controls
861 lines (783 loc) · 31.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#if 0
Do not attempt to compile this file with a Java compiler such as javac.
You first need to preprocess it with cpp, the C preprocessor.
The correct way to build the system is to run 'make'.
#endif
#if !(defined(ARITY1) || defined(ARITY2) || defined(ARITY3))
#error "One of ARITY1, ARITY2, or ARITY3 must be defined"
#endif
#if defined(ARITY1)
#define PPTSLICE1 PptSlice1
#define PPTSLICE1STRING "PptSlice1"
#define NUM_TM 2
#define ARITY 1
#elif defined(ARITY2)
#define PPTSLICE1 PptSlice2
#define PPTSLICE1STRING "PptSlice2"
#define NUM_TM 4
#define ARITY 2
#elif defined(ARITY3)
#define PPTSLICE1 PptSlice3
#define PPTSLICE1STRING "PptSlice3"
#define NUM_TM 8
#define ARITY 3
#else
#error "One of ARITY1, ARITY2, or ARITY3 must be defined"
#endif
// ***** This file is automatically generated from PptSlice.java.jpp
package daikon;
import static daikon.tools.nullness.NullnessUtil.castNonNullDeep;
import daikon.inv.*;
#ifdef ARITY1
import daikon.inv.unary.*;
import daikon.inv.unary.scalar.*;
import daikon.inv.unary.sequence.*;
import daikon.inv.unary.string.*;
import daikon.inv.unary.stringsequence.*;
#endif
#ifdef ARITY2
import daikon.inv.binary.*;
import daikon.inv.binary.twoScalar.*;
import daikon.inv.binary.twoSequence.*;
import daikon.inv.binary.twoString.*;
import daikon.inv.binary.sequenceScalar.*;
#endif
#ifdef ARITY3
import daikon.inv.ternary.*;
import daikon.inv.ternary.threeScalar.*;
#endif
import daikon.suppress.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.checkerframework.checker.initialization.qual.UnknownInitialization;
import org.checkerframework.checker.interning.qual.Interned;
import org.checkerframework.checker.lock.qual.GuardSatisfied;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.plumelib.util.ArraysPlume;
import org.plumelib.util.Intern;
import typequals.prototype.qual.Prototype;
import typequals.prototype.qual.NonPrototype;
/** Contains all of the invariants over a particular set of ARITY variables. */
public final class PPTSLICE1 extends PptSlice {
static final long serialVersionUID = 20040921L;
/** Debug tracer. */
public static final Logger debugSpecific = Logger.getLogger("daikon." + PPTSLICE1STRING);
public static final Logger debugMerge = Logger.getLogger("daikon.PptSlice.merge");
/** Create a new PPTSLICE1. The var_infos must be in varinfo_index order. */
public PPTSLICE1(PptTopLevel parent, VarInfo[] var_infos) {
super(parent, var_infos);
assert var_infos.length == ARITY;
if (debug.isLoggable(Level.FINE) || debugSpecific.isLoggable(Level.FINE)) {
debug.info("Created " + PPTSLICE1STRING + " " + this.name());
}
if (Debug.logOn()) {
Debug.log(getClass(), this, "Created");
}
}
#if defined(ARITY1)
PptSlice1(PptTopLevel parent, VarInfo var_info) {
this(parent, new VarInfo[] {var_info});
}
#elif defined(ARITY2)
PptSlice2(PptTopLevel parent, VarInfo var_info1, VarInfo var_info2) {
this(parent, new VarInfo[] {var_info1, var_info2});
}
#elif defined(ARITY3)
PptSlice3(PptTopLevel parent, VarInfo var_info1, VarInfo var_info2, VarInfo var_info3) {
this(parent, new VarInfo[] {var_info1, var_info2, var_info3});
}
#endif
@Override
public final int arity(
@UnknownInitialization(PptSlice.class) PPTSLICE1 this) {
return ARITY;
}
/**
* Creates all of the invariants that are appropriate for this slice. No invariants are
* created unless the variables in the slice are compatible. If the variables are compatible,
* invariants that match the type of the slices variables are created.
*/
@Override
public void instantiate_invariants() {
instantiate_invariants(Daikon.proto_invs);
}
/**
* Creates all of the invariants that are appropriate for this slice based on the list of
* invariants passed in. No invariants are created unless the variables in the slice are
* compatible. If the variables are compatible, invariants that match the type of the slices
* variables are created.
*/
public void instantiate_invariants(List<@Prototype Invariant> proto_invs) {
if (Debug.logOn()) {
log("instantiate invariants");
}
// Do nothing if the variables aren't compatible
#if defined (ARITY1)
// (TODO: need to compare element and index comparabilities.)
#elif defined (ARITY2)
boolean compatible = true;
VarInfo v1 = var_infos[0];
VarInfo v2 = var_infos[1];
if (v1.rep_type.isArray() == v2.rep_type.isArray()) {
compatible = v1.compatible(v2);
} else if (v1.rep_type.isArray()) {
compatible = v1.eltsCompatible(v2);
} else {
compatible = v2.eltsCompatible(v1);
}
if (!compatible) {
Debug.log(this.getClass(), this, "not created - incompatible");
return;
}
#elif defined (ARITY3)
VarInfo v1 = var_infos[0];
VarInfo v2 = var_infos[1];
VarInfo v3 = var_infos[2];
if (!v1.compatible(v2)) {
return;
}
if (!v2.compatible(v3)) {
return;
}
assert v1.compatible(v3);
#else
#error "Bad Arity"
#endif
// Instantiate each invariant that is valid over those types
for (@Prototype Invariant proto : proto_invs) {
// Skip invariant if the types are not appropriate
if (!proto.valid_types(var_infos) || !proto.instantiate_ok(var_infos)) {
// Debug.log (proto.getClass(), this, "not created-types not valid");
continue;
}
// Skip invariant if it is suppressed. Note that this will work
// even though we haven't instantiated all of the invariants for this
// slice yet because it will check constant values.
NISuppressionSet ss = proto.get_ni_suppressions();
if (NIS.dkconfig_enabled && (ss != null) && !ss.is_instantiate_ok(this)) {
if (Debug.logOn()) { // avoid stringify if not logging
Debug.log(proto.getClass(), this, "not created - suppressed " + ss);
}
continue;
}
// Instantiate the invariant and add it to the list. Null should
// never be returned because we check instantiate_ok() above and
// only enabled invariants are in this list.
@SuppressWarnings("nullness") // application invariant, see comment above
@NonNull Invariant inv = proto.instantiate(this);
addInvariant(inv);
if (Debug.logOn()) {
inv.log("Created invariant %s ss = %s", inv.format(), ss);
}
}
}
// These accessors are for abstract methods declared in Ppt
/** Returns the number of (non-missing) samples observed at this slice. */
@Override
public int num_samples(@UnknownInitialization @GuardSatisfied PPTSLICE1 this) {
if (parent == null || var_infos == null) { // handle not-yet-initialized slices
return 0;
}
// return an approximation
#if defined(ARITY1)
int num_slice_samples = parent.num_samples(var_infos[0]);
#elif defined(ARITY2)
int num_slice_samples = parent.num_samples(var_infos[0], var_infos[1]);
#elif defined(ARITY3)
int num_slice_samples = parent.num_samples(var_infos[0], var_infos[1],
var_infos[2]);
#endif
return num_slice_samples;
}
/**
* Returns an upper bound on the number of distinct values observed at this slice. This is not the
* number of samples observed.
*/
@Override
public int num_values() {
// return an approximation
#if defined(ARITY1)
int num_values = parent.num_values(var_infos[0]);
#elif defined(ARITY2)
int num_values = parent.num_values(var_infos[0], var_infos[1]);
#elif defined(ARITY3)
int num_values = parent.num_values(var_infos[0], var_infos[1],
var_infos[2]);
#endif
return num_values;
}
/**
* This procedure accepts a sample (a ValueTuple), extracts the values from it, casts them to the
* proper types, and passes them along to the invariants proper. (The invariants accept typed
* values rather than a ValueTuple that encapsulates objects of any type whatever.)
*/
@Override
public List<Invariant> add(ValueTuple full_vt, int count) {
assert invs.size() > 0;
for (Invariant inv : invs) {
assert inv != null;
}
#if defined(ARITY1)
VarInfo vi1 = var_infos[0];
#elif defined(ARITY2)
VarInfo vi1 = var_infos[0];
VarInfo vi2 = var_infos[1];
#elif defined(ARITY3)
VarInfo vi1 = var_infos[0];
VarInfo vi2 = var_infos[1];
VarInfo vi3 = var_infos[2];
#endif
// If any var has encountered out of array bounds values,
// stop all invariants in this slice. The presumption here is that
// an index out of bounds implies that the derived variable (eg a[i])
// doesn't really make any sense (essentially that i is not a valid
// index for a). Invariants on the derived variable are thus not
// relevant
for (int i = 0; i < var_infos.length; i++) {
if (var_infos[i].missingOutOfBounds()) {
List<Invariant> result = new ArrayList<>();
for (Invariant inv : invs) {
if (PrintInvariants.print_discarded_invariants) {
DiscReasonMap.put(
inv,
DiscardCode.bad_sample,
var_infos[i].name() + " array index was out of bounds");
}
inv.falsify();
result.add(inv);
if (Invariant.logOn()) {
inv.log("destroyed because %s array index out of bounds", var_infos[i].name());
}
}
if (VarInfo.debugMissing.isLoggable(Level.FINE)) {
VarInfo.debugMissing.fine(
"Removing slice "
+ this
+ " because var "
+ var_infos[i].name()
+ " array index out of bounds");
}
return result;
}
}
int mod1 = full_vt.getModified(vi1);
if (mod1 == ValueTuple.MISSING_FLOW || mod1 == ValueTuple.MISSING_NONSENSICAL) {
return emptyInvList;
}
if (mod1 == ValueTuple.STATIC_CONSTANT) {
assert vi1.is_static_constant;
mod1 = ((num_samples() == 0) ? ValueTuple.MODIFIED : ValueTuple.UNMODIFIED);
}
#if defined(ARITY2) || defined(ARITY3)
int mod2 = full_vt.getModified(vi2);
if (mod2 == ValueTuple.MISSING_FLOW || mod2 == ValueTuple.MISSING_NONSENSICAL) {
return emptyInvList;
}
if (mod2 == ValueTuple.STATIC_CONSTANT) {
assert vi2.is_static_constant;
mod2 = ((num_samples() == 0)
? ValueTuple.MODIFIED : ValueTuple.UNMODIFIED);
}
#endif
#if defined(ARITY3)
int mod3 = full_vt.getModified(vi3);
if (mod3 == ValueTuple.MISSING_FLOW || mod3 == ValueTuple.MISSING_NONSENSICAL) {
return emptyInvList;
}
if (mod3 == ValueTuple.STATIC_CONSTANT) {
assert vi3.is_static_constant;
mod3 = ((num_samples() == 0) ? ValueTuple.MODIFIED : ValueTuple.UNMODIFIED);
}
#endif
Object val1 = full_vt.getValue(vi1);
assert Intern.isInterned(val1) : "obj " + val1 + " class " + val1.getClass();
#if defined(ARITY2) || defined(ARITY3)
Object val2 = full_vt.getValue(vi2);
assert Intern.isInterned(val2);
#endif
#if defined(ARITY3)
Object val3 = full_vt.getValue(vi3);
assert Intern.isInterned(val3);
#endif
// Supply the new values to all the invariant objects.
assert (mod1 == vi1.getModified(full_vt))
|| ((vi1.getModified(full_vt) == ValueTuple.STATIC_CONSTANT)
&& ((mod1 == ValueTuple.UNMODIFIED) || (mod1 == ValueTuple.MODIFIED)));
#if defined(ARITY1)
List<Invariant> weakened_invs = add_val_bu(val1, mod1, count);
#elif defined(ARITY2)
List<Invariant> weakened_invs = add_val_bu(val1, val2, mod1, mod2, count);
#elif defined(ARITY3)
List<Invariant> weakened_invs = add_val_bu(val1, val2, val3, mod1, mod2,
mod3, count);
#endif
return weakened_invs;
}
#if defined (ARITY1)
public List<Invariant> add_val_bu(@Interned Object val1, int mod1, int count) {
assert mod1 != ValueTuple.MISSING_FLOW && mod1 != ValueTuple.MISSING_NONSENSICAL;
List<Invariant> result = new ArrayList<>();
for (Invariant invariant : invs) {
UnaryInvariant inv = (UnaryInvariant) invariant;
if (inv.is_false()) {
continue;
}
InvariantStatus status = inv.add(val1, mod1, count);
if (status == InvariantStatus.FALSIFIED) {
inv.falsify();
result.add(inv);
} else if (status == InvariantStatus.WEAKENED) {
result.add(inv);
}
if (PrintInvariants.print_discarded_invariants && inv.is_false()) {
DiscReasonMap.put(
inv,
DiscardCode.bad_sample,
"Falsified from sample: "
+ var_infos[0].name()
+ " = "
+ Debug.toString(val1));
}
}
return result;
}
#elif defined (ARITY2)
public List<Invariant> add_val_bu(@Interned Object val1, @Interned Object val2,
int mod1, int mod2, int count) {
assert (mod1 != ValueTuple.MISSING_FLOW
&& mod1 != ValueTuple.MISSING_NONSENSICAL)
&& (mod2 != ValueTuple.MISSING_FLOW
&& mod2 != ValueTuple.MISSING_NONSENSICAL);
List<Invariant> result = new ArrayList<>();
boolean array1 = var_infos[0].rep_type.isArray();
boolean array2 = var_infos[1].rep_type.isArray();
if (array2 && ! array1) {
for (Invariant invariant : invs) {
BinaryInvariant inv = (BinaryInvariant) invariant;
if (inv.is_false()) {
continue;
}
InvariantStatus status = inv.add(val2, val1, mod1, count);
if (status == InvariantStatus.FALSIFIED) {
inv.falsify();
result.add(inv);
} else if (status == InvariantStatus.WEAKENED) {
result.add(inv);
}
if (PrintInvariants.print_discarded_invariants && inv.is_false()) {
DiscReasonMap.put(inv, DiscardCode.bad_sample,
"Falsified from sample: "
+ var_infos[0].name()
+ " = " + val1 + "," + var_infos[1].name()
+ " = " + Debug.toString(val2));
}
}
} else {
for (Invariant invariant : invs) {
BinaryInvariant inv = (BinaryInvariant) invariant;
if (inv.is_false()) {
continue;
}
InvariantStatus status = inv.add(val1, val2, mod1, count);
if (status == InvariantStatus.FALSIFIED) {
inv.falsify();
result.add(inv);
} else if (status == InvariantStatus.WEAKENED) {
result.add(inv);
}
if (PrintInvariants.print_discarded_invariants && inv.is_false()) {
if (array1
&& array2
&& !( (inv instanceof SeqSeqIntEqual)
|| (inv instanceof SeqSeqFloatEqual)
|| (inv instanceof SeqSeqStringEqual) )
&& ArraysPlume.length(val1) != ArraysPlume.length(val2)) {
// No need to print out two potentially huge arrays if the
// reason for discard was because of different array lengths
DiscReasonMap.put(inv, DiscardCode.bad_sample,
"Samples seen with different array lengths");
} else {
DiscReasonMap.put(inv, DiscardCode.bad_sample,
"Falsified from sample: " + var_infos[0].name()
+ " = " + Debug.toString(val1)
+ "," + var_infos[1].name()
+ " = " + Debug.toString(val2));
}
}
}
}
return result;
}
#elif defined (ARITY3)
public List<Invariant> add_val_bu(@Interned Object val1, @Interned Object val2, @Interned Object val3,
int mod1, int mod2, int mod3, int count) {
assert (mod1 != ValueTuple.MISSING_FLOW
&& mod1 != ValueTuple.MISSING_NONSENSICAL)
&& (mod2 != ValueTuple.MISSING_FLOW
&& mod2 != ValueTuple.MISSING_NONSENSICAL)
&& (mod3 != ValueTuple.MISSING_FLOW
&& mod3 != ValueTuple.MISSING_NONSENSICAL);
List<Invariant> result = new ArrayList<>();
for (Invariant invariant : invs) {
TernaryInvariant inv = (TernaryInvariant) invariant;
if (inv.is_false()) {
continue;
}
InvariantStatus status = inv.add(val1, val2, val3, mod1, count);
if (status == InvariantStatus.FALSIFIED) {
inv.falsify();
result.add(inv);
} else if (status == InvariantStatus.WEAKENED) {
result.add(inv);
}
if (PrintInvariants.print_discarded_invariants && inv.is_false()) {
// Currently there are no ternary invariants with arrays, but if
// there are in the future, I don't want this to break.
DiscReasonMap.put(inv, DiscardCode.bad_sample,
"Falsified from sample: " + var_infos[0].name() + " = "
+ Debug.toString(val1)
+ "," + var_infos[1].name() + " = "
+ Debug.toString(val2)
+ "," + var_infos[2].name() + " = "
+ Debug.toString(val3));
}
}
return result;
}
#endif
@Override
public void addInvariant(Invariant invariant) {
assert invariant != null;
// assert invariant.ppt == this;
if (Debug.dkconfig_internal_check) {
// Don't add a check here to ensure that the invairant is not suppressed
// in some cases (see NIS.apply_samples), a suppressed invariant is
// added and them later removed when the suppression is found
// if (invariant.is_ni_suppressed()) {
// System.out.printf("suppressed invariant %s is being added to ppt %s "
// + "with %d samples%n", invariant.format(), this,
// this.num_samples());
// NISuppressionSet ss = invariant.get_ni_suppressions();
// ss.suppressed (invariant.ppt);
// System.out.printf("suppression = %s%n", ss);
// throw new Error();
// }
}
invs.add(invariant);
Global.instantiated_invariants++;
if (Invariant.logOn()) {
invariant.log("Instantiated %s", invariant.format());
}
}
/**
* Copy invariants from this slice to a new slice over the variables argNewVarInfos. The new slice
* should not already exist.
*/
@Override
protected PptSlice cloneAndPivot(VarInfo[] argNewVarInfos) {
// Sort the VarInfos by var_index and build a matching permutation
// from the current order to the new order
VarInfo[] vis_sorted = argNewVarInfos.clone();
Arrays.sort(vis_sorted, VarInfo.IndexComparator.getInstance());
int[] permutation = PptTopLevel.build_permute(argNewVarInfos, vis_sorted);
// Assert sorted
for (int i = 0; i < ARITY - 1; i++) {
assert vis_sorted[i].varinfo_index <= vis_sorted[i + 1].varinfo_index;
}
assert ArraysPlume.fnIsPermutation(permutation);
// Assert that the permutation represents the rearrangement
for (int i = 0; i < ARITY; i++) {
// the variable that used to be at position "i" is now found at
// position permutation[i].
VarInfo oldvi = argNewVarInfos[i];
VarInfo newvi = vis_sorted[permutation[i]];
assert oldvi == newvi;
}
// The new slice should not already exist.
assert parent.findSlice(vis_sorted) == null;
// Why not just clone? Because then index order wouldn't be
// preserved
PPTSLICE1 result = new PPTSLICE1(this.parent, vis_sorted);
// re-parent the invariants and copy them out.
List<Invariant> newInvs = new ArrayList<>();
for (Invariant inv : invs) {
assert inv.ppt == this;
Invariant newInv = inv.transfer(result, permutation);
newInvs.add(newInv);
assert newInv != inv;
assert newInv.ppt == result;
assert inv.ppt == this;
}
if (Debug.logOn()) {
result.log(
"Copied "
+ newInvs.size()
+ " invariants from "
+ this.name()
+ " with "
+ invs.size()
+ " invariants");
}
result.invs.addAll(newInvs);
if (PptSliceEquality.debug.isLoggable(Level.FINE)) {
PptSliceEquality.debug.fine("cloneAndPivot: newInvs " + invs);
}
return result;
}
/**
* Creates invariants at this ppt by merging invariants from each of its children. An invariant
* must exist at each of the children in order for it to be created here (at the parent).
* Additionally, some invariants have state information that must be merged. This is done by the
* invariant itself.
*
* <p>The basic steps are:
*
* <ol>
* <li>Find all of the child invariants. These are the invariants in the matching slice of each
* child.
* <li>For each invariant class, build a list of all of the invariants of that class. Note that
* some invariant classes (eg, functionBinary) contain distinct invariants, each of which
* must be merged separately. See Invariant.Match for more information concerning what makes
* an invariant the 'same'
* <li>Each invariant that is found at each of the children is then merged to possibly create a
* parent invariant.
* </ol>
*/
public void merge_invariants() {
boolean debug = parent.name()
.startsWith(
"com.rolemodelsoft.drawlet.basics.AbstractFigure.addPropertyChangeListener(java.beans.PropertyChangeListener)");
if (debugMerge.isLoggable(Level.FINE)) {
debugMerge.fine("entering merge_invariants for " + name());
}
// List of all invariants found over all of the children
List<Invariant> all_invs = new ArrayList<>();
// Keep count of the number of valid children processed. An invariant
// must be found at each valid child in order to exist at the parent.
// A valid child is one that has received samples and has a corresponding
// variable for each parent variable
int valid_child_count = 0;
// Loop through all of the children of our top level parent
child_loop:
for (PptRelation rel : parent.children) {
PptTopLevel ppt = rel.child;
// Skip any children that have not seen any samples
if (ppt.num_samples() == 0) {
if (debugMerge.isLoggable(Level.FINE)) {
debugMerge.fine("-- slice ignored (no samples) " + ppt.name());
}
continue;
}
// Child variable info
/*NNC:@MonotonicNonNull*/ VarInfo[] cvis = new VarInfo[var_infos.length];
/*NNC:@MonotonicNonNull*/ VarInfo[] cvis_sorted = new VarInfo[var_infos.length];
// Build the corresponding array of VarInfos for the child. If any
// of the vars don't exist in this child, skip the child (since we
// won't have data for each variable).
for (int j = 0; j < var_infos.length; j++) {
VarInfo pv = var_infos[j];
VarInfo cv = rel.childVar(pv);
if (cv == null) {
continue child_loop;
}
cvis[j] = cv.canonicalRep();
cvis_sorted[j] = cv.canonicalRep();
}
cvis = castNonNullDeep(cvis); // https://tinyurl.com/cfissue/986
cvis_sorted = castNonNullDeep(cvis_sorted); // https://tinyurl.com/cfissue/986
// If any of the child variables have always been missing, this
// particular slice in the child received no samples. If dynamic
// constants are enabled, the slice will have never been created.
// These slices can be skipped unless they contain a missing out of
// bound var. Out of bounds variables destroy all invariants in
// the slice (since the variable is deemed to be nonsensical)
if (slice_missing(ppt, cvis)) {
if (debugMerge.isLoggable(Level.FINE)) {
debugMerge.fine(
"-- slice ignored (missing) " + ppt.name() + " vars " + Arrays.toString(cvis_sorted));
}
continue;
}
// The child variables must be sorted by their index (in the child)
Arrays.sort(cvis_sorted, VarInfo.IndexComparator.getInstance());
// Keep track of the number of valid children
valid_child_count++;
// Find the corresponding slice. If the slice does not exist or
// has no invariants, there can be no invariants to merge (since
// invariants must exist at each child to exist at the parent)
PPTSLICE1 cslice = (PPTSLICE1) ppt.findSlice(cvis_sorted);
if ((cslice == null) || (cslice.invs.size() == 0)) {
if (Debug.logOn()) {
this.log(
"slice not found "
+ ppt.name()
+ " "
+ Arrays.toString(cvis_sorted)
+ " num_samples= "
+ ppt.num_samples()
+ " ppt.constants = "
+ ppt.constants);
}
if (debugMerge.isLoggable(Level.FINE)) {
debugMerge.fine(
"-- slice not found " + ppt.name() + " vars " + Arrays.toString(cvis_sorted));
}
return;
}
// // Update sample count info
// mod_samples += cslice.mod_samples;
// unmod_samples += cslice.unmod_samples;
// Build the permutation array from child to parent slice
int[] permute = PptTopLevel.build_permute(cvis_sorted, cvis);
// Debug print child vars and permute to parent
if (debugMerge.isLoggable(Level.FINE)) {
debugMerge.fine("-- Processing child " + ppt.name() + " (" + rel.getRelationType() + ")");
debugMerge.fine("-- -- child vars = " + Arrays.toString(cvis_sorted));
debugMerge.fine("-- -- parent vars = " + Arrays.toString(var_infos));
debugMerge.fine("-- -- permute = " + Arrays.toString(permute));
}
// Add each invariant (permuted to match the parent varinfos)
// to our list of invariants.
for (Invariant orig_inv : cslice.invs) {
Invariant inv = orig_inv.clone_and_permute(permute);
all_invs.add(inv);
if (Invariant.logOn()) {
/*NNC:@MonotonicNonNull*/ VarInfo[] child_vars = new VarInfo[var_infos.length];
for (int k = 0; k < var_infos.length; k++) {
VarInfo pv = var_infos[k];
@SuppressWarnings("nullness")
@NonNull VarInfo cv = rel.childVar(pv);
assert cv != null;
child_vars[k] = cv.canonicalRep();
}
child_vars = castNonNullDeep(child_vars); // https://tinyurl.com/cfissue/986
orig_inv.log("org inv");
inv.log(
"Created %s from %s using permute %s cvis_sorted = %s cvis = %s for ppt %s",
inv,
orig_inv,
Arrays.toString(permute),
Arrays.toString(cvis_sorted),
Arrays.toString(child_vars),
parent.name());
}
}
}
if (debug) {
System.out.println("Found " + all_invs.size() + " invariants to merge");
System.out.println(all_invs);
}
log("Found " + all_invs.size() + " invariants to merge");
if (debugMerge.isLoggable(Level.FINE) && (valid_child_count == 0)) {
debugMerge.fine("-- No valid children found");
}
// For each invariant found, find the list of invariants of the
// same type (type corresponds basically but not exactly to the
// invariants class) and add the invariant to that list.
// Invariant.Match.equals() defines if two invariants are of the
// same 'type' for the purpose of merging invariants.
Map<Invariant.Match, List<Invariant>> inv_map =
new LinkedHashMap<>();
for (Invariant inv : all_invs) {
Invariant.Match imatch = new Invariant.Match(inv);
List<Invariant> invs = inv_map.get(imatch);
if (Invariant.logOn()) {
inv.log("Adding %s to %s invs list %s", inv.format(), name(), invs);
}
if (invs == null) {
invs = new ArrayList<Invariant>();
inv_map.put(imatch, invs);
}
invs.add(inv);
}
if (debug) {
System.out.println("inv_map = " + inv_map);
}
// Attempt to create a parent invariant for each invariant that
// appeared at each valid child. Note that some invariants will
// not exist at the parent even if they exist at each child (eg,
// LinearBinary)
for (List<Invariant> child_invs : inv_map.values()) {
if (child_invs.size() > valid_child_count) {
// this shouldn't happen
System.out.println(
"Found "
+ child_invs.size()
+ " invariants at "
+ name()
+ " ("
+ valid_child_count
+ " children)");
for (Invariant child_inv : child_invs) {
System.out.printf(
"-- Invariant = '%s' [%s] @%s%n",
child_inv.repr(), child_inv.getClass(), child_inv.ppt);
}
assert child_invs.size() <= valid_child_count;
}
if (child_invs.size() == valid_child_count) {
Invariant first = child_invs.get(0);
if (debug) {
System.out.printf("Attempting merge of %s invariants into ppt %s%n", child_invs.size(), name());
}
if (Debug.logOn()) {
first.log("Attempting merge of %s invariants into ppt %s", child_invs.size(), name());
}
Invariant parent_inv = first.merge(child_invs, this);
if (parent_inv != null) {
invs.add(parent_inv);
if (debug) {
System.out.printf("Merge successful of %s into %s%n", parent_inv.format(), name());
}
if (Debug.logOn()) {
parent_inv.log("Merge successful of %s into %s", parent_inv.format(), name());
}
}
} else {
if (Debug.logOn()) {
Invariant inv = child_invs.get(0);
if (debug) {
System.out.printf(
"Not merging invariant into %s, Found %s child invariants in %s children%n",
name(), child_invs.size(), valid_child_count);
}
inv.log(
"Not merging invariant into %s, Found %s child invariants in %s children",
name(), child_invs.size(), valid_child_count);
}
}
}
}
/**
* Returns whether or not the slice is missing due to having one or more of its variables always
* missing. This returns true only for missing flow and/or missing nonsensical. Out of Bounds is
* treated differently since it destroys all of its invariants.
*/
private boolean slice_missing(PptTopLevel ppt, VarInfo[] vis) {
if (ppt.constants != null) {
#if defined(ARITY1)
if (ppt.constants.is_missing(vis[0]) && !vis[0].missingOutOfBounds()) {
return true;
}
#elif defined(ARITY2)
if ((ppt.constants.is_missing(vis[0]) || ppt.constants.is_missing(vis[1]))
&& !vis[0].missingOutOfBounds()
&& !vis[1].missingOutOfBounds()) {
return true;
}
#elif defined(ARITY3)
if ((ppt.constants.is_missing(vis[0])
|| ppt.constants.is_missing(vis[1])
|| ppt.constants.is_missing(vis[2]))
&& !vis[0].missingOutOfBounds()
&& !vis[1].missingOutOfBounds()
&& !vis[2].missingOutOfBounds()) {
return true;
}
#endif
}
return false;
}
}