Search in sources :

Example 1 with BufferingPrologControl

use of com.googlecode.prolog_cafe.lang.BufferingPrologControl in project gerrit by GerritCodeReview.

the class PrologShell method run.

@Override
public int run() {
    banner();
    BufferingPrologControl pcl = new BufferingPrologControl();
    pcl.setPrologClassLoader(new PrologClassLoader(getClass().getClassLoader()));
    pcl.setEnabled(Prolog.Feature.IO, true);
    pcl.setEnabled(Prolog.Feature.STATISTICS, true);
    pcl.configureUserIO(System.in, System.out, System.err);
    pcl.initialize(Prolog.BUILTIN);
    for (String file : fileName) {
        String path;
        try {
            path = new File(file).getCanonicalPath();
        } catch (IOException e) {
            path = new File(file).getAbsolutePath();
        }
        pcl.execute(Prolog.BUILTIN, "consult", SymbolTerm.create(path));
        System.err.println();
        System.err.flush();
    }
    try {
        pcl.execute(Prolog.BUILTIN, "cafeteria");
        write("% halt\n");
        return 0;
    } catch (HaltException halt) {
        write("% halt(" + halt.getStatus() + ")\n");
        return halt.getStatus();
    }
}
Also used : HaltException(com.googlecode.prolog_cafe.exceptions.HaltException) BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) IOException(java.io.IOException) File(java.io.File) PrologClassLoader(com.googlecode.prolog_cafe.lang.PrologClassLoader)

Example 2 with BufferingPrologControl

use of com.googlecode.prolog_cafe.lang.BufferingPrologControl in project gerrit by GerritCodeReview.

the class PrologTestCase method newMachine.

private PrologMachineCopy newMachine() {
    BufferingPrologControl ctl = new BufferingPrologControl();
    ctl.setMaxDatabaseSize(16 * 1024);
    ctl.setPrologClassLoader(new PrologClassLoader(getClass().getClassLoader()));
    return PrologMachineCopy.save(ctl);
}
Also used : BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) PrologClassLoader(com.googlecode.prolog_cafe.lang.PrologClassLoader)

Example 3 with BufferingPrologControl

use of com.googlecode.prolog_cafe.lang.BufferingPrologControl in project gerrit by GerritCodeReview.

the class RulesCache method newEmptyMachine.

private BufferingPrologControl newEmptyMachine(ClassLoader cl) {
    BufferingPrologControl ctl = new BufferingPrologControl();
    ctl.setMaxDatabaseSize(maxDbSize);
    ctl.setPrologClassLoader(new PrologClassLoader(new PredicateClassLoader(predicateProviders, cl)));
    ctl.setEnabled(EnumSet.allOf(Prolog.Feature.class), false);
    List<String> packages = new ArrayList<>();
    packages.addAll(PACKAGE_LIST);
    for (PredicateProvider predicateProvider : predicateProviders) {
        packages.addAll(predicateProvider.getPackages());
    }
    // Bootstrap the interpreter and ensure there is clean state.
    ctl.initialize(packages.toArray(new String[packages.size()]));
    return ctl;
}
Also used : BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) ArrayList(java.util.ArrayList) PrologClassLoader(com.googlecode.prolog_cafe.lang.PrologClassLoader)

Example 4 with BufferingPrologControl

use of com.googlecode.prolog_cafe.lang.BufferingPrologControl in project gerrit by GerritCodeReview.

the class RulesCache method consultRules.

private PrologMachineCopy consultRules(String name, Reader rules) throws CompileException {
    BufferingPrologControl ctl = newEmptyMachine(systemLoader);
    PushbackReader in = new PushbackReader(rules, Prolog.PUSHBACK_SIZE);
    try {
        if (!ctl.execute(Prolog.BUILTIN, "consult_stream", SymbolTerm.intern(name), new JavaObjectTerm(in))) {
            return null;
        }
    } catch (SyntaxException e) {
        throw new CompileException(e.toString(), e);
    } catch (TermException e) {
        Term m = e.getMessageTerm();
        if (m instanceof StructureTerm && "syntax_error".equals(m.name()) && m.arity() >= 1) {
            StringBuilder msg = new StringBuilder();
            if (m.arg(0) instanceof ListTerm) {
                msg.append(Joiner.on(' ').join(((ListTerm) m.arg(0)).toJava()));
            } else {
                msg.append(m.arg(0).toString());
            }
            if (m.arity() == 2 && m.arg(1) instanceof StructureTerm && "at".equals(m.arg(1).name())) {
                Term at = m.arg(1).arg(0).dereference();
                if (at instanceof ListTerm) {
                    msg.append(" at: ");
                    msg.append(prettyProlog(at));
                }
            }
            throw new CompileException(msg.toString(), e);
        }
        throw new CompileException("Error while consulting rules from " + name, e);
    } catch (RuntimeException e) {
        throw new CompileException("Error while consulting rules from " + name, e);
    }
    return save(ctl);
}
Also used : TermException(com.googlecode.prolog_cafe.exceptions.TermException) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) SyntaxException(com.googlecode.prolog_cafe.exceptions.SyntaxException) BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) Term(com.googlecode.prolog_cafe.lang.Term) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PushbackReader(java.io.PushbackReader)

Aggregations

BufferingPrologControl (com.googlecode.prolog_cafe.lang.BufferingPrologControl)4 PrologClassLoader (com.googlecode.prolog_cafe.lang.PrologClassLoader)3 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)1 HaltException (com.googlecode.prolog_cafe.exceptions.HaltException)1 SyntaxException (com.googlecode.prolog_cafe.exceptions.SyntaxException)1 TermException (com.googlecode.prolog_cafe.exceptions.TermException)1 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)1 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)1 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)1 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)1 Term (com.googlecode.prolog_cafe.lang.Term)1 File (java.io.File)1 IOException (java.io.IOException)1 PushbackReader (java.io.PushbackReader)1 ArrayList (java.util.ArrayList)1