forked from Jianchu/generic-type-inference-solver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogiqlDebugSolverCalTime.java
283 lines (265 loc) · 11.4 KB
/
LogiqlDebugSolverCalTime.java
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
package checkers.inference.solver.LogiqlDebugSolver;
import java.io.*;
import java.lang.reflect.Array;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.util.Elements;
import org.checkerframework.framework.type.QualifierHierarchy;
import checkers.inference.InferenceSolver;
import checkers.inference.model.CombVariableSlot;
import checkers.inference.model.ConstantSlot;
import checkers.inference.model.Constraint;
import checkers.inference.model.RefinementVariableSlot;
import checkers.inference.model.Slot;
import checkers.inference.model.VariableSlot;
import ostrusted.quals.OsTrusted;
import ostrusted.quals.OsUntrusted;
import org.checkerframework.javacutil.AnnotationUtils;
/**
* Debug solver prints out variables and constraints.
*
* @author Jianchu Li
*
*/
public class LogiqlDebugSolverCalTime implements InferenceSolver {
private final String OSTRUSTED = "ostrusted";
private final String OSUNTRUSTED = "osuntrusted";
private long t_start = 0;
private long t_variable = 0;
private long t_constraint = 0;
private long t_generate_logic = 0;
private long t_decode_e = 0;
private long t_end = 0;
private int trusted = 0;
private String inReply = "";
private int untrusted = 0;
private final String currentPath = new File("").getAbsolutePath();
Map<Integer, AnnotationMirror> result = new HashMap<Integer, AnnotationMirror>();
@Override
public Map<Integer, AnnotationMirror> solve(
Map<String, String> configuration, Collection<Slot> slots,
Collection<Constraint> constraints,
QualifierHierarchy qualHierarchy,
ProcessingEnvironment processingEnvironment) {
String Base;
File file = null;
file = new File(currentPath);
Base = file.getParent().toString();
String Path = Base + "/src/checkers/inference/solver/LogiqlDebugSolver";
//String addVariable = "";
String deleteVariable = "";
String output = "";
String deleteoutput = "";
String s = "";
t_start = System.currentTimeMillis();
for (int i = 0; i < slots.size(); i++) {
// addVariable = addVariable + "+Variable(" + i + ").\n";
deleteVariable = deleteVariable + "-Variable(" + i + ").\n";
}
t_variable = System.currentTimeMillis();
for (Constraint constraint : constraints) {
if (constraint.getClass().getSimpleName().contains("ComparableConstraint")) {
continue;
}
List<Slot> slots1 = constraint.getSlots();
if (slots1.get(0)!= slots1.get(1)) {
String[] vStr = new String[2];
vStr = SlotsToStr(slots1);
for (int i = 0; i < 2; i++) {
if (vStr[i].contains("OsTrusted"))
vStr[i] = OSTRUSTED;
else if (vStr[i].contains("OsUntrusted")) {
vStr[i] = OSUNTRUSTED;
}
}
if (vStr[0]!=vStr[1]) {
output = output + "+Variable(_" + vStr[0] + "),+hasVariableName[_"+vStr[0] + "]="+ vStr[0] +"," + "+Variable(_" + vStr[1] + "),+hasVariableName[_"+vStr[1] + "]="+ vStr[1]+",+"+constraint.getClass().getSimpleName() + "["
+ "_"+vStr[0] + "," + "_"+vStr[1] + "]=true.\n";
output = output.replaceAll("=ostrusted", "=-1");
output = output.replaceAll("=osuntrusted", "=-2");
/*
deleteoutput = deleteoutput + "-Variable(_" + vStr[0] + "),-hasVariableName[_"+vStr[0] + "]="+ vStr[0] +"," + "-Variable(_" + vStr[1] + "),-hasVariableName[_"+vStr[1] + "]="+ vStr[1]+",-"+constraint.getClass().getSimpleName() + "["
+ "_"+vStr[0] + "," + "_"+vStr[1] + "]=true.\n";
deleteoutput = deleteoutput.replaceAll("=ostrusted", "=-1");
deleteoutput = deleteoutput.replaceAll("=osuntrusted", "=-2");
*/
// deleteoutput = deleteoutput + "-"
// + constraint.getClass().getSimpleName() + "[" + vStr[0]
// + "," + vStr[1] + "]=true.\n";
}
}
}
t_constraint = System.currentTimeMillis();
try {
String writePath = Path + "/data.logic";
File f = new File(writePath);
PrintWriter pw = new PrintWriter(f);
// pw.write(addVariable + output);
pw.write(output);
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
String writePath = Path + "/deletedata.logic";
File f = new File(writePath);
PrintWriter pw = new PrintWriter(f);
pw.write(deleteVariable);
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
t_generate_logic = System.currentTimeMillis();
String[] command = new String[7];
// command[0]=
// "source /home/jianchu/Desktop/logicblox-x86_64-linux-4.1.8-91f645cf2a0e/etc/profile.d/logicblox.sh";
command[1] = "lb create pltest";
command[2] = "lb addblock pltest -f" + Path + "/constraint.logic";
command[3] = "lb exec pltest -f" + Path + "/basicData.logic";
command[4] = "lb exec pltest -f" + Path + "/data.logic";
command[5] = "lb print pltest AnnotationOf";
command[6] = "lb exec pltest -f" + Path + "/deletedata.logic";
try {
for (int i = 4; i < 5; i++) {
getOutPut_Error(command[i]);
}
t_end = System.currentTimeMillis();
getOutPut_Error(command[5]);
parseOutPut(processingEnvironment, slots.size());
t_decode_e = System.currentTimeMillis();
//Process p6 = Runtime.getRuntime().exec(command[6]);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String numberOfSlots = "The number of slots: " + slots.size() + "\n";
String numberOfConstraints = "The number of constraints: "
+ constraints.size() + "\n";
String timing = "Total time in LogiqlDebugSolver: "
+ (t_decode_e - t_start) + "\nTime for generating variables: "
+ (t_variable - t_start)
+ "\nTime for generating constraints: "
+ (t_constraint - t_variable)
+ "\nTime for generating logic file: "
+ (t_generate_logic - t_constraint)
+ "\nTime for Logiql solver computing: "
+ (t_end - t_generate_logic)
+ "\nTime for decoding the result of Logicblox: "
+ (t_decode_e - t_end) + "\n";
String numberOfType = "Number Of Ostrusted: " + trusted + "\n"
+ "Number Of OsUntrusted: " + untrusted + "\n";
System.out.println(numberOfSlots + numberOfConstraints + numberOfType
+ timing);
return result;
}
private void getOutPut_Error(String command) throws IOException,
InterruptedException {
final Process p = Runtime.getRuntime().exec(command);
Thread getOutPut = new Thread() {
public void run() {
String s = "";
BufferedReader stdInput = new BufferedReader(
new InputStreamReader(p.getInputStream()));
try {
while ((s = stdInput.readLine()) != null) {
inReply = inReply + s + "\n";
}
// System.out.println("Output from lb print"+inReply);
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
};
getOutPut.start();
Thread getError = new Thread() {
public void run() {
String s = "";
String errReply = "";
BufferedReader stdError = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
try {
while ((s = stdError.readLine()) != null) {
errReply = errReply + s;
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Error from lb command:" + errReply);
}
};
getError.start();
getOutPut.join();
getError.join();
p.waitFor();
}
private void parseOutPut(ProcessingEnvironment processingEnvironment,
int size) {
Elements elements = processingEnvironment.getElementUtils();
final AnnotationMirror OSuntrusted = AnnotationUtils.fromClass(
elements, OsUntrusted.class);
final AnnotationMirror OStrusted = AnnotationUtils.fromClass(elements,
OsTrusted.class);
String s = "";
for (int i = 0; i < size; i++) {
result.put(i, OSuntrusted);
}
BufferedReader stdInput = new BufferedReader(new StringReader(inReply));
try {
while ((s = stdInput.readLine()) != null) {
// System.out.println(s);
if (s.contains("[")) {
String[] Line = s.split(" ");
if (!Line[1].contains("-1") && !Line[1].contains("-2")) {
int VariableId = Integer.parseInt(Line[1]);
if (s.contains("Ostrusted")) {
trusted++;
result.put(VariableId, OStrusted);
} else if (s.contains("Osuntrusted")) {
untrusted++;
result.put(VariableId, OSuntrusted);
}
}
}
}
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
/*
* Transform two constraint slots to their Id value
*/
private String[] SlotsToStr(List<Slot> slots) {
String[] vStr = new String[2];
for (int i = 0; i < 2; i++) {
if (slots.get(i).getClass().equals(VariableSlot.class)) {
VariableSlot slot = (VariableSlot) slots.get(i);
vStr[i] = Integer.toString(slot.getId());
} else if (slots.get(i).getClass()
.equals(RefinementVariableSlot.class)) {
RefinementVariableSlot slot = (RefinementVariableSlot) slots
.get(i);
vStr[i] = Integer.toString(slot.getId());
} else if (slots.get(i).getClass().equals(CombVariableSlot.class)) {
CombVariableSlot slot = (CombVariableSlot) slots.get(i);
vStr[i] = Integer.toString(slot.getId());
} else if (slots.get(i).getClass().equals(ConstantSlot.class)) {
ConstantSlot slot = (ConstantSlot) slots.get(i);
vStr[i] = slot.getValue().toString();
}
}
return vStr;
}
}