Search in sources :

Example 11 with StructureTerm

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

the class PrologTestCase method runPrologBasedTests.

public void runPrologBasedTests() throws Exception {
    int errors = 0;
    long start = TimeUtil.nowMs();
    for (Term test : tests) {
        PrologEnvironment env = envFactory.create(machine);
        setUpEnvironment(env);
        env.setEnabled(Prolog.Feature.IO, true);
        System.out.format("Prolog %-60s ...", removePackage(test));
        System.out.flush();
        if (hasSetup) {
            call(env, "setup");
        }
        List<Term> all = env.all(Prolog.BUILTIN, "call", test);
        if (hasTeardown) {
            call(env, "teardown");
        }
        System.out.println(all.size() == 1 ? "OK" : "FAIL");
        if (all.size() > 0 && !test.equals(all.get(0))) {
            for (Term t : all) {
                Term head = ((StructureTerm) removePackage(t)).args()[0];
                Term[] args = ((StructureTerm) head).args();
                System.out.print("  Result: ");
                for (int i = 0; i < args.length; i++) {
                    if (0 < i) {
                        System.out.print(", ");
                    }
                    System.out.print(args[i]);
                }
                System.out.println();
            }
            System.out.println();
        }
        if (all.size() != 1) {
            errors++;
        }
    }
    long end = TimeUtil.nowMs();
    System.out.println("-------------------------------");
    System.out.format("Prolog tests: %d, Failures: %d, Time elapsed %.3f sec", tests.size(), errors, (end - start) / 1000.0);
    System.out.println();
    assertThat(errors).isEqualTo(0);
}
Also used : StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Term(com.googlecode.prolog_cafe.lang.Term) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm)

Example 12 with StructureTerm

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

the class PRED__load_commit_labels_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term listHead = Prolog.Nil;
    try {
        ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
        LabelTypes types = cd.getLabelTypes();
        for (PatchSetApproval a : cd.currentApprovals()) {
            LabelType t = types.byLabel(a.getLabelId());
            if (t == null) {
                continue;
            }
            StructureTerm labelTerm = new StructureTerm(sym_label, SymbolTerm.intern(t.getName()), new IntegerTerm(a.getValue()));
            StructureTerm userTerm = new StructureTerm(sym_user, new IntegerTerm(a.getAccountId().get()));
            listHead = new ListTerm(new StructureTerm(sym_commit_label, labelTerm, userTerm), listHead);
        }
    } catch (OrmException err) {
        throw new JavaException(this, 1, err);
    }
    if (!a1.unify(listHead, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : LabelTypes(com.google.gerrit.common.data.LabelTypes) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) JavaException(com.googlecode.prolog_cafe.exceptions.JavaException) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) OrmException(com.google.gwtorm.server.OrmException) LabelType(com.google.gerrit.common.data.LabelType) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) ChangeData(com.google.gerrit.server.query.change.ChangeData) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval)

Example 13 with StructureTerm

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

the class PRED_current_user_2 method createUser.

public Term createUser(Prolog engine, Term key) {
    if (!(key instanceof StructureTerm) || key.arity() != 1 || !((StructureTerm) key).functor().equals(user)) {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    Term idTerm = key.arg(0);
    CurrentUser user;
    if (idTerm instanceof IntegerTerm) {
        Map<Account.Id, IdentifiedUser> cache = StoredValues.USERS.get(engine);
        Account.Id accountId = new Account.Id(((IntegerTerm) idTerm).intValue());
        user = cache.get(accountId);
        if (user == null) {
            IdentifiedUser.GenericFactory userFactory = userFactory(engine);
            IdentifiedUser who = userFactory.create(accountId);
            cache.put(accountId, who);
            user = who;
        }
    } else if (idTerm.equals(anonymous)) {
        user = StoredValues.ANONYMOUS_USER.get(engine);
    } else {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    return new JavaObjectTerm(user);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) CurrentUser(com.google.gerrit.server.CurrentUser) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 14 with StructureTerm

use of com.googlecode.prolog_cafe.lang.StructureTerm 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

StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)14 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)12 Term (com.googlecode.prolog_cafe.lang.Term)11 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)8 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)6 Account (com.google.gerrit.reviewdb.client.Account)5 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)4 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)4 CurrentUser (com.google.gerrit.server.CurrentUser)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 PushbackReader (java.io.PushbackReader)2 ArrayList (java.util.ArrayList)2 LabelType (com.google.gerrit.common.data.LabelType)1 LabelTypes (com.google.gerrit.common.data.LabelTypes)1 LabelValue (com.google.gerrit.common.data.LabelValue)1 SubmitRecord (com.google.gerrit.common.data.SubmitRecord)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)1 AnonymousUser (com.google.gerrit.server.AnonymousUser)1 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)1