Search in sources :

Example 1 with AbstractReplacer

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

the class Translator method inlinePredicates.

/**
 * Returns an annotated formula f such that f.node is equivalent to
 * annotated.node with its <tt>simplified</tt> predicates replaced with their
 * corresponding Formulas and the remaining predicates replaced with equivalent
 * constraints. The annotated formula f will contain transitive source
 * information for each of the subformulas of f.node. Specifically, let t be a
 * subformula of f.node, and s be a descdendent of annotated.node from which t
 * was derived. Then, f.source[t] = annotated.source[s].
 * </p>
 *
 * @requires simplified.keySet() in
 *           annotated.predicates()[RelationPredicate.NAME]
 * @requires no disj p, p': simplified.keySet() | simplified.get(p) =
 *           simplifed.get(p') // this must hold in order to maintain the
 *           invariant that each subformula of the returned formula has exactly
 *           one source
 * @requires for each p in simplified.keySet(), the formulas "p and
 *           [[this.bounds]]" and "simplified.get(p) and [[this.bounds]]" are
 *           equisatisfiable
 * @return an annotated formula f such that f.node is equivalent to
 *         annotated.node with its <tt>simplified</tt> predicates replaced with
 *         their corresponding Formulas and the remaining predicates replaced
 *         with equivalent constraints.
 */
private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Map<RelationPredicate, Formula> simplified) {
    final Map<Node, Node> sources = new IdentityHashMap<Node, Node>();
    final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {

        private RelationPredicate source = null;

        @Override
        protected <N extends Node> N cache(N node, N replacement) {
            if (replacement instanceof Formula) {
                if (source == null) {
                    final Node nsource = annotated.sourceOf(node);
                    if (replacement != nsource)
                        sources.put(replacement, nsource);
                } else {
                    sources.put(replacement, source);
                }
            }
            return super.cache(node, replacement);
        }

        @Override
        public Formula visit(RelationPredicate pred) {
            Formula ret = lookup(pred);
            if (ret != null)
                return ret;
            source = pred;
            if (simplified.containsKey(pred)) {
                ret = simplified.get(pred).accept(this);
            } else {
                ret = pred.toConstraints().accept(this);
            }
            source = null;
            return cache(pred, ret);
        }
    };
    return annotate(annotated.node().accept(inliner), sources);
}
Also used : BooleanFormula(kodkod.engine.bool.BooleanFormula) Formula(kodkod.ast.Formula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) IdentityHashMap(java.util.IdentityHashMap) RelationPredicate(kodkod.ast.RelationPredicate) AbstractReplacer(kodkod.ast.visitor.AbstractReplacer)

Example 2 with AbstractReplacer

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

the class Translator method inlinePredicates.

/**
 * Returns an annotated formula f such that f.node is equivalent to
 * annotated.node with its <tt>truePreds</tt> replaced with the constant formula
 * TRUE and the remaining predicates replaced with equivalent constraints.
 *
 * @requires truePreds in annotated.predicates()[RelationnPredicate.NAME]
 * @requires truePreds are trivially true with respect to this.bounds
 * @return an annotated formula f such that f.node is equivalent to
 *         annotated.node with its <tt>truePreds</tt> replaced with the constant
 *         formula TRUE and the remaining predicates replaced with equivalent
 *         constraints.
 */
private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Set<RelationPredicate> truePreds) {
    final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {

        @Override
        public Formula visit(RelationPredicate pred) {
            Formula ret = lookup(pred);
            if (ret != null)
                return ret;
            return truePreds.contains(pred) ? cache(pred, Formula.TRUE) : cache(pred, pred.toConstraints());
        }
    };
    Formula x = annotated.node().accept(inliner);
    return annotate(x);
}
Also used : BooleanFormula(kodkod.engine.bool.BooleanFormula) Formula(kodkod.ast.Formula) RelationPredicate(kodkod.ast.RelationPredicate) AbstractReplacer(kodkod.ast.visitor.AbstractReplacer)

Aggregations

Formula (kodkod.ast.Formula)2 RelationPredicate (kodkod.ast.RelationPredicate)2 AbstractReplacer (kodkod.ast.visitor.AbstractReplacer)2 BooleanFormula (kodkod.engine.bool.BooleanFormula)2 IdentityHashMap (java.util.IdentityHashMap)1 Node (kodkod.ast.Node)1 AnnotatedNode (kodkod.util.nodes.AnnotatedNode)1