Search in sources :

Example 31 with Formula

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);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) NaryFormula(kodkod.ast.NaryFormula) LinkedList(java.util.LinkedList)

Example 32 with Formula

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) AbstractDetector(kodkod.ast.visitor.AbstractDetector) Node(kodkod.ast.Node)

Example 33 with Formula

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();
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 34 with Formula

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);
}
Also used : Formula(kodkod.ast.Formula) Expression(kodkod.ast.Expression)

Example 35 with Formula

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);
}
Also used : Formula(kodkod.ast.Formula) Expression(kodkod.ast.Expression)

Aggregations

Formula (kodkod.ast.Formula)346 Variable (kodkod.ast.Variable)151 Solution (kodkod.engine.Solution)101 Expression (kodkod.ast.Expression)95 Bounds (kodkod.instance.Bounds)83 QuantifiedFormula (kodkod.ast.QuantifiedFormula)72 Solver (kodkod.engine.Solver)67 BinaryFormula (kodkod.ast.BinaryFormula)50 NaryFormula (kodkod.ast.NaryFormula)49 Relation (kodkod.ast.Relation)45 NotFormula (kodkod.ast.NotFormula)43 ComparisonFormula (kodkod.ast.ComparisonFormula)40 IntExpression (kodkod.ast.IntExpression)40 IntComparisonFormula (kodkod.ast.IntComparisonFormula)39 MultiplicityFormula (kodkod.ast.MultiplicityFormula)39 Universe (kodkod.instance.Universe)37 ArrayList (java.util.ArrayList)35 TupleFactory (kodkod.instance.TupleFactory)35 ConstantFormula (kodkod.ast.ConstantFormula)29 TupleSet (kodkod.instance.TupleSet)29