use of com.google.googlejavaformat.java.FormatterException in project bayou by capergroup.
the class Synthesizer method execute.
public List<String> execute(Parser parser, String astJson) {
List<SynthesisResult> synthesizedPrograms = new ArrayList<>();
List<JSONInput> asts = getASTsFromNN(astJson);
classLoader = URLClassLoader.newInstance(parser.classpathURLs);
CompilationUnit cu = parser.cu;
List<String> programs = new ArrayList<>();
Set<String> errors = new HashSet<>();
for (JSONInput ast : asts) {
Visitor visitor = new Visitor(ast.ast, new Document(parser.source), cu, mode);
try {
cu.accept(visitor);
if (visitor.synthesizedProgram == null)
continue;
String program = visitor.synthesizedProgram.replaceAll("\\s", "");
if (!programs.contains(program)) {
String formattedProgram = new Formatter().formatSource(visitor.synthesizedProgram);
programs.add(program);
synthesizedPrograms.add(new SynthesisResult(formattedProgram, visitor.num$Variables));
}
if (// hard coded: synthesize a maximum of 10 programs
synthesizedPrograms.size() >= 10)
break;
} catch (SynthesisException e) {
errors.add(e.getMessage());
} catch (FormatterException e) {
// do nothing and try next sketch
}
}
// if no programs were synthesized, return some meaningful errors
List<String> results = new ArrayList<>();
if (synthesizedPrograms.isEmpty()) {
results.add("Your intended program could not be synthesized for the following reasons: \n- " + (asts.isEmpty() ? "No sketches were generated by the model." : String.join("\n- ", errors)));
} else {
synthesizedPrograms.sort(Comparator.comparingInt(p -> p.getNum$Variables()));
for (SynthesisResult program : synthesizedPrograms) results.add(program.getProgram());
}
return results;
}
Aggregations