use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class Nodes method roots.
/**
* Returns the roots of the given formula. In other words, breaks up the given
* formula into its conjunctive components, {f0, ..., fk}, such that, for all
* 0<=i<=k, f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=>
* [[formula]].
*
* @return subformulas, {f0, ..., fk}, of the given formula such that, for all
* 0<=i<=k, f<sub>i</sub> is not a conjuction and [[f0 && ... && fk]]
* <=> [[formula]].
*/
public static Set<Formula> roots(Formula formula) {
final List<Formula> formulas = new LinkedList<Formula>();
formulas.add(formula);
final ListIterator<Formula> itr = formulas.listIterator();
while (itr.hasNext()) {
final Formula f = itr.next();
if (f instanceof BinaryFormula) {
final BinaryFormula bin = (BinaryFormula) f;
if (bin.op() == FormulaOperator.AND) {
itr.remove();
itr.add(bin.left());
itr.add(bin.right());
itr.previous();
itr.previous();
}
} else if (f instanceof NaryFormula) {
final NaryFormula nf = (NaryFormula) f;
if (nf.op() == FormulaOperator.AND) {
itr.remove();
for (Formula child : nf) {
itr.add(child);
}
for (int i = nf.size(); i > 0; i--) {
itr.previous();
}
}
}
}
return new LinkedHashSet<Formula>(formulas);
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class Nodes method allRoots.
/**
* Returns all {@linkplain #roots(Formula) roots} of the given formula such that
* a node in the given collection is reachable from that root.
*
* @return { r: roots(formula) | some r.*components & descendants.elements }
*/
@SuppressWarnings("unchecked")
public static Set<Formula> allRoots(Formula formula, Collection<? extends Node> descendants) {
final Set<Node> desc = new IdentityHashSet<Node>(descendants);
final AbstractDetector detector = new AbstractDetector(Collections.EMPTY_SET) {
@Override
protected Boolean lookup(Node n) {
return desc.contains(n) ? Boolean.TRUE : cache.get(n);
}
@Override
protected Boolean cache(Node n, boolean val) {
final Boolean ret = Boolean.valueOf(val);
cache.put(n, ret);
return ret;
}
};
final Set<Formula> roots = new LinkedHashSet<Formula>();
for (Formula root : roots(formula)) {
if (root.accept(detector)) {
roots.add(root);
}
}
return roots;
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class PrettyPrinter method print.
/**
* Returns a pretty-printed string representation of the given formulas, with
* each line offset by at least the given number of whitespaces. The line
* parameter determines the length of each pretty-printed line, including the
* offset.
*
* @requires 0 <= offset < line
* @return a pretty-printed string representation of the given formulas
*/
public static String print(Set<Formula> formulas, int offset, int line) {
final Formatter formatter = new Formatter(offset, line);
for (Formula f : formulas) {
f.accept(formatter);
formatter.newline();
}
return formatter.tokens.toString();
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class AbstractWorldDefinitions method AbTransferOkay.
/**
* Returns the application of the AbTransferOkay predicate.
*
* @return application of the AbTransferOkay predicate.
*/
public Formula AbTransferOkay(Expression s, Expression sprime, Expression a_in, Expression a_out) {
final Expression e0 = a_in.join(from);
final Expression e1 = a_in.join(to);
final Formula f0 = AbWorldSecureOp(s, sprime, a_in, a_out);
final Formula f1 = Authentic(s, e0);
final Formula f2 = Authentic(s, e1);
final Formula f3 = SufficientFundsProperty(s, a_in);
final Formula f4 = e0.intersection(e1).no();
final Formula f5 = e0.join(abBalance).join(sprime).eq(e0.join(abBalance).join(s).difference(a_in.join(value)));
final Formula f6 = e0.join(abLost).join(sprime).eq(e0.join(abLost).join(s));
final Formula f7 = e1.join(abBalance).join(sprime).eq(e1.join(abBalance).join(s).union(a_in.join(value)));
final Formula f8 = e1.join(abLost).join(sprime).eq(e1.join(abLost).join(s));
final Formula f9 = Authentic(sprime, e0);
final Formula f10 = Authentic(sprime, e1);
return Formula.and(f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10);
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class AbstractWorldDefinitions method AbWorldSecureOp.
/**
* Returns the application of the AbWorldSecureOp predicate.
*
* @return application of the AbWorldSecureOp predicate.
*/
public Formula AbWorldSecureOp(Expression s, Expression sprime, Expression a_in, Expression a_out) {
final Formula f0 = AbOp(a_out);
final Formula f1 = a_in.in(TransferDetails);
final Expression e0 = a_in.join(from);
final Expression e1 = a_in.join(to);
final Expression e2 = s.join(abAuthPurse).difference(e0).difference(e1);
final Expression e3 = sprime.join(abAuthPurse).difference(e0).difference(e1);
final Formula f2 = e2.eq(e3);
final Formula f3 = XiAbPurse(s, sprime, e2);
return Formula.and(f0, f1, f2, f3);
}
Aggregations