Search in sources :

Example 1 with ListTerm

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

the class RulesCache method prettyProlog.

private static String prettyProlog(Term at) {
    StringBuilder b = new StringBuilder();
    for (Object o : ((ListTerm) at).toJava()) {
        if (o instanceof Term) {
            Term t = (Term) o;
            if (!(t instanceof StructureTerm)) {
                b.append(t.toString()).append(' ');
                continue;
            }
            switch(t.name()) {
                case "atom":
                    SymbolTerm atom = (SymbolTerm) t.arg(0);
                    b.append(atom.toString());
                    break;
                case "var":
                    b.append(t.arg(0).toString());
                    break;
            }
        } else {
            b.append(o);
        }
    }
    return b.toString().trim();
}
Also used : ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) 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)

Example 2 with ListTerm

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

the class SubmitRuleEvaluator method evaluateImpl.

private List<Term> evaluateImpl(String userRuleLocatorName, String userRuleWrapperName, String filterRuleLocatorName, String filterRuleWrapperName, CurrentUser user) throws RuleEvalException {
    PrologEnvironment env = getPrologEnvironment(user);
    try {
        Term sr = env.once("gerrit", userRuleLocatorName, new VariableTerm());
        if (opts.fastEvalLabels()) {
            env.once("gerrit", "assume_range_from_label");
        }
        List<Term> results = new ArrayList<>();
        try {
            for (Term[] template : env.all("gerrit", userRuleWrapperName, sr, new VariableTerm())) {
                results.add(template[1]);
            }
        } catch (ReductionLimitException err) {
            throw new RuleEvalException(String.format("%s on change %d of %s", err.getMessage(), cd.getId().get(), getProjectName()));
        } catch (RuntimeException err) {
            throw new RuleEvalException(String.format("Exception calling %s on change %d of %s", sr, cd.getId().get(), getProjectName()), err);
        } finally {
            reductionsConsumed = env.getReductions();
        }
        Term resultsTerm = toListTerm(results);
        if (!opts.skipFilters()) {
            resultsTerm = runSubmitFilters(resultsTerm, env, filterRuleLocatorName, filterRuleWrapperName);
        }
        List<Term> r;
        if (resultsTerm instanceof ListTerm) {
            r = new ArrayList<>();
            for (Term t = resultsTerm; t instanceof ListTerm; ) {
                ListTerm l = (ListTerm) t;
                r.add(l.car().dereference());
                t = l.cdr().dereference();
            }
        } else {
            r = Collections.emptyList();
        }
        submitRule = sr;
        return r;
    } finally {
        env.close();
    }
}
Also used : ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) ArrayList(java.util.ArrayList) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ReductionLimitException(com.googlecode.prolog_cafe.exceptions.ReductionLimitException) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PrologEnvironment(com.google.gerrit.rules.PrologEnvironment)

Example 3 with ListTerm

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

the class PRED_get_legacy_label_types_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    List<LabelType> list;
    try {
        list = StoredValues.CHANGE_DATA.get(engine).getLabelTypes().getLabelTypes();
    } catch (OrmException err) {
        throw new JavaException(this, 1, err);
    }
    Term head = Prolog.Nil;
    for (int idx = list.size() - 1; 0 <= idx; idx--) {
        head = new ListTerm(export(list.get(idx)), head);
    }
    if (!a1.unify(head, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : JavaException(com.googlecode.prolog_cafe.exceptions.JavaException) OrmException(com.google.gwtorm.server.OrmException) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) 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)

Example 4 with ListTerm

use of com.googlecode.prolog_cafe.lang.ListTerm 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 5 with ListTerm

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

ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)5 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)5 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)5 Term (com.googlecode.prolog_cafe.lang.Term)5 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)3 LabelType (com.google.gerrit.common.data.LabelType)2 OrmException (com.google.gwtorm.server.OrmException)2 JavaException (com.googlecode.prolog_cafe.exceptions.JavaException)2 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)2 LabelTypes (com.google.gerrit.common.data.LabelTypes)1 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)1 PrologEnvironment (com.google.gerrit.rules.PrologEnvironment)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)1 ReductionLimitException (com.googlecode.prolog_cafe.exceptions.ReductionLimitException)1 SyntaxException (com.googlecode.prolog_cafe.exceptions.SyntaxException)1 TermException (com.googlecode.prolog_cafe.exceptions.TermException)1 BufferingPrologControl (com.googlecode.prolog_cafe.lang.BufferingPrologControl)1 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)1 PushbackReader (java.io.PushbackReader)1