Search in sources :

Example 1 with FormatterException

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Document(org.eclipse.jface.text.Document) URLClassLoader(java.net.URLClassLoader) java.util(java.util) edu.rice.cs.caper.bayou.core.dsl(edu.rice.cs.caper.bayou.core.dsl) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Gson(com.google.gson.Gson) Formatter(com.google.googlejavaformat.java.Formatter) FormatterException(com.google.googlejavaformat.java.FormatterException) GsonBuilder(com.google.gson.GsonBuilder) FormatterException(com.google.googlejavaformat.java.FormatterException) Formatter(com.google.googlejavaformat.java.Formatter) Document(org.eclipse.jface.text.Document)

Aggregations

Formatter (com.google.googlejavaformat.java.Formatter)1 FormatterException (com.google.googlejavaformat.java.FormatterException)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 edu.rice.cs.caper.bayou.core.dsl (edu.rice.cs.caper.bayou.core.dsl)1 URLClassLoader (java.net.URLClassLoader)1 java.util (java.util)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 Document (org.eclipse.jface.text.Document)1