Search in sources :

Example 1 with StructureTerm

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

the class AbstractCommitUserIdentityPredicate method exec.

protected Operation exec(Prolog engine, UserIdentity userId) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term a2 = arg2.dereference();
    Term a3 = arg3.dereference();
    Term idTerm;
    Term nameTerm = Prolog.Nil;
    Term emailTerm = Prolog.Nil;
    Account.Id id = userId.getAccount();
    if (id == null) {
        idTerm = anonymous;
    } else {
        idTerm = new IntegerTerm(id.get());
    }
    String name = userId.getName();
    if (name != null && !name.equals("")) {
        nameTerm = SymbolTerm.create(name);
    }
    String email = userId.getEmail();
    if (email != null && !email.equals("")) {
        emailTerm = SymbolTerm.create(email);
    }
    if (!a1.unify(new StructureTerm(user, idTerm), engine.trail)) {
        return engine.fail();
    }
    if (!a2.unify(nameTerm, engine.trail)) {
        return engine.fail();
    }
    if (!a3.unify(emailTerm, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm)

Example 2 with StructureTerm

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

the class PRED_current_user_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    CurrentUser curUser = StoredValues.CURRENT_USER.getOrNull(engine);
    if (curUser == null) {
        throw new EvaluationException("Current user not available in this rule type");
    }
    Term resultTerm;
    if (curUser.isIdentifiedUser()) {
        Account.Id id = curUser.getAccountId();
        resultTerm = new IntegerTerm(id.get());
    } else if (curUser instanceof AnonymousUser) {
        resultTerm = anonymous;
    } else if (curUser instanceof PeerDaemonUser) {
        resultTerm = peerDaemon;
    } else {
        throw new EvaluationException("Unknown user type");
    }
    if (!a1.unify(new StructureTerm(user, resultTerm), engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) CurrentUser(com.google.gerrit.server.CurrentUser) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) AnonymousUser(com.google.gerrit.server.AnonymousUser) 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) EvaluationException(com.googlecode.prolog_cafe.exceptions.EvaluationException) PeerDaemonUser(com.google.gerrit.server.PeerDaemonUser)

Example 3 with StructureTerm

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

the class PrologTestCase method call.

private void call(BufferingPrologControl env, String name) {
    StructureTerm head = SymbolTerm.create(pkg, name, 0);
    assert_().withFailureMessage("Cannot invoke " + pkg + ":" + name).that(env.execute(Prolog.BUILTIN, "call", head)).isTrue();
}
Also used : StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm)

Example 4 with StructureTerm

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

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

the class SubmitRuleEvaluator method resultsToSubmitRecord.

/**
   * Convert the results from Prolog Cafe's format to Gerrit's common format.
   *
   * <p>can_submit/1 terminates when an ok(P) record is found. Therefore walk the results backwards,
   * using only that ok(P) record if it exists. This skips partial results that occur early in the
   * output. Later after the loop the out collection is reversed to restore it to the original
   * ordering.
   */
private List<SubmitRecord> resultsToSubmitRecord(Term submitRule, List<Term> results) {
    List<SubmitRecord> out = new ArrayList<>(results.size());
    for (int resultIdx = results.size() - 1; 0 <= resultIdx; resultIdx--) {
        Term submitRecord = results.get(resultIdx);
        SubmitRecord rec = new SubmitRecord();
        out.add(rec);
        if (!(submitRecord instanceof StructureTerm) || 1 != submitRecord.arity()) {
            return invalidResult(submitRule, submitRecord);
        }
        if ("ok".equals(submitRecord.name())) {
            rec.status = SubmitRecord.Status.OK;
        } else if ("not_ready".equals(submitRecord.name())) {
            rec.status = SubmitRecord.Status.NOT_READY;
        } else {
            return invalidResult(submitRule, submitRecord);
        }
        // Unpack the one argument. This should also be a structure with one
        // argument per label that needs to be reported on to the caller.
        //
        submitRecord = submitRecord.arg(0);
        if (!(submitRecord instanceof StructureTerm)) {
            return invalidResult(submitRule, submitRecord);
        }
        rec.labels = new ArrayList<>(submitRecord.arity());
        for (Term state : ((StructureTerm) submitRecord).args()) {
            if (!(state instanceof StructureTerm) || 2 != state.arity() || !"label".equals(state.name())) {
                return invalidResult(submitRule, submitRecord);
            }
            SubmitRecord.Label lbl = new SubmitRecord.Label();
            rec.labels.add(lbl);
            lbl.label = state.arg(0).name();
            Term status = state.arg(1);
            try {
                if ("ok".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.OK;
                    appliedBy(lbl, status);
                } else if ("reject".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.REJECT;
                    appliedBy(lbl, status);
                } else if ("need".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.NEED;
                } else if ("may".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.MAY;
                } else if ("impossible".equals(status.name())) {
                    lbl.status = SubmitRecord.Label.Status.IMPOSSIBLE;
                } else {
                    return invalidResult(submitRule, submitRecord);
                }
            } catch (UserTermExpected e) {
                return invalidResult(submitRule, submitRecord, e.getMessage());
            }
        }
        if (rec.status == SubmitRecord.Status.OK) {
            break;
        }
    }
    Collections.reverse(out);
    return out;
}
Also used : SubmitRecord(com.google.gerrit.common.data.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ArrayList(java.util.ArrayList) 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)

Aggregations

StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)25 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)20 Term (com.googlecode.prolog_cafe.lang.Term)20 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)12 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)11 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)8 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)7 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)4 PushbackReader (java.io.PushbackReader)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Account (com.google.gerrit.entities.Account)3 SubmitRecord (com.google.gerrit.entities.SubmitRecord)3 Account (com.google.gerrit.reviewdb.client.Account)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 PrologRuleEvaluator (com.google.gerrit.server.rules.PrologRuleEvaluator)2 Module (com.google.inject.Module)2 SyntaxException (com.googlecode.prolog_cafe.exceptions.SyntaxException)2 TermException (com.googlecode.prolog_cafe.exceptions.TermException)2