Search in sources :

Example 1 with Node

use of kodkod.ast.Node 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 Node

use of kodkod.ast.Node 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 3 with Node

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

the class StrategyUtils method roots.

/**
 * Returns a map from variables to the corresponding roots of log.formula.
 *
 * @return
 *
 *         <pre>
 *
 * { v: int, f: Formula | some r: log.records |
 *   r.translated in log.roots() and
 *   r.translated = f and
 *   r.env.isEmpty() and
 *   abs(r.literal) != Integer.MAX_VALUE and
 *   v = abs(r.literal) and
 *   no r': log.records | r'.node = r.node && log.replay.r' > log.replay.r }
 *         </pre>
 */
static SparseSequence<Formula> roots(TranslationLog log) {
    final SparseSequence<Formula> rootVars = new TreeSequence<Formula>();
    final Set<Formula> roots = log.roots();
    final Map<Formula, int[]> maxRootVar = new IdentityHashMap<Formula, int[]>(roots.size());
    final RecordFilter filter = new RecordFilter() {

        @Override
        public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
            return roots.contains(translated) && env.isEmpty();
        }
    };
    for (Iterator<TranslationRecord> itr = log.replay(filter); itr.hasNext(); ) {
        TranslationRecord record = itr.next();
        int[] var = maxRootVar.get(record.translated());
        if (var == null) {
            var = new int[1];
            maxRootVar.put(record.translated(), var);
        }
        var[0] = StrictMath.abs(record.literal());
    }
    for (Map.Entry<Formula, int[]> entry : maxRootVar.entrySet()) {
        final int topVar = entry.getValue()[0];
        if (// formula simplified to TRUE
        topVar != Integer.MAX_VALUE)
            rootVars.put(topVar, entry.getKey());
    }
    return rootVars;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) Formula(kodkod.ast.Formula) TreeSequence(kodkod.util.ints.TreeSequence) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 4 with Node

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

the class StrategyUtils method rootVars.

/**
 * Returns the variables that correspond to the roots of log.formula.
 *
 * @return
 *
 *         <pre>
 *
 * { v: int | some r: log.records |
 *   r.node in log.roots() and
 *   r.env.isEmpty() and
 *   abs(r.literal) != Integer.MAX_VALUE and
 *   v = abs(r.literal) and
 *   no r': log.records | r'.node = r.node && log.replay.r' > log.replay.r }
 *         </pre>
 */
public static IntSet rootVars(TranslationLog log) {
    final IntSet rootVars = new IntTreeSet();
    final Set<Formula> roots = log.roots();
    final Map<Formula, int[]> maxRootVar = new LinkedHashMap<Formula, int[]>(roots.size());
    final RecordFilter filter = new RecordFilter() {

        @Override
        public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) {
            return roots.contains(translated) && env.isEmpty();
        }
    };
    for (Iterator<TranslationRecord> itr = log.replay(filter); itr.hasNext(); ) {
        TranslationRecord record = itr.next();
        int[] var = maxRootVar.get(record.translated());
        if (var == null) {
            var = new int[1];
            maxRootVar.put(record.translated(), var);
        }
        var[0] = StrictMath.abs(record.literal());
    }
    for (int[] var : maxRootVar.values()) {
        int topVar = var[0];
        if (// formula simplified to TRUE
        topVar != Integer.MAX_VALUE)
            rootVars.add(var[0]);
    }
    return rootVars;
}
Also used : IntSet(kodkod.util.ints.IntSet) IntTreeSet(kodkod.util.ints.IntTreeSet) Node(kodkod.ast.Node) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) LinkedHashMap(java.util.LinkedHashMap) Formula(kodkod.ast.Formula) IdentityHashMap(java.util.IdentityHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RecordFilter(kodkod.engine.fol2sat.RecordFilter)

Example 5 with Node

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

the class A4Solution method solve.

// ===================================================================================================//
/**
 * Solve for the solution if not solved already; if cmd==null, we will simply
 * use the lowerbound of each relation as its value.
 */
A4Solution solve(final A4Reporter rep, Command cmd, Simplifier simp, boolean tryBookExamples) throws Err, IOException {
    // If already solved, then return this object as is
    if (solved)
        return this;
    // the lower bound of each relation
    if (cmd == null) {
        Instance inst = new Instance(bounds.universe());
        for (int max = max(), i = min(); i <= max; i++) {
            Tuple it = factory.tuple("" + i);
            inst.add(i, factory.range(it, it));
        }
        for (Relation r : bounds.relations()) inst.add(r, bounds.lowerBound(r));
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
        solved();
        return this;
    }
    // Otherwise, prepare to do the solve...
    final A4Options opt = originalOptions;
    long time = System.currentTimeMillis();
    rep.debug("Simplifying the bounds...\n");
    if (opt.inferPartialInstance && simp != null && formulas.size() > 0 && !simp.simplify(rep, this, formulas))
        addFormula(Formula.FALSE, Pos.UNKNOWN);
    rep.translate(opt.solver.id(), bitwidth, maxseq, solver.options().skolemDepth(), solver.options().symmetryBreaking());
    Formula fgoal = Formula.and(formulas);
    rep.debug("Generating the solution...\n");
    kEnumerator = null;
    Solution sol = null;
    final Reporter oldReporter = solver.options().reporter();
    final boolean[] solved = new boolean[] { true };
    solver.options().setReporter(new // Set up a
    AbstractReporter() {

        // reporter to
        // catch the
        // type+pos of
        // skolems
        @Override
        public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
            try {
                Type t = kv2typepos(decl.variable()).a;
                if (t == Type.EMPTY)
                    return;
                for (int i = (predecl == null ? -1 : predecl.size() - 1); i >= 0; i--) {
                    Type pp = kv2typepos(predecl.get(i).variable()).a;
                    if (pp == Type.EMPTY)
                        return;
                    t = pp.product(t);
                }
                kr2type(skolem, t);
            }// Exception here is not fatal
             catch (Throwable ex) {
            }
        }

        @Override
        public void solvingCNF(int primaryVars, int vars, int clauses) {
            if (solved[0])
                return;
            else
                // initially solved[0] is true, so we
                solved[0] = true;
            // won't report the # of vars/clauses
            if (rep != null)
                rep.solve(primaryVars, vars, clauses);
        }
    });
    if (!opt.solver.equals(SatSolver.CNF) && !opt.solver.equals(SatSolver.KK) && tryBookExamples) {
        // try
        // book
        // examples
        A4Reporter r = AlloyCore.isDebug() ? rep : null;
        try {
            sol = BookExamples.trial(r, this, fgoal, solver, cmd.check);
        } catch (Throwable ex) {
            sol = null;
        }
    }
    // this allows the reporter to report the # of
    solved[0] = false;
    // vars/clauses
    for (Relation r : bounds.relations()) {
        formulas.add(r.eq(r));
    }
    // Without this, kodkod refuses to grow unmentioned relations
    fgoal = Formula.and(formulas);
    // Now pick the solver and solve it!
    if (opt.solver.equals(SatSolver.KK)) {
        File tmpCNF = File.createTempFile("tmp", ".java", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        Util.writeAll(out, debugExtractKInput());
        rep.resultCNF(out);
        return null;
    }
    if (opt.solver.equals(SatSolver.CNF)) {
        File tmpCNF = File.createTempFile("tmp", ".cnf", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        solver.options().setSolver(WriteCNF.factory(out));
        try {
            sol = solver.solve(fgoal, bounds);
        } catch (WriteCNF.WriteCNFCompleted ex) {
            rep.resultCNF(out);
            return null;
        }
        // The formula is trivial (otherwise, it would have thrown an
        // exception)
        // Since the user wants it in CNF format, we manually generate a
        // trivially satisfiable (or unsatisfiable) CNF file.
        Util.writeAll(out, sol.instance() != null ? "p cnf 1 1\n1 0\n" : "p cnf 1 2\n1 0\n-1 0\n");
        rep.resultCNF(out);
        return null;
    }
    if (!solver.options().solver().incremental()) /*
                                                      * || solver.options().solver()==SATFactory. ZChaffMincost
                                                      */
    {
        if (sol == null)
            sol = solver.solve(fgoal, bounds);
    } else {
        kEnumerator = new Peeker<Solution>(solver.solveAll(fgoal, bounds));
        if (sol == null)
            sol = kEnumerator.next();
    }
    if (!solved[0])
        rep.solve(0, 0, 0);
    final Instance inst = sol.instance();
    // To ensure no more output during SolutionEnumeration
    solver.options().setReporter(oldReporter);
    // If unsatisfiable, then retreive the unsat core if desired
    if (inst == null && solver.options().solver() == SATFactory.MiniSatProver) {
        try {
            lCore = new LinkedHashSet<Node>();
            Proof p = sol.proof();
            if (sol.outcome() == UNSATISFIABLE) {
                // only perform the minimization if it was UNSATISFIABLE,
                // rather than TRIVIALLY_UNSATISFIABLE
                int i = p.highLevelCore().size();
                rep.minimizing(cmd, i);
                if (opt.coreMinimization == 0)
                    try {
                        p.minimize(new RCEStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                if (opt.coreMinimization == 1)
                    try {
                        p.minimize(new HybridStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                rep.minimized(cmd, i, p.highLevelCore().size());
            }
            for (Iterator<TranslationRecord> it = p.core(); it.hasNext(); ) {
                Object n = it.next().node();
                if (n instanceof Formula)
                    lCore.add((Formula) n);
            }
            Map<Formula, Node> map = p.highLevelCore();
            hCore = new LinkedHashSet<Node>(map.keySet());
            hCore.addAll(map.values());
        } catch (Throwable ex) {
            lCore = hCore = null;
        }
    }
    // If satisfiable, then add/rename the atoms and skolems
    if (inst != null) {
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
    }
    // report the result
    solved();
    time = System.currentTimeMillis() - time;
    if (inst != null)
        rep.resultSAT(cmd, time, this);
    else
        rep.resultUNSAT(cmd, time, this);
    return this;
}
Also used : HybridStrategy(kodkod.engine.ucore.HybridStrategy) Instance(kodkod.instance.Instance) Node(kodkod.ast.Node) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) RCEStrategy(kodkod.engine.ucore.RCEStrategy) Solution(kodkod.engine.Solution) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) AbstractReporter(kodkod.engine.config.AbstractReporter) Reporter(kodkod.engine.config.Reporter) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) Decl(kodkod.ast.Decl) Evaluator(kodkod.engine.Evaluator) UniqueNameGenerator(edu.mit.csail.sdg.alloy4.UniqueNameGenerator) Type(edu.mit.csail.sdg.ast.Type) Proof(kodkod.engine.Proof) File(java.io.File) Tuple(kodkod.instance.Tuple)

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