Search in sources :

Example 6 with Node

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

the class FormulaFlattener method apply.

/**
 * Returns the result of applying this visitor to the given annotated formula.
 *
 * @return the result of applying this visitor to the given annotated formula.
 */
final AnnotatedNode<Formula> apply(AnnotatedNode<Formula> annotated) {
    annotated.node().accept(this);
    final List<Formula> roots = new ArrayList<Formula>(conjuncts.size());
    roots.addAll(conjuncts.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = conjuncts.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();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(Formula.and(roots), conjuncts);
}
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) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 7 with Node

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

the class FormulaFlattener method flatten.

/**
 * Flattens the given formula into a set of conjuncts by pushing negations
 * through quantifier-free formulas, if breakupQuantifiers is false. Otherwise,
 * pushes the negations through all formulas, breaking up universal quantifiers
 * whenever possible. The source map of the returned annotated node reflects the
 * source relationships from the descendants of the returned formula to the
 * sources of the corresponding descendants of annotated.node.
 *
 * @return a map that binds each flattened conjuncts to the corresponding
 *         subformula of annotated.node
 */
public static AnnotatedNode<Formula> flatten(AnnotatedNode<Formula> annotated, boolean breakupQuantifiers) {
    final FormulaFlattener flat = new FormulaFlattener(annotated.sharedNodes(), breakupQuantifiers);
    annotated.node().accept(flat);
    final List<Formula> roots = new ArrayList<Formula>(flat.conjuncts.size());
    roots.addAll(flat.conjuncts.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = flat.conjuncts.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();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(Formula.and(roots), flat.conjuncts);
}
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) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 8 with Node

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

the class PrenexNFConverter method toPNF.

public static AnnotatedNode<Formula> toPNF(AnnotatedNode<Formula> annotated) {
    final PrenexNFConverter pnfConv = new PrenexNFConverter(annotated.sharedNodes());
    List<Formula> conj = new ArrayList<Formula>();
    for (Formula f : Nodes.allConjuncts(annotated.node(), null)) conj.add(f.accept(pnfConv));
    Formula ans = Formula.and(conj);
    final List<Formula> roots = new ArrayList<Formula>(pnfConv.annotations.size());
    roots.addAll(pnfConv.annotations.keySet());
    for (Iterator<Map.Entry<Formula, Node>> itr = pnfConv.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();
        } else {
            entry.setValue(source);
        }
    }
    return AnnotatedNode.annotate(ans, pnfConv.annotations);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NotFormula(kodkod.ast.NotFormula) NaryFormula(kodkod.ast.NaryFormula) Node(kodkod.ast.Node) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) ArrayList(java.util.ArrayList) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with Node

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

the class Skolemizer method skolemize.

/**
 * Skolemizes the given annotated formula using the given bounds and options. If
 * Options.logTranslation is set and the formula is skolemizable, the resulting
 * annotated formula will contain transitive source information for each of its
 * subformulas. Specifically, let f be the returned annotated formula, t be a
 * descendant of f.node, and s a descendant of annotated.node from which t was
 * derived. Then, f.source[t] = annotated.source[s]. If options.logTranslation
 * is false, no source information will be recorded (i.e. f.source[t] = t for
 * all descendants t of f).
 *
 * @ensures upper bound mappings for skolem constants, if any, are added to the
 *          bounds
 * @return the skolemized version of the given formula
 * @throws NullPointerException any of the arguments are null
 * @throws IllegalArgumentException some Relation & annotated.node.^children -
 *             bounds.relations
 * @throws UnsupportedOperationException bounds is unmodifiable
 */
public static AnnotatedNode<Formula> skolemize(final AnnotatedNode<Formula> annotated, Bounds bounds, Options options) {
    if (options.logTranslation() > 0) {
        final Map<Node, Node> source = new IdentityHashMap<Node, Node>();
        final Skolemizer r = new Skolemizer(annotated, bounds, options) {

            @Override
            protected Formula source(Formula f, Node n) {
                // System.out.println("logging " + f + " <-- " + n);
                final Node nsource = annotated.sourceOf(n);
                if (f != nsource)
                    source.put(f, nsource);
                return f;
            }
        };
        final Formula f = annotated.node().accept(r);
        return f == annotated.node() ? annotated : annotate(f, source);
    } else {
        final Skolemizer r = new Skolemizer(annotated, bounds, options) {
        };
        final Formula f = annotated.node().accept(r);
        return f == annotated.node() ? annotated : annotate(f);
    }
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) IdentityHashMap(java.util.IdentityHashMap)

Example 10 with Node

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

the class ResolutionBasedProof method highLevelCore.

/**
 * {@inheritDoc}
 *
 * @see kodkod.engine.Proof#highLevelCore()
 */
@Override
public final Map<Formula, Node> highLevelCore() {
    if (coreRoots == null) {
        final RecordFilter unitFilter = new RecordFilter() {

            final IntSet coreUnits = StrategyUtils.coreUnits(solver.proof());

            final Set<Formula> roots = log().roots();

            @Override
            public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
                return roots.contains(translated) && coreUnits.contains(Math.abs(literal));
            }
        };
        coreRoots = new LinkedHashMap<Formula, Node>();
        final IntSet seenUnits = new IntTreeSet();
        for (Iterator<TranslationRecord> itr = log().replay(unitFilter); itr.hasNext(); ) {
            // it is possible that two top-level formulas have identical
            // meaning,
            // and are represented with the same core unit; in that case, we
            // want only
            // one of them in the core.
            final TranslationRecord rec = itr.next();
            if (seenUnits.add(rec.literal())) {
                coreRoots.put(rec.translated(), rec.node());
            }
        }
        coreRoots = Collections.unmodifiableMap(coreRoots);
    }
    return coreRoots;
}
Also used : Formula(kodkod.ast.Formula) IntTreeSet(kodkod.util.ints.IntTreeSet) IdentityHashSet(kodkod.util.collections.IdentityHashSet) IntSet(kodkod.util.ints.IntSet) Set(java.util.Set) TupleSet(kodkod.instance.TupleSet) IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

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