Search in sources :

Example 11 with TupleSet

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

the class BoundsComputer method computeLowerBound.

// ==============================================================================================================//
/**
 * Computes the lowerbound from bottom-up; it will also set a suitable initial
 * value for each sig's upperbound. Precondition: sig is not a builtin sig
 */
private TupleSet computeLowerBound(List<Tuple> atoms, final PrimSig sig) throws Err {
    int n = sc.sig2scope(sig);
    TupleSet lower = factory.noneOf(1);
    for (PrimSig c : sig.children()) lower.addAll(computeLowerBound(atoms, c));
    TupleSet upper = lower.clone();
    boolean isExact = sc.isExact(sig);
    if (isExact || sig.isTopLevel())
        for (n = n - upper.size(); n > 0; n--) {
            Tuple atom = atoms.remove(atoms.size() - 1);
            // atoms to the UPPERBOUND.
            if (isExact)
                lower.add(atom);
            upper.add(atom);
        }
    lb.put(sig, lower);
    ub.put(sig, upper);
    return lower;
}
Also used : TupleSet(kodkod.instance.TupleSet) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Tuple(kodkod.instance.Tuple)

Example 12 with TupleSet

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

the class BoundsComputer method allocateSubsetSig.

// ==============================================================================================================//
/**
 * Allocate relations for SubsetSig top-down.
 */
private Expression allocateSubsetSig(SubsetSig sig) throws Err {
    // We must not visit the same SubsetSig more than once, so if we've been
    // here already, then return the old value right away
    Expression sum = sol.a2k(sig);
    if (sum != null && sum != Expression.NONE)
        return sum;
    // Recursively form the union of all parent expressions
    TupleSet ts = factory.noneOf(1);
    for (Sig parent : sig.parents) {
        Expression p = (parent instanceof PrimSig) ? sol.a2k(parent) : allocateSubsetSig((SubsetSig) parent);
        ts.addAll(sol.query(true, p, false));
        if (sum == null)
            sum = p;
        else
            sum = sum.union(p);
    }
    // If subset is exact, then just use the "sum" as is
    if (sig.exact) {
        sol.addSig(sig, sum);
        return sum;
    }
    // Allocate a relation for this subset sig, then bound it
    rep.bound("Sig " + sig + " in " + ts + "\n");
    Relation r = sol.addRel(sig.label, null, ts);
    sol.addSig(sig, r);
    // Add a constraint that it is INDEED a subset of the union of its
    // parents
    sol.addFormula(r.in(sum), sig.isSubset);
    return r;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) TupleSet(kodkod.instance.TupleSet) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Relation(kodkod.ast.Relation) Expression(kodkod.ast.Expression) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 13 with TupleSet

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

the class A4SolutionReader method parseSkolem.

/**
 * Parse skolem.
 */
private ExprVar parseSkolem(String id) throws IOException, Err {
    final XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown ID " + id + " encountered.");
    if (!node.is("skolem"))
        throw new IOException("ID " + id + " is not a skolem.");
    String label = label(node);
    Expr type = null;
    for (XMLNode sub : node) if (sub.is("types")) {
        Expr t = parseType(sub);
        if (type == null)
            type = t;
        else
            type = type.plus(t);
    }
    int arity;
    if (type == null || (arity = type.type().arity()) < 1)
        throw new IOException("Skolem " + label + " is maltyped.");
    ExprVar var = ExprVar.make(Pos.UNKNOWN, label, type.type());
    TupleSet ts = parseTuples(node, arity);
    expr2ts.put(var, ts);
    return var;
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) TupleSet(kodkod.instance.TupleSet) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) IOException(java.io.IOException)

Example 14 with TupleSet

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

the class A4SolutionReader method parseField.

/**
 * Parse field.
 */
private Field parseField(String id) throws IOException, Err {
    final XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown FieldID " + id + " encountered.");
    if (!node.is("field"))
        throw new IOException("ID " + id + " is not a field.");
    String label = label(node);
    Pos isPrivate = yes(node, "private") ? Pos.UNKNOWN : null;
    Pos isMeta = yes(node, "meta") ? Pos.UNKNOWN : null;
    Expr type = null;
    for (XMLNode sub : node) if (sub.is("types")) {
        Expr t = parseType(sub);
        if (type == null)
            type = t;
        else
            type = type.plus(t);
    }
    int arity;
    if (type == null || (arity = type.type().arity()) < 2)
        throw new IOException("Field " + label + " is maltyped.");
    String parentID = node.getAttribute("parentID");
    Sig parent = id2sig.get(parentID);
    if (parent == null)
        throw new IOException("ID " + parentID + " is not a sig.");
    Field field = null;
    for (Field f : parent.getFields()) if (f.label.equals(label) && f.type().arity() == arity && choices.contains(f)) {
        field = f;
        choices.remove(f);
        break;
    }
    if (field == null)
        field = parent.addTrickyField(Pos.UNKNOWN, isPrivate, null, null, isMeta, new String[] { label }, UNIV.join(type))[0];
    TupleSet ts = parseTuples(node, arity);
    expr2ts.put(field, ts);
    return field;
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) TupleSet(kodkod.instance.TupleSet) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) Pos(edu.mit.csail.sdg.alloy4.Pos) IOException(java.io.IOException)

Example 15 with TupleSet

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

the class TableView method toTable.

/**
 * Format a solution to a string
 *
 * @param solution
 * @param instance
 * @param sigs
 * @return
 */
public static Map<String, Table> toTable(A4Solution solution, Instance instance, SafeList<Sig> sigs) {
    Map<String, Table> map = new HashMap<String, Table>();
    for (Sig s : sigs) {
        if (!s.label.startsWith("this/"))
            continue;
        TupleSet instanceTuples = instance.tuples(s.label);
        if (instanceTuples != null) {
            SimTupleset sigInstances = toSimTupleset(instanceTuples);
            Table table = new Table(sigInstances.size() + 1, s.getFields().size() + 1, 1);
            table.set(0, 0, s.label);
            if (s.getFields().size() == 0 && sigInstances.size() <= 1)
                continue;
            int c = 1;
            for (Field f : s.getFields()) {
                table.set(0, c++, f.label);
            }
            map.put(s.label, table);
            int r = 1;
            for (SimTuple sigInstance : sigInstances) {
                assert sigInstance.arity() == 1;
                SimTupleset leftJoin = SimTupleset.make(sigInstance);
                table.set(r, 0, sigInstance.get(0));
                c = 1;
                for (Field f : s.getFields()) {
                    SimTupleset relations = toSimTupleset(solution.eval(f));
                    SimTupleset joined = leftJoin.join(relations);
                    Table relationTable = toTable(joined);
                    table.set(r, c++, relationTable);
                }
                r++;
            }
        }
    }
    return map;
}
Also used : Sig(edu.mit.csail.sdg.ast.Sig) A4TupleSet(edu.mit.csail.sdg.translator.A4TupleSet) TupleSet(kodkod.instance.TupleSet) Field(edu.mit.csail.sdg.ast.Sig.Field) Table(org.alloytools.util.table.Table) HashMap(java.util.HashMap) SimTupleset(edu.mit.csail.sdg.sim.SimTupleset) SimTuple(edu.mit.csail.sdg.sim.SimTuple)

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