forked from gsdlab/chocosolver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialIntAnalyzer.java
More file actions
564 lines (510 loc) · 22.3 KB
/
Copy pathPartialIntAnalyzer.java
File metadata and controls
564 lines (510 loc) · 22.3 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
package org.clafer.ast.analysis;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import org.clafer.ast.AstAbstractClafer;
import org.clafer.ast.AstArithm;
import org.clafer.ast.AstBoolArithm;
import org.clafer.ast.AstBoolExpr;
import org.clafer.ast.AstChildRelation;
import org.clafer.ast.AstClafer;
import org.clafer.ast.AstConcreteClafer;
import org.clafer.ast.AstConstant;
import org.clafer.ast.AstConstraint;
import org.clafer.ast.AstGlobal;
import org.clafer.ast.AstJoin;
import org.clafer.ast.AstJoinRef;
import org.clafer.ast.AstMinus;
import org.clafer.ast.AstRef;
import org.clafer.ast.AstSetExpr;
import org.clafer.ast.AstSetTest;
import org.clafer.ast.AstTernary;
import org.clafer.ast.AstThis;
import org.clafer.ast.AstUnion;
import org.clafer.ast.AstUpcast;
import org.clafer.ast.AstUtil;
import org.clafer.collection.Either;
import org.clafer.common.Util;
import org.clafer.domain.Domain;
import org.clafer.domain.Domains;
import org.clafer.ontology.Concept;
import org.clafer.ontology.ConstraintDatabase;
import org.clafer.ontology.KnowledgeDatabase;
import org.clafer.ontology.Oracle;
import org.clafer.ontology.Path;
/**
*
* @author jimmy
*/
public class PartialIntAnalyzer {
private final Analysis analysis;
private final Map<AstClafer, Concept> conceptMap = new HashMap<>();
private final Map<Concept, AstClafer> claferMap = new HashMap<>();
private final KnowledgeDatabase knowledgeDatabase = new KnowledgeDatabase();
private PartialIntAnalyzer(Analysis analysis) {
this.analysis = analysis;
}
private Concept asConcept(AstClafer clafer) {
Concept concept = conceptMap.get(clafer);
if (concept == null) {
concept = knowledgeDatabase.newConcept(clafer.getName());
conceptMap.put(clafer, concept);
claferMap.put(concept, clafer);
}
return concept;
}
private AstClafer asClafer(Concept concept) {
return claferMap.get(concept);
}
public static Analysis analyze(Analysis analysis) {
PartialIntAnalyzer analyzer = new PartialIntAnalyzer(analysis);
analysis.getClafers().forEach(analyzer::analyzeClafer);
for (Entry<AstConstraint, AstBoolExpr> constraintExpr : analysis.getConstraintExprs().entrySet()) {
AstConstraint constraint = constraintExpr.getKey();
AstBoolExpr expr = constraintExpr.getValue();
if (constraint.isHard()) {
analyzer.analyzeConstraint(expr, constraint.getContext(), null);
}
}
Oracle oracle = analyzer.knowledgeDatabase.oracle();
do {
for (Entry<AstConstraint, AstBoolExpr> constraintExpr : analysis.getConstraintExprs().entrySet()) {
AstConstraint constraint = constraintExpr.getKey();
AstBoolExpr expr = constraintExpr.getValue();
if (constraint.isHard()) {
analyzer.analyzeConstraint(expr, constraint.getContext(), oracle);
}
}
} while (oracle.propagate());
Map<AstConcreteClafer, Domain[]> partialInts = analyzer.partialInts(oracle);
return analysis.setPartialIntsMap(partialInts);
}
/**
* Returns a map from Clafer to a set of covering paths reaching that
* Clafer.
*
* Each Clafer maps to a two dimensional array of Paths. The first dimension
* is for the id of the Clafer. The second dimension contains the set of
* covering paths for that (Clafer, id) pair.
*
* Uses dynamic programming, which works well unless of the hierarchy
* contains circularity.
*
* @return a map from Clafer to a set of covering paths reaching that Clafer
*/
private Map<AstClafer, Path[][]> pathsToClafers() {
Map<AstClafer, Path[][]> pathsToClafers = new HashMap<>(analysis.getClafers().size());
// For each potential (Clafer, id) pair, find all paths that can reach it.
for (Set<AstClafer> component : analysis.getClafersInParentAndSubOrder()) {
for (AstClafer clafer : component) {
PartialSolution partialSolution = analysis.getPartialSolution(clafer);
Path[][] paths = new Path[partialSolution.size()][];
if (clafer instanceof AstAbstractClafer) {
AstAbstractClafer abstractClafer = (AstAbstractClafer) clafer;
Offsets offsets = analysis.getOffsets(abstractClafer);
for (AstClafer sub : abstractClafer.getSubs()) {
int offset = offsets.getOffset(sub);
Path[][] subPaths = pathsToClafers.get(sub);
if (subPaths == null) {
// This can occur if circularity in hierarcy.
Arrays.fill(paths, new Path[]{new Path(asConcept(clafer))});
break;
}
System.arraycopy(subPaths, 0, paths, offset, subPaths.length);
}
} else {
if (clafer.hasParent()) {
Concept concept = asConcept(clafer);
Path[][] parentPaths = pathsToClafers.get(clafer.getParent());
if (parentPaths == null) {
// This can occur if circularity in hierarcy.
Arrays.fill(paths, new Path[]{new Path(asConcept(clafer))});
} else {
Path[][] childPaths = new Path[parentPaths.length][];
for (int i = 0; i < childPaths.length; i++) {
childPaths[i] = new Path[parentPaths[i].length];
for (int j = 0; j < childPaths[i].length; j++) {
childPaths[i][j] = parentPaths[i][j].append(concept);
}
}
for (int i = 0; i < paths.length; i++) {
int[] possibleParents = partialSolution.getPossibleParents(i);
int size = 0;
for (int possibleParent : possibleParents) {
size += childPaths[possibleParent].length;
}
Path[] pathsForId = new Path[size];
int offset = 0;
for (int possibleParent : possibleParents) {
System.arraycopy(
childPaths[possibleParent], 0,
pathsForId, offset,
childPaths[possibleParent].length);
offset += childPaths[possibleParent].length;
}
paths[i] = pathsForId;
}
}
} else {
Arrays.fill(paths, new Path[]{new Path(asConcept(clafer))});
}
}
pathsToClafers.put(clafer, paths);
}
}
return pathsToClafers;
}
private Map<AstConcreteClafer, Domain[]> partialInts(Oracle oracle) {
Map<AstClafer, Path[][]> pathsToClafers = pathsToClafers();
Map<AstConcreteClafer, Domain[]> partialInts = new HashMap<>();
for (AstConcreteClafer clafer : analysis.getConcreteClafers()) {
AstRef ref = AstUtil.getInheritedRef(clafer);
if (ref != null) {
Path[][] paths = pathsToClafers.get(clafer);
// NULL_DEREFERENCE
if (paths == null){
return partialInts;
}
Domain[] domains = new Domain[paths.length];
for (int i = 0; i < paths.length; i++) {
Domain domain = Domains.EmptyDomain;
for (Path path : paths[i]) {
Domain pathDomain = oracle.getAssignment(path);
if (pathDomain == null) {
domain = null;
break;
}
domain = domain.union(pathDomain);
}
domains[i] = domain;
}
partialInts.put(clafer, domains);
}
}
return partialInts;
}
private void analyzeClafer(AstClafer clafer) {
if (clafer.hasSuperClafer()) {
knowledgeDatabase.newIsA(asConcept(clafer), asConcept(clafer.getSuperClafer()));
}
clafer.getChildren().forEach(child
-> knowledgeDatabase.newHasA(asConcept(clafer), asConcept(child)));
}
private void analyzeConstraint(AstBoolExpr expr, AstClafer context, Oracle oracle) {
Either<Conjunction, Disjunction> conjOrDisj = analyzeExpr(expr, context, oracle);
if (conjOrDisj.isLeft()) {
if (oracle == null) {
conjOrDisj.getLeft().paths.forEach(x -> addEquality(x, context, knowledgeDatabase));
} else {
conjOrDisj.getLeft().paths.forEach(x -> addEquality(x, context, oracle));
}
} else {
addDisjunction(conjOrDisj.getRight(), context);
}
}
private void addDisjunction(Disjunction disj, AstClafer context) {
ConstraintDatabase[] disjunction = new ConstraintDatabase[disj.conjunctions.size()];
int i = 0;
for (Conjunction conjunction : disj.conjunctions) {
ConstraintDatabase database = KnowledgeDatabase.or();
conjunction.paths.forEach(x -> addEquality(x, context, database));
disjunction[i] = database;
i++;
}
assert i == disjunction.length;
knowledgeDatabase.newDisjunction(disjunction);
}
private void addEquality(Either<Subset, Equal> constraint, AstClafer context, Oracle oracle) {
if (constraint.isLeft()) {
Subset subset = constraint.getLeft();
Path sub = subset.sub;
Domain sup = subset.sup;
if (sub instanceof LocalPath || analysis.getGlobalCard(context).getLow() > 0) {
oracle.newAssignment(sub, sup);
}
}
}
private void addEquality(Either<Subset, Equal> constraint, AstClafer context, ConstraintDatabase constraintDatabase) {
if (constraint.isLeft()) {
Subset subset = constraint.getLeft();
Path sub = subset.sub;
Domain sup = subset.sup;
if (sub instanceof LocalPath || analysis.getGlobalCard(context).getLow() > 0) {
constraintDatabase.newAssignment(sub, sup);
}
} else {
Equal equal = constraint.getRight();
Path p1 = equal.p1;
Path p2 = equal.p2;
if (p1 instanceof LocalPath && p2 instanceof LocalPath) {
constraintDatabase.newLocalEquality(p1, p2);
} else if (p1 instanceof LocalPath || p2 instanceof LocalPath) {
Path localPath = p1 instanceof LocalPath ? p1 : p2;
Path globalPath = p1 instanceof LocalPath ? p2 : p1;
for (AstConcreteClafer sub : AstUtil.getConcreteSubs(asClafer(localPath.getContext()))) {
if (analysis.getGlobalCard(sub).getLow() > 0) {
constraintDatabase.newLocalEquality(
localPath.replaceContext(asConcept(sub)),
globalPath);
}
}
} else {
if (analysis.getGlobalCard(context).getLow() > 0) {
constraintDatabase.newLocalEquality(p1, p2);
}
}
}
}
private Either<Conjunction, Disjunction> analyzeExpr(
AstBoolExpr expr, AstClafer context, Oracle oracle) {
if (expr instanceof AstSetTest) {
AstSetTest compare = (AstSetTest) expr;
switch (compare.getOp()) {
case Equal:
List<Either<Subset, Equal>> paths = new ArrayList<>(2);
if (compare.getLeft() instanceof AstJoinRef) {
analyzeEqual((AstJoinRef) compare.getLeft(), compare.getRight(), context, oracle)
.ifPresent(paths::add);
}
if (compare.getRight() instanceof AstJoinRef) {
analyzeEqual((AstJoinRef) compare.getRight(), compare.getLeft(), context, oracle)
.ifPresent(paths::add);
}
return Either.left(new Conjunction(paths));
}
} else if (expr instanceof AstBoolArithm) {
AstBoolArithm boolArithm = (AstBoolArithm) expr;
switch (boolArithm.getOp()) {
case And:
List<Either<Subset, Equal>> paths = new ArrayList<>();
for (AstBoolExpr operand : boolArithm.getOperands()) {
Either<Conjunction, Disjunction> conjOrDisj = analyzeExpr(operand, context, oracle);
if (conjOrDisj.isLeft()) {
paths.addAll(conjOrDisj.getLeft().paths);
}
}
return Either.left(new Conjunction(paths));
case Or:
List<Conjunction> conjunctions = new ArrayList<>();
for (AstBoolExpr operand : boolArithm.getOperands()) {
Either<Conjunction, Disjunction> conjOrDisj = analyzeExpr(operand, context, oracle);
if (conjOrDisj.isLeft()) {
conjunctions.add(conjOrDisj.getLeft());
} else {
conjunctions.addAll(conjOrDisj.getRight().conjunctions);
}
}
return Either.right(new Disjunction(conjunctions));
}
}
return Either.left(new Conjunction(Collections.emptyList()));
}
private Optional<Either<Subset, Equal>> analyzeEqual(
AstJoinRef var, AstSetExpr value, AstClafer context, Oracle oracle) {
Optional<Path> varPath = asPath(var.getDeref(), context);
if (varPath.isPresent()) {
Optional<Domain> valueDomain = asDomain(value, context, oracle);
if (valueDomain.isPresent()) {
return Optional.of(Either.left(new Subset(varPath.get(), valueDomain.get())));
}
if (value instanceof AstJoinRef) {
Optional<Path> valuePath = asPath((AstJoinRef) value, context);
if (valuePath.isPresent()) {
return Optional.of(Either.right(new Equal(varPath.get(), valuePath.get())));
}
}
}
return Optional.empty();
}
private Optional<Path> asPath(AstJoinRef ast, AstClafer context) {
return asPath(ast.getDeref(), context);
}
private Optional<Path> asPath(AstSetExpr ast, AstClafer context) {
if (ast instanceof AstThis) {
return asPath((AstThis) ast, context);
}
if (ast instanceof AstGlobal) {
return asPath((AstGlobal) ast, context);
}
if (ast instanceof AstConstant) {
return asPath((AstConstant) ast, context);
}
if (ast instanceof AstJoin) {
return asPath((AstJoin) ast, context);
}
if (ast instanceof AstUpcast) {
return asPath((AstUpcast) ast, context);
}
return Optional.empty();
}
private Optional<Path> asPath(AstUpcast ast, AstClafer context) {
return asPath(ast.getBase(), context);
}
private Optional<Path> asPath(AstJoin ast, AstClafer context) {
if (ast.getRight() instanceof AstChildRelation) {
AstChildRelation right = (AstChildRelation) ast.getRight();
Optional<Path> left = asPath(ast.getLeft(), context);
return left.map(x -> x.append(asConcept(right.getChildType())));
}
return Optional.empty();
}
private Optional<Path> asPath(AstThis ast, AstClafer context) {
return Optional.of(new LocalPath(asConcept(context)));
}
private Optional<Path> asPath(AstGlobal ast, AstClafer context) {
return Optional.of(new Path(asConcept(ast.getType().getParent()), asConcept(ast.getType())));
}
private Optional<Path> asPath(AstConstant ast, AstClafer context) {
if (ast.getType().isClaferType()) {
return Optional.of(new Path(asConcept(ast.getType().getClaferType())));
}
return Optional.empty();
}
private Optional<Domain> asDomain(AstSetExpr ast, AstClafer context, Oracle oracle) {
if (ast instanceof AstConstant) {
return asDomain((AstConstant) ast, context, oracle);
}
if (ast instanceof AstArithm) {
return asDomain((AstArithm) ast, context, oracle);
}
if (ast instanceof AstMinus) {
return asDomain((AstMinus) ast, context, oracle);
}
if (ast instanceof AstUnion) {
return asDomain((AstUnion) ast, context, oracle);
}
if (ast instanceof AstTernary) {
return asDomain((AstTernary) ast, context, oracle);
}
if (ast instanceof AstUpcast) {
return asDomain((AstUpcast) ast, context, oracle);
}
if (oracle != null && ast instanceof AstJoinRef) {
AstJoinRef joinRef = (AstJoinRef) ast;
Optional<Path> path = asPath(joinRef.getDeref(), context);
return path.map(oracle::getAssignment);
}
return Optional.empty();
}
private Optional<Domain> asDomain(AstConstant ast, AstClafer context, Oracle oracle) {
if (ast.getValue().length > 0 && ast.getValue()[0].length == 1) {
int[] values = new int[ast.getValue().length];
for (int i = 0; i < values.length; i++) {
values[i] = ast.getValue()[i][0];
}
return Optional.of(Domains.enumDomain(values));
}
return Optional.empty();
}
private Optional<Domain> asDomain(AstArithm ast, AstClafer context, Oracle oracle) {
int[] operands = new int[ast.getOperands().length];
for (int i = 0; i < operands.length; i++) {
Optional<Domain> domain = asDomain(ast.getOperands()[i], context, oracle);
// TODO handle none constants
if (!domain.isPresent() || domain.get().size() != 1) {
return Optional.empty();
}
operands[i] = domain.get().getLowBound();
}
switch (ast.getOp()) {
case Add:
return Optional.of(Domains.constantDomain(Util.sum(operands)));
case Sub:
int difference = operands[0];
for (int i = 1; i < operands.length; i++) {
difference -= operands[i];
}
return Optional.of(Domains.constantDomain(difference));
case Mul:
int product = 1;
for (int operand : operands) {
product *= operand;
}
return Optional.of(Domains.constantDomain(product));
case Div:
int quotient = operands[0];
for (int i = 1; i < operands.length; i++) {
quotient /= operands[i];
}
return Optional.of(Domains.constantDomain(quotient));
default:
return Optional.empty();
}
}
private Optional<Domain> asDomain(AstMinus ast, AstClafer context, Oracle oracle) {
Optional<Domain> left = asDomain(ast.getExpr(), context, oracle);
return left.map(Domain::minus);
}
private Optional<Domain> asDomain(AstUnion ast, AstClafer context, Oracle oracle) {
Optional<Domain> left = asDomain(ast.getLeft(), context, oracle);
if (left.isPresent()) {
Optional<Domain> right = asDomain(ast.getRight(), context, oracle);
if (right.isPresent()) {
return Optional.of(left.get().union(right.get()));
}
}
return Optional.empty();
}
private Optional<Domain> asDomain(AstTernary ast, AstClafer context, Oracle oracle) {
Optional<Domain> consequent = asDomain(ast.getConsequent(), context, oracle);
if (consequent.isPresent()) {
Optional<Domain> alternative = asDomain(ast.getAlternative(), context, oracle);
if (alternative.isPresent()) {
return Optional.of(consequent.get().union(alternative.get()));
}
}
return Optional.empty();
}
private Optional<Domain> asDomain(AstUpcast ast, AstClafer context, Oracle oracle) {
Optional<Domain> base = asDomain(ast.getBase(), context, oracle);
if (base.isPresent()) {
int offset = analysis.getOffset(
ast.getTarget().getClaferType(),
analysis.getType(ast.getBase()).getClaferType());
return Optional.of(base.get().offset(offset));
}
return Optional.empty();
}
private class LocalPath extends Path {
public LocalPath(Concept... steps) {
super(steps);
}
@Override
protected Path newPath(Concept... steps) {
return new LocalPath(steps);
}
}
private static class Conjunction {
final List<Either<Subset, Equal>> paths;
Conjunction(List<Either<Subset, Equal>> paths) {
this.paths = paths;
}
}
private static class Disjunction {
final List<Conjunction> conjunctions;
public Disjunction(List<Conjunction> conjunctions) {
this.conjunctions = conjunctions;
}
}
private static class Subset {
final Path sub;
final Domain sup;
Subset(Path sub, Domain sup) {
this.sub = sub;
this.sup = sup;
}
}
private static class Equal {
final Path p1, p2;
Equal(Path p1, Path p2) {
this.p1 = p1;
this.p2 = p2;
}
}
}