Search in sources :

Example 41 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class COM008 method bounds.

/**
 * Returns bounds for the given scope.
 *
 * @return bounds for the given scope.
 */
public final Bounds bounds(int n) {
    assert n > 0;
    final List<String> atoms = new ArrayList<String>(n);
    atoms.add("goal");
    for (int i = 0; i < n; i++) atoms.add("a" + i);
    final Universe u = new Universe(atoms);
    final Bounds bound = new Bounds(u);
    final TupleFactory f = u.factory();
    final TupleSet d1 = f.range(f.tuple("a0"), f.tuple("a" + (n - 1)));
    final TupleSet d2 = d1.product(d1);
    bound.bound(rewrite, d2);
    bound.bound(equalish, d2);
    bound.bound(a, d1);
    bound.bound(b, d1);
    bound.bound(c, d1);
    bound.boundExactly(Atom, d1);
    bound.bound(trr, d2);
    bound.bound(goal, f.setOf("goal"));
    return bound;
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe)

Example 42 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class MED001 method bounds.

/**
 * Returns bounds for the given scope.
 *
 * @return bounds for the given scope.
 */
public final Bounds bounds(int n) {
    assert n > 0;
    final List<String> atoms = new ArrayList<String>(n);
    for (int i = 0; i < n; i++) atoms.add("a" + i);
    final Universe u = new Universe(atoms);
    final Bounds b = new Bounds(u);
    final TupleFactory f = u.factory();
    final TupleSet s = f.allOf(1);
    b.bound(bcapacityne, s);
    b.bound(bcapacityex, s);
    b.bound(bcapacitysn, s);
    b.bound(conditionhyper, s);
    b.bound(conditionhypo, s);
    b.bound(conditionnormo, s);
    b.bound(drugi, s);
    b.bound(uptakelg, s);
    b.bound(uptakepg, s);
    b.bound(releaselg, s);
    b.bound(bsecretioni, s);
    b.bound(drugbg, s);
    b.bound(qilt27, s);
    b.bound(s0, s);
    b.bound(s1, s);
    b.bound(s2, s);
    b.bound(s3, s);
    b.bound(drugsu, s);
    b.bound(n0, s);
    b.bound(gt, f.allOf(2));
    return b;
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) ArrayList(java.util.ArrayList) TupleFactory(kodkod.instance.TupleFactory) Universe(kodkod.instance.Universe)

Example 43 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class A4Solution method query.

/**
 * Query the Bounds object to find the lower/upper bound; throws ErrorFatal if
 * expr is not Relation, nor a {union, product} of Relations.
 */
TupleSet query(boolean findUpper, Expression expr, boolean makeMutable) throws ErrorFatal {
    if (expr == Expression.NONE)
        return factory.noneOf(1);
    if (expr == Expression.INTS)
        return makeMutable ? sigintBounds.clone() : sigintBounds;
    if (expr == KK_SEQIDX)
        return makeMutable ? seqidxBounds.clone() : seqidxBounds;
    if (expr == KK_STRING)
        return makeMutable ? stringBounds.clone() : stringBounds;
    if (expr instanceof Relation) {
        TupleSet ans = findUpper ? bounds.upperBound((Relation) expr) : bounds.lowerBound((Relation) expr);
        if (ans != null)
            return makeMutable ? ans.clone() : ans;
    } else if (expr instanceof BinaryExpression) {
        BinaryExpression b = (BinaryExpression) expr;
        if (b.op() == ExprOperator.UNION) {
            TupleSet left = query(findUpper, b.left(), true);
            TupleSet right = query(findUpper, b.right(), false);
            left.addAll(right);
            return left;
        } else if (b.op() == ExprOperator.PRODUCT) {
            TupleSet left = query(findUpper, b.left(), true);
            TupleSet right = query(findUpper, b.right(), false);
            return left.product(right);
        }
    }
    throw new ErrorFatal("Unknown expression encountered during bounds computation: " + expr);
}
Also used : TupleSet(kodkod.instance.TupleSet) Relation(kodkod.ast.Relation) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) BinaryExpression(kodkod.ast.BinaryExpression)

Example 44 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class A4Solution method eval.

/**
 * Return the A4TupleSet for the given field (if solution not yet solved, or
 * unsatisfiable, or field not found, then return an empty tupleset)
 */
public A4TupleSet eval(Field field) {
    try {
        if (!solved || eval == null)
            return new A4TupleSet(factory.noneOf(field.type().arity()), this);
        A4TupleSet ans = evalCache.get(field);
        if (ans != null)
            return ans;
        TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, field));
        ans = new A4TupleSet(ts, this);
        evalCache.put(field, ans);
        return ans;
    } catch (Err er) {
        return new A4TupleSet(factory.noneOf(field.type().arity()), this);
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Err(edu.mit.csail.sdg.alloy4.Err)

Example 45 with TupleSet

use of kodkod.instance.TupleSet in project org.alloytools.alloy by AlloyTools.

the class A4Solution method toString.

/**
 * Dumps the Kodkod solution into String.
 */
@Override
public String toString() {
    if (!solved)
        return "---OUTCOME---\nUnknown.\n";
    if (eval == null)
        return "---OUTCOME---\nUnsatisfiable.\n";
    String answer = toStringCache;
    if (answer != null)
        return answer;
    Instance sol = eval.instance();
    StringBuilder sb = new StringBuilder();
    sb.append("---INSTANCE---\n" + "integers={");
    boolean firstTuple = true;
    for (IndexedEntry<TupleSet> e : sol.intTuples()) {
        if (firstTuple)
            firstTuple = false;
        else
            sb.append(", ");
        // No need to print e.index() since we've ensured the Int atom's
        // String representation is always equal to ""+e.index()
        Object atom = e.value().iterator().next().atom(0);
        sb.append(atom2name(atom));
    }
    sb.append("}\n");
    try {
        for (Sig s : sigs) {
            sb.append(s.label).append("=").append(eval(s)).append("\n");
            for (Field f : s.getFields()) sb.append(s.label).append("<:").append(f.label).append("=").append(eval(f)).append("\n");
        }
        for (ExprVar v : skolems) {
            sb.append("skolem ").append(v.label).append("=").append(eval(v)).append("\n");
        }
        return toStringCache = sb.toString();
    } catch (Err er) {
        return toStringCache = ("<Evaluator error occurred: " + er + ">");
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) ExprVar(edu.mit.csail.sdg.ast.ExprVar) TupleSet(kodkod.instance.TupleSet) Field(edu.mit.csail.sdg.ast.Sig.Field) Err(edu.mit.csail.sdg.alloy4.Err) Instance(kodkod.instance.Instance)

Aggregations

TupleSet (kodkod.instance.TupleSet)89 TupleFactory (kodkod.instance.TupleFactory)55 Bounds (kodkod.instance.Bounds)53 Universe (kodkod.instance.Universe)50 Relation (kodkod.ast.Relation)43 ArrayList (java.util.ArrayList)30 Formula (kodkod.ast.Formula)29 Solution (kodkod.engine.Solution)23 Solver (kodkod.engine.Solver)21 Expression (kodkod.ast.Expression)20 IntExpression (kodkod.ast.IntExpression)18 Variable (kodkod.ast.Variable)15 Instance (kodkod.instance.Instance)13 Tuple (kodkod.instance.Tuple)12 Decls (kodkod.ast.Decls)10 Sig (edu.mit.csail.sdg.ast.Sig)7 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)7 Evaluator (kodkod.engine.Evaluator)7 Field (edu.mit.csail.sdg.ast.Sig.Field)5 Map (java.util.Map)5