forked from typetools/checker-framework-inference
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMaxSatFormatTranslator.java
More file actions
81 lines (68 loc) · 3.21 KB
/
Copy pathMaxSatFormatTranslator.java
File metadata and controls
81 lines (68 loc) · 3.21 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
package checkers.inference.solver.backend.maxsat;
import org.checkerframework.javacutil.AnnotationMirrorMap;
import org.sat4j.core.VecInt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import checkers.inference.solver.backend.AbstractFormatTranslator;
import checkers.inference.solver.backend.encoder.ConstraintEncoderFactory;
import checkers.inference.solver.backend.maxsat.encoder.MaxSATConstraintEncoderFactory;
import checkers.inference.solver.frontend.Lattice;
/** MaxSatFormatTranslator converts constraint into array of VecInt as clauses. */
public class MaxSatFormatTranslator extends AbstractFormatTranslator<VecInt[], VecInt[], Integer> {
/**
* typeToInt maps each type qualifier to an unique integer value starts from 0 on continuous
* basis.
*/
protected final Map<AnnotationMirror, Integer> typeToInt;
/**
* intToType maps an integer value to each type qualifier, which is a reversed map of typeToInt.
*/
protected final Map<Integer, AnnotationMirror> intToType;
public MaxSatFormatTranslator(Lattice lattice) {
super(lattice);
// Initialize mappings between type and int.
Map<AnnotationMirror, Integer> typeToIntRes = new AnnotationMirrorMap<>();
Map<Integer, AnnotationMirror> intToTypeRes = new HashMap<Integer, AnnotationMirror>();
int curInt = 0;
for (AnnotationMirror type : lattice.allTypes) {
typeToIntRes.put(type, curInt);
intToTypeRes.put(curInt, type);
curInt++;
}
typeToInt = Collections.unmodifiableMap(typeToIntRes);
intToType = Collections.unmodifiableMap(intToTypeRes);
finishInitializingEncoders();
}
@Override
protected ConstraintEncoderFactory<VecInt[]> createConstraintEncoderFactory() {
return new MaxSATConstraintEncoderFactory(lattice, typeToInt, this);
}
/** generate well form clauses such that there is one and only one beta value can be true. */
public void generateWellFormednessClauses(
List<VecInt> wellFormednessClauses, Integer varSlotId) {
int[] leastOneIsTrue = new int[lattice.numTypes];
for (Integer i : intToType.keySet()) {
leastOneIsTrue[i] = MathUtils.mapIdToMatrixEntry(varSlotId, i.intValue(), lattice);
}
wellFormednessClauses.add(VectorUtils.asVec(leastOneIsTrue));
List<Integer> varList = new ArrayList<Integer>(intToType.keySet());
for (int i = 0; i < varList.size(); i++) {
for (int j = i + 1; j < varList.size(); j++) {
VecInt vecInt = new VecInt(2);
vecInt.push(-MathUtils.mapIdToMatrixEntry(varSlotId, varList.get(i), lattice));
vecInt.push(-MathUtils.mapIdToMatrixEntry(varSlotId, varList.get(j), lattice));
wellFormednessClauses.add(vecInt);
}
}
}
@Override
public AnnotationMirror decodeSolution(
Integer var, ProcessingEnvironment processingEnvironment) {
return intToType.get(MathUtils.getIntRep(var, lattice));
}
}