Between Tuesday August 20th, 5:00pm and Thursday August 22nd, 8:00am git.uwaterloo.ca will be down for an upgrade to version 10.8.7.

0bf0effa by Amin Bandali

Properly write out json serializations

- Mark file_results as transient so as to not include it in main file.
  Instead, write out one json file for each FileResult analyzeFile(…).

- Add a list of SSR candidate signature names to SSRResult.

- Use non-prettified json for smaller file sizes.
1 parent e7019326
......@@ -76,7 +76,7 @@ protected void initialize(Path outputTxtFile, Path outputJsonFile) {
abstract protected CorpusResult analyzeCorpus(String corpus);
abstract protected FileResult analyzeFile(String corpus, Path p, int index, int total);
private BufferedWriter setupWriter(Path p, boolean createParents) {
protected BufferedWriter setupWriter(Path p, boolean createParents) {
if (createParents)
p.getParent().toFile().mkdirs();
......
package org.aminb.alloy.catalyst;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -15,8 +17,6 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;
import com.google.gson.GsonBuilder;
import edu.mit.csail.sdg.alloy4.A4Reporter;
import edu.mit.csail.sdg.alloy4.ConstList;
import edu.mit.csail.sdg.alloy4.Err;
......@@ -67,7 +67,6 @@ private Solver(SatSolver solver) {
public Mut() {}
public void run() {
gson = new GsonBuilder().setPrettyPrinting().create();
initialize(Paths.get(outDir, OUT_TEXT), Paths.get(outDir, OUT_JSON));
// ugly workaround to load the solvers' native libraries
......@@ -119,7 +118,7 @@ protected final CorpusResult analyzeCorpus(String corpus) {
} catch (Exception e) {
String msg = "Failed while trying to iterate through and analyze " + corpus + " models.";
cr.error = String.format("%s%n%s", msg, Utils.toString(e));
System.out.print(cr.error);
p(cr.error);
throw Utils.systemExit(1);
}
Instant end = Instant.now();
......@@ -140,6 +139,9 @@ protected final FileResult analyzeFile(String corpus, Path p, int index, int tot
fr.mutation_results = new ArrayList<MutationResult>();
fr.errors = new ArrayList<ParseOrTypeError>();
Path outFile = Paths.get(outDir, corpus, fr.file_name.replace("/", outSep));
Writer w = setupWriter(outFile, true);
String progress = String.format("[%d/%d]: ", index, total);
A4Reporter rep = new A4Reporter() {
......@@ -194,6 +196,23 @@ public void warning(ErrorWarning msg) {
}
pln(progress + "done: " + fr.file_name);
try {
w.write(gson.toJson(fr));
}
catch (IOException e) {
pln("I/O error occured while trying to serialize FileResult to JSON:");
pln(Utils.toString(e));
}
try {
w.close();
}
catch (IOException e) {
pln("I/O error occured while trying to close JSON writer:");
pln(Utils.toString(e));
}
return fr;
}
......
......@@ -15,7 +15,7 @@
class CorpusResult {
public String corpus_name;
public int file_count;
public List<FileResult> file_results;
public transient List<FileResult> file_results;
public long elapsed_time;
public SSRResult ssr_result;
public String error;
......@@ -42,6 +42,7 @@ public ParseOrTypeError(String msg, boolean fatal) {
class SSRResult {
public long sig_count;
public long ssr_candidate_count;
public List<String> ssr_candidates;
}
class MutationResult {
......
package org.aminb.alloy.catalyst;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -8,13 +10,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.gson.GsonBuilder;
import edu.mit.csail.sdg.alloy4.A4Reporter;
import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4.ErrorWarning;
......@@ -32,7 +33,6 @@
public SSR() {}
public void run() {
gson = new GsonBuilder().setPrettyPrinting().create();
initialize(Paths.get(outDir, OUT_TEXT), Paths.get(outDir, OUT_JSON));
Instant start = Instant.now();
......@@ -47,7 +47,22 @@ public void run() {
pln(String.format("total error count: %d", r.total_error_count));
pln(String.format("total elapsed time: %dms", r.total_elapsed_time));
pln(gson.toJson(r));
try {
wjson.write(gson.toJson(r));
}
catch (IOException e) {
pln("I/O error occured while trying to serialize Result to JSON:");
pln(Utils.toString(e));
}
try {
wjson.close();
}
catch (IOException e) {
pln("I/O error occured while trying to close JSON writer:");
pln(Utils.toString(e));
}
ptxt.close();
}
......@@ -77,7 +92,7 @@ protected final CorpusResult analyzeCorpus(String corpus) {
} catch (Exception e) {
String msg = "Failed while trying to iterate through and analyze " + corpus + " models.";
cr.error = String.format("%s%n%s", msg, Utils.toString(e));
System.out.print(cr.error);
p(cr.error);
throw Utils.systemExit(1);
}
Instant end = Instant.now();
......@@ -109,6 +124,9 @@ protected final FileResult analyzeFile(String corpus, Path p, int index, int tot
fr.mutation_results = new ArrayList<MutationResult>();
fr.errors = new ArrayList<ParseOrTypeError>();
Path outFile = Paths.get(outDir, corpus, fr.file_name.replace("/", outSep));
Writer w = setupWriter(outFile, true);
String progress = String.format("[%d/%d]: ", index, total);
A4Reporter rep = new A4Reporter() {
......@@ -135,6 +153,23 @@ public void warning(ErrorWarning msg) {
fr.ssr_result = detectRemovableSigs(world);
pln(progress + "done: " + fr.file_name);
try {
w.write(gson.toJson(fr));
}
catch (IOException e) {
pln("I/O error occured while trying to serialize FileResult to JSON:");
pln(Utils.toString(e));
}
try {
w.close();
}
catch (IOException e) {
pln("I/O error occured while trying to close JSON writer:");
pln(Utils.toString(e));
}
return fr;
}
......@@ -143,9 +178,14 @@ private SSRResult detectRemovableSigs(CompModule world) {
world.visitExpressions(v);
SSRResult r = new SSRResult();
r.sig_count = v.candidates.size();
r.ssr_candidate_count = Collections.frequency(v.candidates.values(), true);
// System.out.println("SSR candidates: " + v.candidates);
// System.out.println();
r.ssr_candidates = new ArrayList<String>();
v.candidates.entrySet()
.stream()
.forEach(entry -> {
if (entry.getValue().equals(true))
r.ssr_candidates.add(entry.getKey().toString());
});
r.ssr_candidate_count = r.ssr_candidates.size();
return r;
}
}
......