Search in sources :

Example 1 with VariableTerm

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

the class SubmitRuleEvaluator method runSubmitFilters.

private Term runSubmitFilters(Term results, PrologEnvironment env, String filterRuleLocatorName, String filterRuleWrapperName) throws RuleEvalException {
    ProjectState projectState = control.getProjectControl().getProjectState();
    PrologEnvironment childEnv = env;
    for (ProjectState parentState : projectState.parents()) {
        PrologEnvironment parentEnv;
        try {
            parentEnv = parentState.newPrologEnvironment();
        } catch (CompileException err) {
            throw new RuleEvalException("Cannot consult rules.pl for " + parentState.getProject().getName(), err);
        }
        parentEnv.copyStoredValues(childEnv);
        Term filterRule = parentEnv.once("gerrit", filterRuleLocatorName, new VariableTerm());
        try {
            if (opts.fastEvalLabels()) {
                env.once("gerrit", "assume_range_from_label");
            }
            Term[] template = parentEnv.once("gerrit", filterRuleWrapperName, filterRule, results, new VariableTerm());
            results = template[2];
        } catch (ReductionLimitException err) {
            throw new RuleEvalException(String.format("%s on change %d of %s", err.getMessage(), cd.getId().get(), parentState.getProject().getName()));
        } catch (RuntimeException err) {
            throw new RuleEvalException(String.format("Exception calling %s on change %d of %s", filterRule, cd.getId().get(), parentState.getProject().getName()), err);
        } finally {
            reductionsConsumed += env.getReductions();
        }
        childEnv = parentEnv;
    }
    return results;
}
Also used : CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) 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 2 with VariableTerm

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

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

the class PRED_commit_delta_4 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.cont = cont;
    engine.setB0();
    Term a1 = arg1.dereference();
    if (a1 instanceof VariableTerm) {
        throw new PInstantiationException(this, 1);
    }
    if (!(a1 instanceof SymbolTerm)) {
        throw new IllegalTypeException(this, 1, "symbol", a1);
    }
    Pattern regex = Pattern.compile(a1.name());
    engine.r1 = new JavaObjectTerm(regex);
    engine.r2 = arg2;
    engine.r3 = arg3;
    engine.r4 = arg4;
    PatchList pl = StoredValues.PATCH_LIST.get(engine);
    Iterator<PatchListEntry> iter = pl.getPatches().iterator();
    engine.r5 = new JavaObjectTerm(iter);
    return engine.jtry5(commit_delta_check, commit_delta_next);
}
Also used : Pattern(java.util.regex.Pattern) PatchList(com.google.gerrit.server.patch.PatchList) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) PatchListEntry(com.google.gerrit.server.patch.PatchListEntry) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) 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) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) PInstantiationException(com.googlecode.prolog_cafe.exceptions.PInstantiationException)

Example 4 with VariableTerm

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

the class PRED__check_user_label_3 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term a2 = arg2.dereference();
    Term a3 = arg3.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();
    if (a3 instanceof VariableTerm) {
        throw new PInstantiationException(this, 3);
    }
    if (!(a3 instanceof IntegerTerm)) {
        throw new IllegalTypeException(this, 3, "integer", a3);
    }
    short val = (short) ((IntegerTerm) a3).intValue();
    try {
        ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
        LabelType type = cd.getLabelTypes().byLabel(label);
        if (type == null) {
            return engine.fail();
        }
        StoredValues.PERMISSION_BACKEND.get(engine).user(user).change(cd).check(new LabelPermission.WithValue(type, val));
        return cont;
    } catch (OrmException err) {
        throw new JavaException(this, 1, err);
    } catch (AuthException err) {
        return engine.fail();
    } catch (PermissionBackendException err) {
        SystemException se = new SystemException(err.getMessage());
        se.initCause(err);
        throw se;
    }
}
Also used : IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) JavaException(com.googlecode.prolog_cafe.exceptions.JavaException) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) CurrentUser(com.google.gerrit.server.CurrentUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) 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 5 with VariableTerm

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

Aggregations

SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)7 Term (com.googlecode.prolog_cafe.lang.Term)7 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)7 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)5 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)5 PInstantiationException (com.googlecode.prolog_cafe.exceptions.PInstantiationException)4 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)4 IllegalTypeException (com.googlecode.prolog_cafe.exceptions.IllegalTypeException)3 LabelType (com.google.gerrit.common.data.LabelType)2 PrologEnvironment (com.google.gerrit.rules.PrologEnvironment)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 OrmException (com.google.gwtorm.server.OrmException)2 JavaException (com.googlecode.prolog_cafe.exceptions.JavaException)2 ReductionLimitException (com.googlecode.prolog_cafe.exceptions.ReductionLimitException)2 SystemException (com.googlecode.prolog_cafe.exceptions.SystemException)2 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)2 ArrayList (java.util.ArrayList)2