forked from typetools/checker-framework-inference
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDebugSolver.java
More file actions
97 lines (77 loc) · 3.53 KB
/
Copy pathDebugSolver.java
File metadata and controls
97 lines (77 loc) · 3.53 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
package checkers.inference.solver;
import org.checkerframework.framework.type.QualifierHierarchy;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.processing.ProcessingEnvironment;
import checkers.inference.InferenceResult;
import checkers.inference.InferenceSolver;
import checkers.inference.model.Constraint;
import checkers.inference.model.Slot;
import checkers.inference.model.serialization.ToStringSerializer;
import checkers.inference.util.InferenceUtil;
/** Debug solver prints out variables and constraints. */
public class DebugSolver implements InferenceSolver {
private static final boolean showAstPaths =
true; // System.getProperty("showAstPaths", "false").equalsIgnoreCase("true");
public static final String constraintFile = "constraint-file";
@Override
public InferenceResult solve(
Map<String, String> configuration,
Collection<Slot> slots,
Collection<Constraint> constraints,
QualifierHierarchy qualHierarchy,
ProcessingEnvironment processingEnvironment) {
List<String> output = new ArrayList<>();
InferenceUtil.flushAllLoggers(true);
final ToStringSerializer serializer = new ToStringSerializer(showAstPaths);
final Map<Class<? extends Slot>, List<Slot>> typesToSlots = partitionSlots(slots);
serializer.setIndentationLevel(1);
StringBuilder stringBuilder = new StringBuilder();
for (final Entry<Class<? extends Slot>, List<Slot>> entry : typesToSlots.entrySet()) {
final Class<? extends Slot> type = entry.getKey();
final List<Slot> slotsForType = entry.getValue();
stringBuilder.append("\nCreated " + type.getSimpleName() + "\n");
stringBuilder.append(serializer.serializeSlots(slotsForType, "\n\n"));
stringBuilder.append("\n");
System.out.print(stringBuilder.toString());
output.add(stringBuilder.toString());
stringBuilder.setLength(0);
}
stringBuilder.append("\nCreated these Constraints:\n");
stringBuilder.append(serializer.serializeConstraints(constraints, "\n\n"));
stringBuilder.append("\n\n");
System.out.print(stringBuilder.toString());
System.out.flush();
output.add(stringBuilder.toString());
stringBuilder = null;
if (configuration.containsKey(constraintFile)) {
String filename = configuration.get(constraintFile);
try (FileWriter file = new FileWriter(new File(filename))) {
for (String out : output) file.write(out);
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static Map<Class<? extends Slot>, List<Slot>> partitionSlots(Collection<Slot> slots) {
Map<Class<? extends Slot>, List<Slot>> typeToSlots = new LinkedHashMap<>();
for (final Slot slot : slots) {
Class<? extends Slot> slotClass = slot.getClass();
// Create array list for the type of the slot when we first encounter a new slot type
if (typeToSlots.get(slotClass) == null) {
typeToSlots.put(slotClass, new ArrayList<Slot>());
}
typeToSlots.get(slotClass).add(slot);
}
return typeToSlots;
}
}