forked from typetools/checker-framework-inference
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathJsonSerializer.java
More file actions
370 lines (311 loc) · 14 KB
/
Copy pathJsonSerializer.java
File metadata and controls
370 lines (311 loc) · 14 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
package checkers.inference.model.serialization;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import checkers.inference.model.ArithmeticConstraint;
import checkers.inference.model.ArithmeticVariableSlot;
import checkers.inference.model.CombVariableSlot;
import checkers.inference.model.CombineConstraint;
import checkers.inference.model.ComparableConstraint;
import checkers.inference.model.ComparisonConstraint;
import checkers.inference.model.ComparisonVariableSlot;
import checkers.inference.model.ConstantSlot;
import checkers.inference.model.Constraint;
import checkers.inference.model.EqualityConstraint;
import checkers.inference.model.ExistentialConstraint;
import checkers.inference.model.ExistentialVariableSlot;
import checkers.inference.model.ImplicationConstraint;
import checkers.inference.model.InequalityConstraint;
import checkers.inference.model.LubVariableSlot;
import checkers.inference.model.PreferenceConstraint;
import checkers.inference.model.RefinementVariableSlot;
import checkers.inference.model.Serializer;
import checkers.inference.model.Slot;
import checkers.inference.model.SourceVariableSlot;
import checkers.inference.model.SubtypeConstraint;
/**
* // Scores are numeric // Everything else is a string (including version and qualifier ids) //
* Game side ignores any key in a map prefixed with "system-" // Variables are prefixed with "var:"
* // Types are prefixed with "type:"
*
* <p>// Variable values are set in the "variables": "var:ID": "type_value": key.
*
* <p>{ "version": "1",
*
* <p>"scoring": { "constraints": 1000, "variables": { "type:0" : 0, "type:1": 100 } },
*
* <p>// Extra configurations on variables // Listing variables here is optional "variables": {
* "var:10" : { "type_value": "type:0", "keyfor_value" : [],' "score": { "type:0" : 0, "type:1":
* 1000 }, "possible_keyfor": ["mymap1", "mymap2"] } },
*
* <p>"constraints": [ // Format 1 "var:10 <= type:0",
*
* <p>// Subtype { "constraint" : "subtype", // subtype, equality, inequality "lhs" : "var:1",
* "rhs": "var:2" "score": 100 },
*
* <p>// Map.get { "constraint": "map.get", "name": "mymap1", "value_type": "var:1", "key": "var:2",
* "result": "var:3" },
*
* <p>// If Node { "constraint": "selection_check", "id": "var:11", "type": "type:0", "then": [ ...
* ], // Nested list of constraints "else": [ ... ], },
*
* <p>// Generics { "constraint": "enabled_check", "id" : "var:12", "then": [ ... ], "else": [ ...
* ], } ] }
*/
public class JsonSerializer implements Serializer<String, JSONObject> {
// Version of this format
protected static final String VERSION_KEY = "version";
// Constraints
protected static final String CONSTRAINTS_KEY = "constraints";
protected static final String CONSTRAINT_KEY = "constraint";
protected static final String SUBTYPE_CONSTRAINT_KEY = "subtype";
protected static final String SUBTYPE_SUB_KEY = "sub";
protected static final String SUBTYPE_SUPER_KEY = "sup";
protected static final String EQUALITY_CONSTRAINT_KEY = "equality";
protected static final String EQUALITY_RHS = "rhs";
protected static final String EQUALITY_LHS = "lhs";
protected static final String INEQUALITY_CONSTRAINT_KEY = "inequality";
protected static final String INEQUALITY_RHS = "rhs";
protected static final String INEQUALITY_LHS = "lhs";
protected static final String COMPARABLE_CONSTRAINT_KEY = "comparable";
protected static final String COMPARABLE_RHS = "rhs";
protected static final String COMPARABLE_LHS = "lhs";
protected static final String COMPARISON_CONSTRAINT_KEY = "comparison";
protected static final String COMPARISON_RHS = "rhs";
protected static final String COMPARISON_LHS = "lhs";
protected static final String COMPARISON_RESULT = "result";
protected static final String COMB_CONSTRAINT_KEY = "combine";
protected static final String COMB_TARGET = "target";
protected static final String COMB_DECL = "declared";
protected static final String COMB_RESULT = "result";
protected static final String PREFERENCE_CONSTRAINT_KEY = "preference";
protected static final String PREFERENCE_VARIABLE = "variable";
protected static final String PREFERENCE_GOAL = "goal";
protected static final String PREFERENCE_WEIGHT = "weight";
protected static final String VARIABLES_KEY = "variables";
protected static final String VARIABLES_VALUE_KEY = "type_value";
protected static final String EXISTENTIAL_VARIABLES_KEY = "enabled_vars";
protected static final String EXISTENTIAL_CONSTRAINT_KEY = "enabled_check";
protected static final String EXISTENTIAL_ID = "id";
protected static final String EXISTENTIAL_THEN = "then";
protected static final String EXISTENTIAL_ELSE = "else";
protected static final String IMPLICATION_CONSTRAINT_KEY = "implication";
protected static final String IMPLICATION_ASSUMPTIONS = "assumptions";
protected static final String IMPLICATION_CONCLUSTION = "conclusion";
protected static final String ARITH_LEFT_OPERAND = "left_operand";
protected static final String ARITH_RIGHT_OPERAND = "right_operand";
protected static final String ARITH_RESULT = "result";
protected static final String VERSION = "2";
protected static final String VAR_PREFIX = "var:";
@SuppressWarnings("unused")
private final Collection<Slot> slots;
private final Collection<Constraint> constraints;
private final Map<Integer, AnnotationMirror> solutions;
private AnnotationMirrorSerializer annotationSerializer;
public JsonSerializer(
Collection<Slot> slots,
Collection<Constraint> constraints,
Map<Integer, AnnotationMirror> solutions,
AnnotationMirrorSerializer annotationSerializer) {
this.slots = slots;
this.constraints = constraints;
this.solutions = solutions;
this.annotationSerializer = annotationSerializer;
}
@SuppressWarnings("unchecked")
public JSONObject generateConstraintFile() {
JSONObject result = new JSONObject();
result.put(VERSION_KEY, VERSION);
if (solutions != null && solutions.size() > 0) {
result.put(VARIABLES_KEY, generateVariablesSection());
}
result.put(CONSTRAINTS_KEY, constraintsToJsonArray(constraints));
return result;
}
@SuppressWarnings("unchecked")
protected JSONObject generateVariablesSection() {
JSONObject variables = new JSONObject();
for (Map.Entry<Integer, AnnotationMirror> entry : solutions.entrySet()) {
JSONObject variable = new JSONObject();
variable.put(VARIABLES_VALUE_KEY, getConstantString(entry.getValue()));
variables.put(VAR_PREFIX + entry.getKey(), variable);
}
return variables;
}
protected JSONArray constraintsToJsonArray(final Collection<Constraint> constraints) {
JSONArray jsonConstraints = new JSONArray();
for (Constraint constraint : constraints) {
JSONObject constraintObj = constraint.serialize(this);
if (constraintObj != null) {
jsonConstraints.add(constraintObj);
}
}
return jsonConstraints;
}
protected String getConstantString(AnnotationMirror value) {
return annotationSerializer.serialize(value);
}
private String serializeSlot(Slot slot) {
return VAR_PREFIX + slot.getId();
}
@Override
public String serialize(SourceVariableSlot slot) {
return serializeSlot(slot);
}
@Override
public String serialize(RefinementVariableSlot slot) {
return serializeSlot(slot);
}
@Override
public String serialize(ExistentialVariableSlot slot) {
throw new UnsupportedOperationException(
"Existential slots should be normalized away before serialization.");
}
@Override
public String serialize(ConstantSlot slot) {
return getConstantString(slot.getValue());
}
@Override
public String serialize(CombVariableSlot slot) {
return serializeSlot(slot);
}
@Override
public String serialize(LubVariableSlot slot) {
return serializeSlot(slot);
}
@Override
public String serialize(ArithmeticVariableSlot slot) {
return serializeSlot(slot);
}
@Override
public String serialize(ComparisonVariableSlot slot) {
return serializeSlot(slot);
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(SubtypeConstraint constraint) {
if (constraint.getSubtype() == null || constraint.getSupertype() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, SUBTYPE_CONSTRAINT_KEY);
obj.put(SUBTYPE_SUB_KEY, constraint.getSubtype().serialize(this));
obj.put(SUBTYPE_SUPER_KEY, constraint.getSupertype().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(EqualityConstraint constraint) {
if (constraint.getFirst() == null || constraint.getSecond() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, EQUALITY_CONSTRAINT_KEY);
obj.put(EQUALITY_LHS, constraint.getFirst().serialize(this));
obj.put(EQUALITY_RHS, constraint.getSecond().serialize(this));
return obj;
}
@Override
public JSONObject serialize(ExistentialConstraint constraint) {
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, EXISTENTIAL_CONSTRAINT_KEY);
obj.put(EXISTENTIAL_ID, constraint.getPotentialVariable().serialize(this));
obj.put(EXISTENTIAL_THEN, constraintsToJsonArray(constraint.potentialConstraints()));
obj.put(EXISTENTIAL_ELSE, constraintsToJsonArray(constraint.getAlternateConstraints()));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(InequalityConstraint constraint) {
if (constraint.getFirst() == null || constraint.getSecond() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, INEQUALITY_CONSTRAINT_KEY);
obj.put(INEQUALITY_LHS, constraint.getFirst().serialize(this));
obj.put(INEQUALITY_RHS, constraint.getSecond().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(ComparableConstraint constraint) {
if (constraint.getFirst() == null || constraint.getSecond() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, COMPARABLE_CONSTRAINT_KEY);
obj.put(COMPARABLE_LHS, constraint.getFirst().serialize(this));
obj.put(COMPARABLE_RHS, constraint.getSecond().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(ComparisonConstraint constraint) {
if (constraint.getLeft() == null || constraint.getRight() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, COMPARISON_CONSTRAINT_KEY);
obj.put(COMPARISON_LHS, constraint.getLeft().serialize(this));
obj.put(COMPARISON_RHS, constraint.getRight().serialize(this));
obj.put(COMPARISON_RESULT, constraint.getResult().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(CombineConstraint constraint) {
if (constraint.getTarget() == null
|| constraint.getDeclared() == null
|| constraint.getResult() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, COMB_CONSTRAINT_KEY);
obj.put(COMB_TARGET, constraint.getTarget().serialize(this));
obj.put(COMB_DECL, constraint.getDeclared().serialize(this));
obj.put(COMB_RESULT, constraint.getResult().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(PreferenceConstraint constraint) {
if (constraint.getVariable() == null || constraint.getGoal() == null) {
return null;
}
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, PREFERENCE_CONSTRAINT_KEY);
obj.put(PREFERENCE_VARIABLE, constraint.getVariable().serialize(this));
obj.put(PREFERENCE_GOAL, constraint.getGoal().serialize(this));
// TODO: is the int showing up correctly in JSON?
obj.put(PREFERENCE_WEIGHT, constraint.getWeight());
return obj;
}
@Override
public JSONObject serialize(ImplicationConstraint implicationConstraint) {
// If either assumptions or conclusion is null, the program would have been terminated
// when trying to create that ImplicationConstraint instance.
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, IMPLICATION_CONSTRAINT_KEY);
List<JSONObject> serializedAssumptions = new ArrayList<>();
for (Constraint assumption : implicationConstraint.getAssumptions()) {
serializedAssumptions.add(assumption.serialize(this));
}
obj.put(IMPLICATION_ASSUMPTIONS, serializedAssumptions);
obj.put(IMPLICATION_CONCLUSTION, implicationConstraint.getConclusion().serialize(this));
return obj;
}
@SuppressWarnings("unchecked")
@Override
public JSONObject serialize(ArithmeticConstraint constraint) {
JSONObject obj = new JSONObject();
obj.put(CONSTRAINT_KEY, constraint.getOperation().name().toLowerCase());
obj.put(ARITH_LEFT_OPERAND, constraint.getLeftOperand().serialize(this));
obj.put(ARITH_RIGHT_OPERAND, constraint.getRightOperand().serialize(this));
obj.put(ARITH_RESULT, constraint.getResult().serialize(this));
return obj;
}
}