use of com.googlecode.prolog_cafe.lang.Term in project gerrit by GerritCodeReview.
the class AbstractCommitUserIdentityPredicate method exec.
protected Operation exec(Prolog engine, UserIdentity userId) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Term a3 = arg3.dereference();
Term idTerm;
Term nameTerm = Prolog.Nil;
Term emailTerm = Prolog.Nil;
Account.Id id = userId.getAccount();
if (id == null) {
idTerm = anonymous;
} else {
idTerm = new IntegerTerm(id.get());
}
String name = userId.getName();
if (name != null && !name.equals("")) {
nameTerm = SymbolTerm.create(name);
}
String email = userId.getEmail();
if (email != null && !email.equals("")) {
emailTerm = SymbolTerm.create(email);
}
if (!a1.unify(new StructureTerm(user, idTerm), engine.trail)) {
return engine.fail();
}
if (!a2.unify(nameTerm, engine.trail)) {
return engine.fail();
}
if (!a3.unify(emailTerm, engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.googlecode.prolog_cafe.lang.Term 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;
}
}
use of com.googlecode.prolog_cafe.lang.Term 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;
}
use of com.googlecode.prolog_cafe.lang.Term in project gerrit by GerritCodeReview.
the class PRED_current_user_1 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
CurrentUser curUser = StoredValues.CURRENT_USER.getOrNull(engine);
if (curUser == null) {
throw new EvaluationException("Current user not available in this rule type");
}
Term resultTerm;
if (curUser.isIdentifiedUser()) {
Account.Id id = curUser.getAccountId();
resultTerm = new IntegerTerm(id.get());
} else if (curUser instanceof AnonymousUser) {
resultTerm = anonymous;
} else if (curUser instanceof PeerDaemonUser) {
resultTerm = peerDaemon;
} else {
throw new EvaluationException("Unknown user type");
}
if (!a1.unify(new StructureTerm(user, resultTerm), engine.trail)) {
return engine.fail();
}
return cont;
}
use of com.googlecode.prolog_cafe.lang.Term 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();
}
Aggregations