use of kodkod.ast.Expression in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(ifExpr) and returns the cached value, if any. If a replacement
* has not been cached, visits the expression's children. If nothing changes,
* the argument is cached and returned, otherwise a replacement expression is
* cached and returned.
*
* @return { i: IfExpression | i.condition = ifExpr.condition.accept(delegate)
* && i.thenExpr = ifExpr.thenExpr.accept(delegate) && i.elseExpr =
* ifExpr.elseExpr.accept(delegate) }
*/
@Override
public Expression visit(IfExpression ifExpr) {
Expression ret = lookup(ifExpr);
if (ret != null)
return ret;
final Formula condition = ifExpr.condition().accept(delegate);
final Expression thenExpr = ifExpr.thenExpr().accept(delegate);
final Expression elseExpr = ifExpr.elseExpr().accept(delegate);
ret = (condition == ifExpr.condition() && thenExpr == ifExpr.thenExpr() && elseExpr == ifExpr.elseExpr()) ? ifExpr : condition.thenElse(thenExpr, elseExpr);
return cache(ifExpr, ret);
}
use of kodkod.ast.Expression in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(binExpr) and returns the cached value, if any. If a replacement
* has not been cached, visits the expression's children. If nothing changes,
* the argument is cached and returned, otherwise a replacement expression is
* cached and returned.
*
* @return { b: BinaryExpression | b.left = binExpr.left.accept(delegate) &&
* b.right = binExpr.right.accept(delegate) && b.op = binExpr.op }
*/
@Override
public Expression visit(BinaryExpression binExpr) {
Expression ret = lookup(binExpr);
if (ret != null)
return ret;
final Expression left = binExpr.left().accept(delegate);
final Expression right = binExpr.right().accept(delegate);
ret = (left == binExpr.left() && right == binExpr.right()) ? binExpr : left.compose(binExpr.op(), right);
return cache(binExpr, ret);
}
use of kodkod.ast.Expression in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(pred) and returns the cached value, if any. If a replacement has
* not been cached, visits the formula's children. If nothing changes, the
* argument is cached and returned, otherwise a replacement formula is cached
* and returned.
*
* @return { p: RelationPredicate | p.name = pred.name && p.relation =
* pred.relation.accept(delegate) && p.name = FUNCTION => p.targetMult =
* pred.targetMult && p.domain = pred.domain.accept(delegate) && p.range
* = pred.range.accept(delegate), p.name = TOTAL_ORDERING => p.ordered =
* pred.ordered.accept(delegate) && p.first =
* pred.first.accept(delegate) && p.last = pred.last.accept(delegate) }
*/
@Override
public Formula visit(RelationPredicate pred) {
Formula ret = lookup(pred);
if (ret != null)
return ret;
final Relation r = (Relation) pred.relation().accept(delegate);
switch(pred.name()) {
case ACYCLIC:
ret = (r == pred.relation()) ? pred : r.acyclic();
break;
case FUNCTION:
final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
final Expression domain = fp.domain().accept(delegate);
final Expression range = fp.range().accept(delegate);
ret = (r == fp.relation() && domain == fp.domain() && range == fp.range()) ? fp : (fp.targetMult() == Multiplicity.ONE ? r.function(domain, range) : r.partialFunction(domain, range));
break;
case TOTAL_ORDERING:
final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
final Relation ordered = (Relation) tp.ordered().accept(delegate);
final Relation first = (Relation) tp.first().accept(delegate);
final Relation last = (Relation) tp.last().accept(delegate);
ret = (r == tp.relation() && ordered == tp.ordered() && first == tp.first() && last == tp.last()) ? tp : r.totalOrder(ordered, first, last);
break;
default:
throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
}
return cache(pred, ret);
}
use of kodkod.ast.Expression in project org.alloytools.alloy by AlloyTools.
the class BookExamples method trial.
/**
* This tries a particular solution against the formula.
*/
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
try {
frame.kr2typeCLEAR();
Bounds b = null;
TupleSet ts = null;
for (int i = 1; i < t.length; i++) {
Object x = t[i];
if (x == null)
return null;
if (x instanceof String && ((String) x).length() > 0) {
// This
// means
// it's
// a
// unary
// Tuple
// containing
// the
// given
// atom
Tuple xx = fac.tuple((String) x);
if (ts == null)
ts = fac.noneOf(1);
ts.add(xx);
continue;
}
if (x instanceof Tuple) {
// This means it's a Tuple
Tuple xx = (Tuple) x;
if (ts == null)
ts = fac.noneOf(xx.arity());
ts.add(xx);
continue;
}
if (x instanceof String) {
// The empty string means the sig
// name follows here
i++;
if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
return null;
String sigName = (String) (t[i]);
i++;
String fieldName = (String) (t[i]);
Sig first = hasSig(sigs, sigName);
if (first == null)
return null;
Expression expr = null;
if (fieldName.length() == 0) {
expr = frame.a2k(first);
} else {
for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
expr = frame.a2k(field);
while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
break;
}
}
if (!(expr instanceof Relation))
return null;
if (b == null)
// We delay the expansive
b = frame.getBounds();
// really find a possible match
if (ts == null)
ts = fac.noneOf(expr.arity());
if (!ts.containsAll(b.lowerBound((Relation) expr)))
// Sanity check
return null;
if (!b.upperBound((Relation) expr).containsAll(ts))
// Sanity check
return null;
b.boundExactly((Relation) expr, ts);
ts = null;
continue;
}
}
SATFactory sat = solver.options().solver();
Solution sol;
try {
solver.options().setSolver(SATFactory.DefaultSAT4J);
sol = solver.solve(f, b);
} finally {
solver.options().setSolver(sat);
}
if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
return null;
if (rep != null)
rep.debug("Comment: " + t[0] + "\n");
return sol;
} catch (Throwable ex) {
return null;
}
}
use of kodkod.ast.Expression in project org.alloytools.alloy by AlloyTools.
the class Handshake method puzzle.
/**
* Returns the Puzzle predicate
*
* @return
*
* <pre>
* pred Puzzle() {
* // everyone but Jocelyn has shaken a different number of hands
* all disj p,q: Person - Jocelyn | #p.shaken != #q.shaken
* // Hilary's spouse is Jocelyn
* Hilary.spouse = Jocelyn
* }
* </pre>
*/
public Formula puzzle() {
final Variable p = Variable.unary("p");
final Variable q = Variable.unary("q");
final Formula f = p.eq(q).not().implies(p.join(shaken).count().eq(q.join(shaken).count()).not());
final Expression e = Person.difference(Jocelyn);
return f.forAll(p.oneOf(e).and(q.oneOf(e))).and(Hilary.join(spouse).eq(Jocelyn));
}
Aggregations