Search in sources :

Example 21 with StructureTerm

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

the class PRED_files_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term listHead = Prolog.Nil;
    try (RevWalk revWalk = new RevWalk(StoredValues.REPOSITORY.get(engine))) {
        RevCommit commit = revWalk.parseCommit(StoredValues.getPatchSet(engine).commitId());
        Collection<FileDiffOutput> modifiedFiles = StoredValues.DIFF_LIST.get(engine).values();
        Set<String> submodules = getAllSubmodulePaths(StoredValues.REPOSITORY.get(engine), commit, modifiedFiles);
        for (FileDiffOutput fileDiff : modifiedFiles) {
            if (fileDiff.newPath().isPresent() && Patch.isMagic(fileDiff.newPath().get())) {
                continue;
            }
            String newPath = FilePathAdapter.getNewPath(fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType());
            SymbolTerm fileNameTerm = SymbolTerm.create(newPath);
            SymbolTerm changeType = SymbolTerm.create(fileDiff.changeType().getCode());
            SymbolTerm fileType;
            if (submodules.contains(newPath)) {
                fileType = SymbolTerm.create("SUBMODULE");
            } else {
                fileType = SymbolTerm.create("REGULAR");
            }
            listHead = new ListTerm(new StructureTerm(file, fileNameTerm, changeType, fileType), listHead);
        }
    } catch (IOException ex) {
        return engine.fail();
    }
    if (!a1.unify(listHead, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
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) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 22 with StructureTerm

use of com.googlecode.prolog_cafe.lang.StructureTerm 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;
    ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
    LabelTypes types = cd.getLabelTypes();
    for (PatchSetApproval a : cd.currentApprovals()) {
        Optional<LabelType> t = types.byLabel(a.labelId());
        if (!t.isPresent()) {
            continue;
        }
        StructureTerm labelTerm = new StructureTerm(sym_label, SymbolTerm.intern(t.get().getName()), new IntegerTerm(a.value()));
        StructureTerm userTerm = new StructureTerm(sym_user, new IntegerTerm(a.accountId().get()));
        listHead = new ListTerm(new StructureTerm(sym_commit_label, labelTerm, userTerm), listHead);
    }
    if (!a1.unify(listHead, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) LabelType(com.google.gerrit.entities.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.entities.PatchSetApproval)

Example 23 with StructureTerm

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

the class PrologRuleEvaluatorIT method convertsPrologToSubmitRecord.

@Test
public void convertsPrologToSubmitRecord() {
    PrologRuleEvaluator evaluator = makeEvaluator();
    StructureTerm verifiedLabel = makeLabel(LabelId.VERIFIED, "may");
    StructureTerm labels = new StructureTerm("label", verifiedLabel);
    List<Term> terms = ImmutableList.of(makeTerm("ok", labels));
    SubmitRecord record = evaluator.resultsToSubmitRecord(null, terms);
    assertThat(record.status).isEqualTo(SubmitRecord.Status.OK);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PrologRuleEvaluator(com.google.gerrit.server.rules.PrologRuleEvaluator) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 24 with StructureTerm

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

the class PrologRuleEvaluatorIT method abortsEarlyWithOkayRecord.

/**
 * The Prolog behavior is everything but intuitive. Several submit_rules can be defined, and each
 * will provide a different SubmitRecord answer when queried. The current implementation stops
 * parsing the Prolog terms into SubmitRecord objects once it finds an OK record. This might lead
 * to tangling results, as reproduced by this test.
 *
 * <p>Let's consider this rules.pl file (equivalent to the code in this test)
 *
 * <pre>{@code
 * submit_rule(submit(R)) :-
 *     gerrit:uploader(U),
 *     R = label('Verified', reject(U)).
 *
 * submit_rule(submit(CR, V)) :-
 *     gerrit:uploader(U),
 *     V = label('Code-Review', ok(U)).
 *
 * submit_rule(submit(R)) :-
 *     gerrit:uploader(U),
 *     R = label('Any-Label-Name', reject(U)).
 * }</pre>
 *
 * The first submit_rule always fails because the Verified label is rejected.
 *
 * <p>The second submit_rule is always valid, and provides two labels: OK and Code-Review.
 *
 * <p>The third submit_rule always fails because the Any-Label-Name label is rejected.
 *
 * <p>In this case, the last two SubmitRecords are used, the first one is discarded.
 */
@Test
public void abortsEarlyWithOkayRecord() {
    PrologRuleEvaluator evaluator = makeEvaluator();
    SubmitRecord.Label submitRecordLabel1 = new SubmitRecord.Label();
    submitRecordLabel1.label = LabelId.VERIFIED;
    submitRecordLabel1.status = SubmitRecord.Label.Status.REJECT;
    submitRecordLabel1.appliedBy = admin.id();
    SubmitRecord.Label submitRecordLabel2 = new SubmitRecord.Label();
    submitRecordLabel2.label = LabelId.CODE_REVIEW;
    submitRecordLabel2.status = SubmitRecord.Label.Status.OK;
    submitRecordLabel2.appliedBy = admin.id();
    SubmitRecord.Label submitRecordLabel3 = new SubmitRecord.Label();
    submitRecordLabel3.label = "Any-Label-Name";
    submitRecordLabel3.status = SubmitRecord.Label.Status.REJECT;
    submitRecordLabel3.appliedBy = user.id();
    List<Term> terms = new ArrayList<>();
    StructureTerm label1 = makeLabel(submitRecordLabel1.label, "reject", admin);
    StructureTerm label2 = makeLabel(submitRecordLabel2.label, "ok", admin);
    StructureTerm label3 = makeLabel(submitRecordLabel3.label, "reject", user);
    terms.add(makeTerm("not_ready", makeLabels(label1)));
    terms.add(makeTerm("ok", makeLabels(label2)));
    terms.add(makeTerm("not_ready", makeLabels(label3)));
    // When
    SubmitRecord record = evaluator.resultsToSubmitRecord(null, terms);
    // assert that
    SubmitRecord expectedRecord = new SubmitRecord();
    expectedRecord.status = SubmitRecord.Status.OK;
    expectedRecord.labels = new ArrayList<>();
    expectedRecord.labels.add(submitRecordLabel2);
    expectedRecord.labels.add(submitRecordLabel3);
    assertThat(record).isEqualTo(expectedRecord);
}
Also used : SubmitRecord(com.google.gerrit.entities.SubmitRecord) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ArrayList(java.util.ArrayList) PrologRuleEvaluator(com.google.gerrit.server.rules.PrologRuleEvaluator) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 25 with StructureTerm

use of com.googlecode.prolog_cafe.lang.StructureTerm 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);
    }
    ReductionLimitException thrown = assertThrows(ReductionLimitException.class, () -> env.once(Prolog.BUILTIN, "call", new StructureTerm(":", SymbolTerm.create("user"), SymbolTerm.create("loopy"))));
    assertThat(thrown).hasMessageThat().contains("exceeded reduction limit of 1300");
}
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) ReductionLimitException(com.googlecode.prolog_cafe.exceptions.ReductionLimitException) PushbackReader(java.io.PushbackReader) Test(org.junit.Test)

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