Search in sources :

Example 16 with Node

use of kodkod.ast.Node in project org.alloytools.alloy by AlloyTools.

the class AnnotatedNode method relations.

/**
 * Returns the set of all relations at the leaves of this annotated node.
 *
 * @return Relation & this.node.*components
 */
public final Set<Relation> relations() {
    final Set<Relation> relations = new IdentityHashSet<Relation>();
    final AbstractVoidVisitor visitor = new AbstractVoidVisitor() {

        private final Set<Node> visited = new IdentityHashSet<Node>(sharedNodes.size());

        @Override
        protected boolean visited(Node n) {
            return sharedNodes.contains(n) && !visited.add(n);
        }

        @Override
        public void visit(Relation relation) {
            relations.add(relation);
        }
    };
    node.accept(visitor);
    return relations;
}
Also used : IdentityHashSet(kodkod.util.collections.IdentityHashSet) AbstractVoidVisitor(kodkod.ast.visitor.AbstractVoidVisitor) Relation(kodkod.ast.Relation) Set(java.util.Set) IdentityHashSet(kodkod.util.collections.IdentityHashSet) Node(kodkod.ast.Node)

Example 17 with Node

use of kodkod.ast.Node in project org.alloytools.alloy by AlloyTools.

the class FormulaFlattener method visit.

/**
 * {@inheritDoc}
 *
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.QuantifiedFormula)
 */
@Override
public final void visit(QuantifiedFormula qf) {
    if (visited(qf))
        return;
    if (breakupQuantifiers) {
        final Quantifier quant = qf.quantifier();
        if ((!negated && quant == ALL) || (negated && quant == SOME)) {
            // may
            // break
            // down
            // further
            final Map<Formula, Node> oldConjuncts = conjuncts;
            conjuncts = new LinkedHashMap<Formula, Node>();
            qf.formula().accept(this);
            if (conjuncts.size() > 1) {
                // was broken down further
                final Decls decls = qf.decls();
                for (Map.Entry<Formula, Node> entry : conjuncts.entrySet()) {
                    oldConjuncts.put(entry.getKey().forAll(decls), entry.getValue());
                }
                conjuncts = oldConjuncts;
                return;
            } else {
                // wasn't broken down further
                conjuncts = oldConjuncts;
            }
        }
    // won't break down further
    }
    addConjunct(qf);
}
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) IntComparisonFormula(kodkod.ast.IntComparisonFormula) Decls(kodkod.ast.Decls) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) Quantifier(kodkod.ast.operator.Quantifier) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 18 with Node

use of kodkod.ast.Node in project org.alloytools.alloy by AlloyTools.

the class FullNegationPropagator method toNNF.

public static AnnotatedNode<Formula> toNNF(AnnotatedNode<Formula> annotated, Reporter reporter) {
    if (reporter != null)
        reporter.convertingToNNF();
    final FullNegationPropagator flat = new FullNegationPropagator(annotated.sharedNodes());
    annotated.node().accept(flat);
    final List<Formula> roots = new ArrayList<Formula>(flat.annotations.size());
    roots.addAll(flat.annotations.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = flat.annotations.entrySet().iterator(); itr.hasNext(); ) {
        final Map.Entry<Formula, Node> entry = itr.next();
        final Node source = annotated.sourceOf(entry.getValue());
        if (entry.getKey() == source) {
            itr.remove();
        /* TODO: what is this for? */
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(Formula.and(flat.conjuncts), flat.annotations);
}
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) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 19 with Node

use of kodkod.ast.Node in project org.alloytools.alloy by AlloyTools.

the class ReductionAndProofTest method testGranularity.

public final void testGranularity() {
    final Variable x = Variable.unary("x");
    final Variable y = Variable.unary("y");
    final Formula f0 = a.some();
    final Formula f1 = b.some();
    final Formula f2 = a.eq(b);
    final Formula f3 = x.product(y).in(Expression.UNIV.product(Expression.UNIV));
    final Formula f4 = x.eq(y);
    final Formula f5 = f3.or(f4).forSome(y.oneOf(b));
    final Formula f6 = f5.forAll(x.oneOf(a));
    final Formula f7 = f2.or(f6).not();
    final Formula f8 = b.intersection(Expression.UNIV).some();
    final Formula f9 = Formula.and(f0, f1, f7, f8);
    Set<Node> core = core(f9, 0);
    assertEquals(2, core.size());
    assertTrue(core.contains(f1));
    assertTrue(core.contains(f7));
    core = core(f9, 1);
    assertEquals(2, core.size());
    assertTrue(core.contains(f1));
    assertTrue(core.contains(f6));
    core = reduce(f9, 2);
    assertEquals(2, core.size());
    assertTrue(core.contains(f1));
    assertTrue(core.contains(f5));
    core = core(f9, 3);
    assertEquals(2, core.size());
    assertTrue(core.contains(f1));
    assertTrue(core.contains(f3));
}
Also used : Formula(kodkod.ast.Formula) Variable(kodkod.ast.Variable) Node(kodkod.ast.Node)

Aggregations

Node (kodkod.ast.Node)19 Formula (kodkod.ast.Formula)17 BinaryFormula (kodkod.ast.BinaryFormula)11 LinkedHashMap (java.util.LinkedHashMap)10 Map (java.util.Map)10 NaryFormula (kodkod.ast.NaryFormula)10 IdentityHashMap (java.util.IdentityHashMap)8 NotFormula (kodkod.ast.NotFormula)8 QuantifiedFormula (kodkod.ast.QuantifiedFormula)8 ComparisonFormula (kodkod.ast.ComparisonFormula)7 IntComparisonFormula (kodkod.ast.IntComparisonFormula)7 MultiplicityFormula (kodkod.ast.MultiplicityFormula)7 TranslationRecord (kodkod.engine.fol2sat.TranslationRecord)7 AnnotatedNode (kodkod.util.nodes.AnnotatedNode)7 ConstantFormula (kodkod.ast.ConstantFormula)6 Set (java.util.Set)5 IdentityHashSet (kodkod.util.collections.IdentityHashSet)5 ArrayList (java.util.ArrayList)4 RecordFilter (kodkod.engine.fol2sat.RecordFilter)4 LinkedHashSet (java.util.LinkedHashSet)3