use of com.googlecode.prolog_cafe.lang.ListTerm in project gerrit by GerritCodeReview.
the class RulesCache method prettyProlog.
private static String prettyProlog(Term at) {
StringBuilder b = new StringBuilder();
for (Object o : ((ListTerm) at).toJava()) {
if (o instanceof Term) {
Term t = (Term) o;
if (!(t instanceof StructureTerm)) {
b.append(t.toString()).append(' ');
continue;
}
switch(t.name()) {
case "atom":
SymbolTerm atom = (SymbolTerm) t.arg(0);
b.append(atom.toString());
break;
case "var":
b.append(t.arg(0).toString());
break;
}
} else {
b.append(o);
}
}
return b.toString().trim();
}
use of com.googlecode.prolog_cafe.lang.ListTerm 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();
}
}
use of com.googlecode.prolog_cafe.lang.ListTerm in project gerrit by GerritCodeReview.
the class PRED_get_legacy_label_types_1 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
List<LabelType> list;
try {
list = StoredValues.CHANGE_DATA.get(engine).getLabelTypes().getLabelTypes();
} catch (OrmException err) {
throw new JavaException(this, 1, err);
}
Term head = Prolog.Nil;
for (int idx = list.size() - 1; 0 <= idx; idx--) {
head = new ListTerm(export(list.get(idx)), head);
}
if (!a1.unify(head, engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.googlecode.prolog_cafe.lang.ListTerm 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;
try {
ChangeData cd = StoredValues.CHANGE_DATA.get(engine);
LabelTypes types = cd.getLabelTypes();
for (PatchSetApproval a : cd.currentApprovals()) {
LabelType t = types.byLabel(a.getLabelId());
if (t == null) {
continue;
}
StructureTerm labelTerm = new StructureTerm(sym_label, SymbolTerm.intern(t.getName()), new IntegerTerm(a.getValue()));
StructureTerm userTerm = new StructureTerm(sym_user, new IntegerTerm(a.getAccountId().get()));
listHead = new ListTerm(new StructureTerm(sym_commit_label, labelTerm, userTerm), listHead);
}
} catch (OrmException err) {
throw new JavaException(this, 1, err);
}
if (!a1.unify(listHead, engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.googlecode.prolog_cafe.lang.ListTerm 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);
}
Aggregations