Search in sources :

Example 6 with SymbolTerm

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

the class PRED__user_label_range_4 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term a2 = arg2.dereference();
    Term a3 = arg3.dereference();
    Term a4 = arg4.dereference();
    if (a1 instanceof VariableTerm) {
        throw new PInstantiationException(this, 1);
    }
    if (!(a1 instanceof SymbolTerm)) {
        throw new IllegalTypeException(this, 1, "atom", a1);
    }
    String label = a1.name();
    if (a2 instanceof VariableTerm) {
        throw new PInstantiationException(this, 2);
    }
    if (!(a2 instanceof JavaObjectTerm) || !a2.convertible(CurrentUser.class)) {
        throw new IllegalTypeException(this, 2, "CurrentUser)", a2);
    }
    CurrentUser user = (CurrentUser) ((JavaObjectTerm) a2).object();
    Set<LabelPermission.WithValue> can;
    try {
        ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
        LabelType type = cd.getLabelTypes().byLabel(label);
        if (type == null) {
            return engine.fail();
        }
        can = StoredValues.PERMISSION_BACKEND.get(engine).user(user).change(cd).test(type);
    } catch (OrmException err) {
        throw new JavaException(this, 1, err);
    } catch (PermissionBackendException err) {
        SystemException se = new SystemException(err.getMessage());
        se.initCause(err);
        throw se;
    }
    int min = 0;
    int max = 0;
    for (LabelPermission.WithValue v : can) {
        min = Math.min(min, v.value());
        max = Math.max(max, v.value());
    }
    if (!a3.unify(new IntegerTerm(min), engine.trail)) {
        return engine.fail();
    }
    if (!a4.unify(new IntegerTerm(max), engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : JavaException(com.googlecode.prolog_cafe.exceptions.JavaException) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) CurrentUser(com.google.gerrit.server.CurrentUser) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ChangeData(com.google.gerrit.server.query.change.ChangeData) PInstantiationException(com.googlecode.prolog_cafe.exceptions.PInstantiationException) SystemException(com.googlecode.prolog_cafe.exceptions.SystemException) OrmException(com.google.gwtorm.server.OrmException) LabelType(com.google.gerrit.common.data.LabelType) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) LabelPermission(com.google.gerrit.server.permissions.LabelPermission)

Example 7 with SymbolTerm

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

the class SubmitRuleEvaluator method getSubmitType.

/**
   * Evaluate the submit type rules to get the submit type.
   *
   * @return record from the evaluated rules.
   */
public SubmitTypeRecord getSubmitType() {
    initOptions();
    try {
        initPatchSet();
    } catch (OrmException e) {
        return typeError("Error looking up patch set " + control.getChange().currentPatchSetId(), e);
    }
    try {
        if (control.getChange().getStatus() == Change.Status.DRAFT && !control.isDraftVisible(cd.db(), cd)) {
            return SubmitTypeRecord.error("Patch set " + patchSet.getId() + " not found");
        }
        if (patchSet.isDraft() && !control.isDraftVisible(cd.db(), cd)) {
            return SubmitTypeRecord.error("Patch set " + patchSet.getId() + " not found");
        }
    } catch (OrmException err) {
        String msg = "Cannot read patch set " + patchSet.getId();
        log.error(msg, err);
        return SubmitTypeRecord.error(msg);
    }
    List<Term> results;
    try {
        results = evaluateImpl("locate_submit_type", "get_submit_type", "locate_submit_type_filter", "filter_submit_type_results", // have a consistent view of the submit type.
        null);
    } catch (RuleEvalException e) {
        return typeError(e.getMessage(), e);
    }
    if (results.isEmpty()) {
        // Should never occur for a well written rule
        return typeError("Submit rule '" + getSubmitRuleName() + "' for change " + cd.getId() + " of " + getProjectName() + " has no solution.");
    }
    Term typeTerm = results.get(0);
    if (!(typeTerm instanceof SymbolTerm)) {
        return typeError("Submit rule '" + getSubmitRuleName() + "' for change " + cd.getId() + " of " + getProjectName() + " did not return a symbol.");
    }
    String typeName = ((SymbolTerm) typeTerm).name();
    try {
        return SubmitTypeRecord.OK(SubmitType.valueOf(typeName.toUpperCase()));
    } catch (IllegalArgumentException e) {
        return typeError("Submit type rule " + getSubmitRule() + " for change " + cd.getId() + " of " + getProjectName() + " output invalid result: " + typeName);
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) 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)

Example 8 with SymbolTerm

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

the class GerritCommonTest method reductionLimit.

@Test
public void reductionLimit() throws Exception {
    PrologEnvironment env = envFactory.create(machine);
    setUpEnvironment(env);
    String script = "loopy :- b(5).\nb(N) :- N > 0, !, S = N - 1, b(S).\nb(_) :- true.\n";
    SymbolTerm nameTerm = SymbolTerm.create("testReductionLimit");
    JavaObjectTerm inTerm = new JavaObjectTerm(new PushbackReader(new StringReader(script), Prolog.PUSHBACK_SIZE));
    if (!env.execute(Prolog.BUILTIN, "consult_stream", nameTerm, inTerm)) {
        throw new CompileException("Cannot consult " + nameTerm);
    }
    exception.expect(ReductionLimitException.class);
    exception.expectMessage("exceeded reduction limit of 1300");
    env.once(Prolog.BUILTIN, "call", new StructureTerm(":", SymbolTerm.create("user"), SymbolTerm.create("loopy")));
}
Also used : StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) StringReader(java.io.StringReader) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

Aggregations

SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)8 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)6 Term (com.googlecode.prolog_cafe.lang.Term)6 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)4 OrmException (com.google.gwtorm.server.OrmException)3 IllegalTypeException (com.googlecode.prolog_cafe.exceptions.IllegalTypeException)3 PInstantiationException (com.googlecode.prolog_cafe.exceptions.PInstantiationException)3 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)3 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)3 LabelType (com.google.gerrit.common.data.LabelType)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 LabelPermission (com.google.gerrit.server.permissions.LabelPermission)2 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 JavaException (com.googlecode.prolog_cafe.exceptions.JavaException)2 SystemException (com.googlecode.prolog_cafe.exceptions.SystemException)2 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)2 PushbackReader (java.io.PushbackReader)2 AuthException (com.google.gerrit.extensions.restapi.AuthException)1