Search in sources :

Example 36 with Term

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

the class RulesCache method consultRules.

private PrologMachineCopy consultRules(String name, Reader rules) throws CompileException {
    BufferingPrologControl ctl = newEmptyMachine(systemLoader);
    PushbackReader in = new PushbackReader(rules, Prolog.PUSHBACK_SIZE);
    try {
        if (!ctl.execute(Prolog.BUILTIN, "consult_stream", SymbolTerm.intern(name), new JavaObjectTerm(in))) {
            return null;
        }
    } catch (SyntaxException e) {
        throw new CompileException(e.toString(), e);
    } catch (TermException e) {
        Term m = e.getMessageTerm();
        if (m instanceof StructureTerm && "syntax_error".equals(m.name()) && m.arity() >= 1) {
            StringBuilder msg = new StringBuilder();
            if (m.arg(0) instanceof ListTerm) {
                msg.append(Joiner.on(' ').join(((ListTerm) m.arg(0)).toJava()));
            } else {
                msg.append(m.arg(0).toString());
            }
            if (m.arity() == 2 && m.arg(1) instanceof StructureTerm && "at".equals(m.arg(1).name())) {
                Term at = m.arg(1).arg(0).dereference();
                if (at instanceof ListTerm) {
                    msg.append(" at: ");
                    msg.append(prettyProlog(at));
                }
            }
            throw new CompileException(msg.toString(), e);
        }
        throw new CompileException("Error while consulting rules from " + name, e);
    } catch (RuntimeException e) {
        throw new CompileException("Error while consulting rules from " + name, e);
    }
    return save(ctl);
}
Also used : TermException(com.googlecode.prolog_cafe.exceptions.TermException) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) SyntaxException(com.googlecode.prolog_cafe.exceptions.SyntaxException) BufferingPrologControl(com.googlecode.prolog_cafe.lang.BufferingPrologControl) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) 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) PushbackReader(java.io.PushbackReader)

Example 37 with Term

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

the class PRED_change_topic_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term topicTerm = Prolog.Nil;
    Change change = StoredValues.getChange(engine);
    String topic = change.getTopic();
    if (topic != null) {
        topicTerm = SymbolTerm.create(topic);
    }
    if (!a1.unify(topicTerm, engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : Term(com.googlecode.prolog_cafe.lang.Term) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) Change(com.google.gerrit.entities.Change)

Example 38 with Term

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

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

the class PRED_project_default_submit_type_1 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    ProjectState projectState = StoredValues.PROJECT_STATE.get(engine);
    SubmitType submitType = projectState.getSubmitType();
    if (!a1.unify(term[submitType.ordinal()], engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : ProjectState(com.google.gerrit.server.project.ProjectState) SubmitType(com.google.gerrit.extensions.client.SubmitType) Term(com.googlecode.prolog_cafe.lang.Term) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm)

Example 40 with Term

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

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