-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerBTSpaceMapperRunner.java
More file actions
268 lines (243 loc) · 10.9 KB
/
PerBTSpaceMapperRunner.java
File metadata and controls
268 lines (243 loc) · 10.9 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
package il.ac.bgu.cs.bp.statespacemapper;
import il.ac.bgu.cs.bp.bpjs.model.*;
import il.ac.bgu.cs.bp.statespacemapper.jgrapht.MapperEdge;
import il.ac.bgu.cs.bp.statespacemapper.jgrapht.MapperVertex;
import il.ac.bgu.cs.bp.statespacemapper.jgrapht.exports.DotExporter;
import il.ac.bgu.cs.bp.statespacemapper.jgrapht.exports.Exporter;
import il.ac.bgu.cs.bp.statespacemapper.jgrapht.exports.JsonExporter;
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.Graphs;
import org.jgrapht.graph.AbstractBaseGraph;
import org.jgrapht.nio.DefaultAttribute;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static java.util.stream.Collectors.joining;
public class PerBTSpaceMapperRunner {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Missing input files");
System.err.println("Sample execution argument: \"hot-cold.js\"");
System.exit(1);
}
var bprog = getBProgram(args);
var runName = bprog.getName().replace(".js+", "");
System.out.println("// start");
bprog.setup();
var mapperResults =
((Map<String, Object>) bprog.getFromGlobalScope("bthreads", Map.class).get()).keySet().stream()
.collect(Collectors.toMap(
name -> name,
bt -> {
var btBProg = getBProgram(args);
btBProg.appendSource("bp.registerBThread('" + bt + "',bthreads['" + bt + "'])");
btBProg.setEventSelectionStrategy(new EssForPerBThread());
try {
return mapSpace(btBProg);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
Graph<MapperVertex, MapperEdge> union = null;
for (var res : mapperResults.values()) {
if (union == null) {
union = (Graph<MapperVertex, MapperEdge>) ((AbstractBaseGraph) res.graph).clone();
} else {
Graphs.addGraph(union, res.graph);
}
}
exportSpace(runName, new MapperResult(union));
}
public static void exportSpace(String runName, MapperResult res) throws IOException {
var outputDir = "exports";
System.out.println("// Export to JSON...");
var path = Paths.get(outputDir, runName + ".json").toString();
var jsonExporter = new JsonExporter(res);
setExporterProviders(res, jsonExporter);
jsonExporter.export(path, runName);
System.out.println("// Export to GraphViz...");
path = Paths.get(outputDir, runName + ".dot").toString();
var dotExporter = new DotExporter(res);
setExporterProviders(res, dotExporter);
dotExporter.export(path, runName);
}
public static void setExporterProviders(MapperResult res, Exporter exporter) {
exporter.setVertexAttributeProvider(v -> {
var snapshot = v.bpss.getBThreadSnapshots().stream()
.findFirst();
if (snapshot.isPresent()) {
var syst = snapshot.get().getSyncStatement();
return Map.of(
"isHot", DefaultAttribute.createAttribute(syst.isHot()),
"request", DefaultAttribute.createAttribute(syst.getRequest().stream().map(BEvent::toString).collect(joining(","))),
"waitFor", DefaultAttribute.createAttribute(Utils.eventSetToList(syst.getWaitFor()).stream().map(BEvent::toString).collect(joining(","))),
"block", DefaultAttribute.createAttribute(Utils.eventSetToList(syst.getBlock()).stream().map(BEvent::toString).collect(joining(","))),
"interrupt", DefaultAttribute.createAttribute(Utils.eventSetToList(syst.getInterrupt()).stream().map(BEvent::toString).collect(joining(","))),
"start", DefaultAttribute.createAttribute(v.startVertex),
"accepting", DefaultAttribute.createAttribute(v.accepting),
"bthread", DefaultAttribute.createAttribute(exporter.getSanitizerProvider().apply(getBThreads(v.bpss))));
} else {
return Map.of();
}
});
var oldGraphProvider = exporter.getGraphAttributeProvider().get();
oldGraphProvider.put("edges", DefaultAttribute.createAttribute(res.edges().stream().map(MapperEdge::getEvent).map(BEvent::toString).collect(joining(",", "\"[", "]\""))));
oldGraphProvider.put("bthreads", DefaultAttribute.createAttribute(res.states().stream().flatMap(v->v.bpss.getBThreadSnapshots().stream()).map(BThreadSyncSnapshot::getName).distinct().sorted().collect(joining(",","\"[","]\""))));
exporter.setGraphAttributeProvider(() -> oldGraphProvider);
}
public static String getBThreads(BProgramSyncSnapshot bpss) {
return bpss.getBThreadSnapshots().stream().map(BThreadSyncSnapshot::getName).collect(joining(","));
}
public static MapperResult mapSpace(BProgram bprog) throws Exception {
System.out.println("// Start mapping space");
var mpr = new StateSpaceMapper(bprog);
// the maximal trace length can be limited: mpr.setMaxTraceLength(50);
var res = mpr.mapSpace();
System.out.println("// completed mapping the states graph");
System.out.println(res.toString());
return res;
}
public static BProgram getBProgram(String[] args) {
BProgram bprog = null;
try {
bprog = new ResourceBProgram(args);
} catch (Exception ignored) {
bprog = new BProgram(args[0].replaceAll("\\.\\.[/\\\\]", "")) {
@Override
protected void setupProgramScope(Scriptable scope) {
for (String arg : args) {
if (arg.equals("-")) {
System.out.println(" [READ] stdin");
try {
evaluate(System.in, "stdin", Context.getCurrentContext());
} catch (EvaluatorException ee) {
logScriptExceptionAndQuit(ee, arg);
}
} else {
if (!arg.startsWith("-")) {
Path inFile = Paths.get(arg);
System.out.printf(" [READ] %s\n", inFile.toAbsolutePath());
if (!Files.exists(inFile)) {
System.out.printf("File %s does not exit\n", inFile.toAbsolutePath());
System.exit(-2);
}
try (InputStream in = Files.newInputStream(inFile)) {
evaluate(in, arg, Context.getCurrentContext());
} catch (EvaluatorException ee) {
logScriptExceptionAndQuit(ee, arg);
} catch (IOException ex) {
System.out.printf("Exception while processing %s: %s\n", arg, ex.getMessage());
}
}
}
System.out.printf(" [ OK ] %s\n", arg);
}
}
private void logScriptExceptionAndQuit(EvaluatorException ee, String arg) {
System.out.printf("Error in source %s:\n", arg);
System.out.println(ee.details());
System.out.printf("line: %d: %d\n", ee.lineNumber(), ee.columnNumber());
System.out.printf("source: %s\n", ee.lineSource());
System.exit(-3);
}
};
}
return bprog;
}
/*
* TODO: should be moved to test...
*/
private static void testAllPaths(MapperResult res) {
System.out.println("// Generated paths:");
var allDirectedPathsAlgorithm1 = res.createAllDirectedPathsBuilder()
.setSimplePathsOnly(true)
.setIncludeReturningEdgesInSimplePaths(true)
.setLongestPathsOnly(false)
.build();
var allDirectedPathsAlgorithm2 = res.createAllDirectedPathsBuilder()
.setSimplePathsOnly(true)
.setIncludeReturningEdgesInSimplePaths(true)
.setLongestPathsOnly(true)
.build();
var graphPaths1 = allDirectedPathsAlgorithm1.getAllPaths();
var graphPaths2 = allDirectedPathsAlgorithm2.getAllPaths();
var eventPaths1 = MapperResult.GraphPaths2BEventPaths(graphPaths1)
.stream()
.map(l -> l.stream()
.map(BEvent::toString)
.map(s -> s.replaceAll("\\[BEvent name:([^]]+)\\]", "$1"))
.collect(Collectors.joining(", ")))
.distinct()
.sorted()
.collect(Collectors.joining("\n"));
var eventPaths2longest = MapperResult.GraphPaths2BEventPaths(graphPaths2);
var eventPaths2All = new ArrayList<List<BEvent>>();
for (var l : eventPaths2longest) {
for (int i = 0; i <= l.size(); i++) {
eventPaths2All.add(l.subList(0, i));
}
}
var eventPaths2 = eventPaths2All.stream()
.map(l -> l.stream()
.map(BEvent::toString)
.map(s -> s.replaceAll("\\[BEvent name:([^]]+)\\]", "$1"))
.collect(Collectors.joining(", ")))
.distinct()
.sorted()
.collect(Collectors.joining("\n"));
System.out.println("EventPath1 = " + eventPaths1);
System.out.println("EventPath2 = " + eventPaths2);
System.out.println("ep1==ep2: " + (eventPaths1.equals(eventPaths2)));
}
/**
* Generate all paths and write them to a zip file containing a csv file with the paths.
* See {@link il.ac.bgu.cs.bp.statespacemapper.jgrapht.AllDirectedPaths} for all the possible algorithm configurations.
*/
private static void writeCompressedPaths(String csvFileName, Integer maxPathLength, MapperResult res, String outputDir) throws IOException {
System.out.println("// Generating paths...");
var allDirectedPathsAlgorithm = res.createAllDirectedPathsBuilder()
.setSimplePathsOnly(maxPathLength == null)
.setIncludeReturningEdgesInSimplePaths(maxPathLength == null)
.setLongestPathsOnly(false)
.setMaxPathLength(maxPathLength)
.build();
var graphPaths = allDirectedPathsAlgorithm.getAllPaths();
int maxLength = graphPaths.parallelStream().map(GraphPath::getLength).max(Integer::compareTo).orElse(0);
System.out.println("// Number of paths = " + graphPaths.size());
System.out.println("// Max path length = " + maxLength);
System.out.println("// Writing paths...");
try (var fos = new FileOutputStream(Paths.get(outputDir, csvFileName) + ".zip");
var zipOut = new ZipOutputStream(fos)) {
var zipEntry = new ZipEntry(csvFileName);
zipOut.putNextEntry(zipEntry);
zipOut.setLevel(9);
MapperResult.GraphPaths2BEventPaths(graphPaths)
.parallelStream()
.map(l -> l.stream()
.map(BEvent::getName)
// .filter(s -> !List.of("KeepDown", "ClosingRequest", "OpeningRequest").contains(s))
.collect(Collectors.joining(",", "", "\n")))
.distinct()
.sorted()
.forEachOrdered(s -> {
try {
zipOut.write(s.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}