Search in sources :

Example 1 with SATFactory

use of kodkod.engine.satlab.SATFactory in project org.alloytools.alloy by AlloyTools.

the class BookExamples method trial.

/**
 * This tries a particular solution against the formula.
 */
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
    try {
        frame.kr2typeCLEAR();
        Bounds b = null;
        TupleSet ts = null;
        for (int i = 1; i < t.length; i++) {
            Object x = t[i];
            if (x == null)
                return null;
            if (x instanceof String && ((String) x).length() > 0) {
                // This
                // means
                // it's
                // a
                // unary
                // Tuple
                // containing
                // the
                // given
                // atom
                Tuple xx = fac.tuple((String) x);
                if (ts == null)
                    ts = fac.noneOf(1);
                ts.add(xx);
                continue;
            }
            if (x instanceof Tuple) {
                // This means it's a Tuple
                Tuple xx = (Tuple) x;
                if (ts == null)
                    ts = fac.noneOf(xx.arity());
                ts.add(xx);
                continue;
            }
            if (x instanceof String) {
                // The empty string means the sig
                // name follows here
                i++;
                if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
                    return null;
                String sigName = (String) (t[i]);
                i++;
                String fieldName = (String) (t[i]);
                Sig first = hasSig(sigs, sigName);
                if (first == null)
                    return null;
                Expression expr = null;
                if (fieldName.length() == 0) {
                    expr = frame.a2k(first);
                } else {
                    for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
                        expr = frame.a2k(field);
                        while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
                        break;
                    }
                }
                if (!(expr instanceof Relation))
                    return null;
                if (b == null)
                    // We delay the expansive
                    b = frame.getBounds();
                // really find a possible match
                if (ts == null)
                    ts = fac.noneOf(expr.arity());
                if (!ts.containsAll(b.lowerBound((Relation) expr)))
                    // Sanity check
                    return null;
                if (!b.upperBound((Relation) expr).containsAll(ts))
                    // Sanity check
                    return null;
                b.boundExactly((Relation) expr, ts);
                ts = null;
                continue;
            }
        }
        SATFactory sat = solver.options().solver();
        Solution sol;
        try {
            solver.options().setSolver(SATFactory.DefaultSAT4J);
            sol = solver.solve(f, b);
        } finally {
            solver.options().setSolver(sat);
        }
        if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
            return null;
        if (rep != null)
            rep.debug("Comment: " + t[0] + "\n");
        return sol;
    } catch (Throwable ex) {
        return null;
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) Relation(kodkod.ast.Relation) SATFactory(kodkod.engine.satlab.SATFactory) BinaryExpression(kodkod.ast.BinaryExpression) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) Tuple(kodkod.instance.Tuple) Solution(kodkod.engine.Solution)

Aggregations

Sig (edu.mit.csail.sdg.ast.Sig)1 Field (edu.mit.csail.sdg.ast.Sig.Field)1 BinaryExpression (kodkod.ast.BinaryExpression)1 Expression (kodkod.ast.Expression)1 Relation (kodkod.ast.Relation)1 Solution (kodkod.engine.Solution)1 SATFactory (kodkod.engine.satlab.SATFactory)1 Bounds (kodkod.instance.Bounds)1 Tuple (kodkod.instance.Tuple)1 TupleSet (kodkod.instance.TupleSet)1