Search in sources :

Example 6 with Term

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

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

Example 8 with Term

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

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

the class PrologTestCase method load.

protected void load(String pkg, String prologResource, Module... modules) throws CompileException, IOException {
    ArrayList<Module> moduleList = new ArrayList<>();
    moduleList.add(new PrologModule.EnvironmentModule());
    moduleList.addAll(Arrays.asList(modules));
    envFactory = Guice.createInjector(moduleList).getInstance(PrologEnvironment.Factory.class);
    PrologEnvironment env = envFactory.create(newMachine());
    consult(env, getClass(), prologResource);
    this.pkg = pkg;
    hasSetup = has(env, "setup");
    hasTeardown = has(env, "teardown");
    StructureTerm head = new StructureTerm(":", SymbolTerm.intern(pkg), new StructureTerm(test_1, new VariableTerm()));
    tests = new ArrayList<>();
    for (Term[] pair : env.all(Prolog.BUILTIN, "clause", head, new VariableTerm())) {
        tests.add(pair[0]);
    }
    assertThat(tests).isNotEmpty();
    machine = PrologMachineCopy.save(env);
}
Also used : StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ArrayList(java.util.ArrayList) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) 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) Module(com.google.inject.Module)

Example 10 with Term

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

Aggregations

Term (com.googlecode.prolog_cafe.lang.Term)43 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)37 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)30 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)27 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)20 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)17 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)13 ArrayList (java.util.ArrayList)6 SubmitRecord (com.google.gerrit.entities.SubmitRecord)4 CurrentUser (com.google.gerrit.server.CurrentUser)4 RuleEvalException (com.google.gerrit.server.project.RuleEvalException)4 ChangeData (com.google.gerrit.server.query.change.ChangeData)4 OrmException (com.google.gwtorm.server.OrmException)4 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)4 IllegalTypeException (com.googlecode.prolog_cafe.exceptions.IllegalTypeException)4 JavaException (com.googlecode.prolog_cafe.exceptions.JavaException)4 PInstantiationException (com.googlecode.prolog_cafe.exceptions.PInstantiationException)4 ReductionLimitException (com.googlecode.prolog_cafe.exceptions.ReductionLimitException)4 IOException (java.io.IOException)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4