Verified Commit 9731343c authored by Amin Bandali's avatar Amin Bandali
Browse files

Add {write,close}Writer, rename ParseOrTypeError to CatalystError

parent f227a041
......@@ -23,11 +23,15 @@ package org.shemshak.alloy.catalyst;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.gson.Gson;
......@@ -41,7 +45,7 @@ public abstract class CatalystCommand {
protected transient Result r;
protected transient Gson gson;
protected transient AtomicInteger errorCount;
protected transient Writer wtxt, wjson;
protected transient Writer wtxt, wjson, ejson;
protected transient PrintWriter ptxt;
@Option(names = "--models-dir",
......@@ -83,7 +87,11 @@ public abstract class CatalystCommand {
@Option(names = "--parallel", description = "Parallel mode (use parallel streams)")
protected boolean parallel;
protected void initialize(Path outputTxtFile, Path outputJsonFile) {
final transient protected Map errors = parallel ?
Collections.synchronizedMap(new LinkedHashMap<Path, CatalystError>()) :
new LinkedHashMap<Path, CatalystError>();
protected void initialize(Path outputTxtFile, Path outputJsonFile, Path errorsJsonFile) {
r = new Result();
r.options = this;
r.results = new ArrayList<CorpusResult>();
......@@ -91,6 +99,7 @@ public abstract class CatalystCommand {
errorCount = new AtomicInteger();
wtxt = setupWriter(outputTxtFile, true);
wjson = setupWriter(outputJsonFile, true);
ejson = setupWriter(errorsJsonFile, true);
ptxt = new PrintWriter(wtxt);
}
......@@ -115,6 +124,26 @@ public abstract class CatalystCommand {
return w;
}
protected void writeWriter(Writer w, String s, Path f) {
try {
w.write(s);
}
catch (IOException e) {
pf("I/O error occured while trying to write to %s:%n", f.toString());
pln(Utils.toString(e));
}
}
protected void closeWriter(Writer w, Path f) {
try {
wjson.close();
}
catch (IOException e) {
pf("I/O error occured while trying to close %s writer:%n", f.toString());
pln(Utils.toString(e));
}
}
protected void p(String s) {
System.out.print(s);
ptxt.print(s);
......
......@@ -20,7 +20,6 @@
package org.shemshak.alloy.catalyst;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -82,12 +81,17 @@ public final class Mut extends CatalystCommand implements Runnable {
final transient private String
OUT_TEXT = "catalyst_mut.txt",
OUT_JSON = "catalyst_mut.json";
OUT_JSON = "catalyst_mut.json",
ERR_JSON = "catalyst_mut_errors.json";
public Mut() {}
public void run() {
initialize(Paths.get(outDir, OUT_TEXT), Paths.get(outDir, OUT_JSON));
Path
ot = Paths.get(outDir, OUT_TEXT),
oj = Paths.get(outDir, OUT_JSON),
ej = Paths.get(outDir, ERR_JSON);
initialize(ot, oj, ej);
// ugly workaround to load the solvers' native libraries
// right off the bat and get it over with, to avoid polluting
......@@ -108,7 +112,11 @@ public final class Mut extends CatalystCommand implements Runnable {
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));
writeWriter(wjson, gson.toJson(r), oj);
closeWriter(wjson, oj);
writeWriter(ejson, gson.toJson(errors), ej);
closeWriter(ejson, ej);
ptxt.close();
}
......@@ -157,7 +165,7 @@ public final class Mut extends CatalystCommand implements Runnable {
FileResult fr = new FileResult();
fr.file_name = p.toString();
fr.mutation_results = new ArrayList<MutationResult>();
fr.errors = new ArrayList<ParseOrTypeError>();
fr.errors = new ArrayList<CatalystError>();
Path outFile = Paths.get(outDir, corpus, fr.file_name.replace("/", outSep));
Writer w = setupWriter(outFile, true);
......@@ -167,7 +175,7 @@ public final class Mut extends CatalystCommand implements Runnable {
A4Reporter rep = new A4Reporter() {
@Override
public void warning(ErrorWarning msg) {
fr.errors.add(new ParseOrTypeError(msg.toString().trim(), false));
fr.errors.add(new CatalystError(msg.toString().trim(), false));
}
};
......@@ -181,7 +189,9 @@ public final class Mut extends CatalystCommand implements Runnable {
msg = "Error in parsing or type checking. Stack trace from the exception:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
CatalystError error = new CatalystError(String.format("%s%n%s", msg, err), true);
fr.errors.add(error);
errors.put(p, error);
errorCount.getAndIncrement();
return fr;
}
......@@ -194,7 +204,9 @@ public final class Mut extends CatalystCommand implements Runnable {
msg = "Error while analyzing original module. Stack trace from the exception:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
CatalystError error = new CatalystError(String.format("%s%n%s", msg, err), true);
fr.errors.add(error);
errors.put(p, error);
errorCount.getAndIncrement();
return fr;
}
......@@ -208,7 +220,9 @@ public final class Mut extends CatalystCommand implements Runnable {
msg = "Error while analyzing mutation. Stack trace from the exception:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
CatalystError error = new CatalystError(String.format("%s%n%s", msg, err), true);
fr.errors.add(error);
errors.put(p, error);
errorCount.getAndIncrement();
return fr;
}
......@@ -217,21 +231,8 @@ public final class Mut extends CatalystCommand implements Runnable {
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));
}
writeWriter(w, gson.toJson(fr), outFile);
closeWriter(w, outFile);
return fr;
}
......
......@@ -41,12 +41,12 @@ class CorpusResult {
public String error;
}
class ParseOrTypeError {
class CatalystError {
private String msg;
private boolean fatal;
public ParseOrTypeError() {}
public ParseOrTypeError(String msg, boolean fatal) {
public CatalystError() {}
public CatalystError(String msg, boolean fatal) {
this.msg = msg;
this.fatal = fatal;
}
......@@ -56,7 +56,7 @@ class FileResult {
public String file_name;
public SSRResult ssr_result;
public List<MutationResult> mutation_results;
public List<ParseOrTypeError> errors;
public List<CatalystError> errors;
}
class SSRResult {
......
......@@ -21,7 +21,6 @@
package org.shemshak.alloy.catalyst;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -47,12 +46,17 @@ import picocli.CommandLine.Command;
public final class SSR extends CatalystCommand implements Runnable {
final transient private String
OUT_TEXT = "catalyst_ssr.txt",
OUT_JSON = "catalyst_ssr.json";
OUT_JSON = "catalyst_ssr.json",
ERR_JSON = "catalyst_ssr_errors.json";
public SSR() {}
public void run() {
initialize(Paths.get(outDir, OUT_TEXT), Paths.get(outDir, OUT_JSON));
Path
ot = Paths.get(outDir, OUT_TEXT),
oj = Paths.get(outDir, OUT_JSON),
ej = Paths.get(outDir, ERR_JSON);
initialize(ot, oj, ej);
Instant start = Instant.now();
for (String corpus : corpora)
......@@ -66,21 +70,10 @@ public final class SSR extends CatalystCommand implements Runnable {
pln(String.format("total error count: %d", r.total_error_count));
pln(String.format("total elapsed time: %dms", r.total_elapsed_time));
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));
}
writeWriter(wjson, gson.toJson(r), oj);
closeWriter(wjson, oj);
writeWriter(ejson, gson.toJson(errors), ej);
closeWriter(ejson, ej);
ptxt.close();
}
......@@ -142,7 +135,7 @@ public final class SSR extends CatalystCommand implements Runnable {
protected final FileResult analyzeFile(String corpus, Path p, int index, int total) {
FileResult fr = new FileResult();
fr.file_name = p.toString();
fr.errors = new ArrayList<ParseOrTypeError>();
fr.errors = new ArrayList<CatalystError>();
String progress = String.format("[%d/%d]: ", index, total);
String finalStatus = "done";
......@@ -161,7 +154,9 @@ public final class SSR extends CatalystCommand implements Runnable {
msg = "Error in creating output json file:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
CatalystError error = new CatalystError(String.format("%s%n%s", msg, err), true);
fr.errors.add(error);
errors.put(p, error);
errorCount.getAndIncrement();
return fr;
}
......@@ -172,7 +167,7 @@ public final class SSR extends CatalystCommand implements Runnable {
A4Reporter rep = new A4Reporter() {
@Override
public void warning(ErrorWarning msg) {
fr.errors.add(new ParseOrTypeError(msg.toString().trim(), false));
fr.errors.add(new CatalystError(msg.toString().trim(), false));
}
};
......@@ -186,28 +181,17 @@ public final class SSR extends CatalystCommand implements Runnable {
String
msg = "Error in parsing or type checking. Stack trace from the exception:",
err = Utils.toString(e);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
CatalystError error = new CatalystError(String.format("%s%n%s", msg, err), true);
fr.errors.add(error);
errors.put(p, error);
errorCount.getAndIncrement();
finalStatus = "error";
}
pln(progress + finalStatus + ": " + 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));
}
writeWriter(w, gson.toJson(fr), outFile);
closeWriter(w, outFile);
return fr;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment