Search in sources :

Example 11 with Field

use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.

the class SimInstance method write.

/**
 * Write the bitwidth, maxseq, set of all atoms, and map of all sig/field/var
 * into the given file.
 */
private void write(BufferedOutputStream out) throws IOException {
    write(out, "maxseq = " + maxseq + ("\n" + "bitwidth = ") + bitwidth + "\n");
    for (Map.Entry<Expr, SimTupleset> entry : sfs.entrySet()) {
        Expr e = entry.getKey();
        if (e instanceof Sig)
            write(out, "sig " + ((Sig) e).label + " = ");
        else if (e instanceof Field)
            write(out, "field " + ((Field) e).sig.label + " " + ((Field) e).label + " = ");
        else if (e instanceof ExprVar)
            write(out, "var " + ((ExprVar) e).label + " = ");
        else
            continue;
        entry.getValue().write(out);
        out.write('\n');
    }
}
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) ExprVar(edu.mit.csail.sdg.ast.ExprVar) Field(edu.mit.csail.sdg.ast.Sig.Field) Expr(edu.mit.csail.sdg.ast.Expr) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 12 with Field

use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.

the class SimInstance method visit.

/**
 * {@inheritDoc}
 */
@Override
public SimTupleset visit(Sig x) throws Err {
    if (x.isSame(Sig.NONE))
        return SimTupleset.EMPTY;
    if (x.isSame(Sig.SEQIDX))
        return SimTupleset.make(0, maxseq - 1);
    if (x.isSame(Sig.SIGINT))
        return SimTupleset.make(min, max);
    if (x.isSame(Sig.STRING)) {
        if (cacheSTRING == null) {
            cacheSTRING = SimTupleset.EMPTY;
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof Field || e.getKey() instanceof ExprVar) {
                for (SimTuple t : e.getValue()) for (int i = t.arity() - 1; i >= 0; i--) {
                    String a = t.get(i).toString();
                    if (a.length() > 0 && a.charAt(0) == '"')
                        cacheSTRING = cacheSTRING.union(SimTuple.make(t.get(i)));
                }
            }
        }
        return cacheSTRING;
    }
    if (x == Sig.UNIV) {
        if (cacheUNIV == null) {
            cacheUNIV = SimTupleset.make(min, max);
            for (Map.Entry<Expr, SimTupleset> e : sfs.entrySet()) if (e.getKey() instanceof PrimSig && ((PrimSig) (e.getKey())).isTopLevel())
                cacheUNIV = cacheUNIV.union(e.getValue());
            cacheUNIV = cacheUNIV.union(visit(Sig.STRING));
        }
        return cacheUNIV;
    }
    Object ans = sfs.get(x);
    if (ans instanceof SimTupleset)
        return (SimTupleset) ans;
    else
        throw new ErrorFatal("Unknown sig " + x + " encountered during evaluation.");
}
Also used : ExprVar(edu.mit.csail.sdg.ast.ExprVar) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 13 with Field

use of edu.mit.csail.sdg.ast.Sig.Field 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)

Example 14 with Field

use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.

the class A4SolutionWriter method writesig.

/**
 * Write the given Sig.
 */
private A4TupleSet writesig(final Sig x) throws Err {
    A4TupleSet ts = null, ts2 = null;
    if (x == Sig.NONE)
        // should not happen, but we test for it anyway
        return null;
    if (sol == null && x.isMeta != null)
        // When writing the metamodel, skip the metamodel sigs!
        return null;
    if (x instanceof PrimSig)
        for (final PrimSig sub : children((PrimSig) x)) {
            A4TupleSet ts3 = writesig(sub);
            if (ts2 == null)
                ts2 = ts3;
            else
                ts2 = ts2.plus(ts3);
        }
    if (rep != null)
        rep.write(x);
    Util.encodeXMLs(out, "\n<sig label=\"", x.label, "\" ID=\"", map(x));
    if (x instanceof PrimSig && x != Sig.UNIV)
        Util.encodeXMLs(out, "\" parentID=\"", map(((PrimSig) x).parent));
    if (x.builtin)
        out.print("\" builtin=\"yes");
    if (x.isAbstract != null)
        out.print("\" abstract=\"yes");
    if (x.isOne != null)
        out.print("\" one=\"yes");
    if (x.isLone != null)
        out.print("\" lone=\"yes");
    if (x.isSome != null)
        out.print("\" some=\"yes");
    if (x.isPrivate != null)
        out.print("\" private=\"yes");
    if (x.isMeta != null)
        out.print("\" meta=\"yes");
    if (x instanceof SubsetSig && ((SubsetSig) x).exact)
        out.print("\" exact=\"yes");
    if (x.isEnum != null)
        out.print("\" enum=\"yes");
    out.print("\">\n");
    try {
        if (sol != null && x != Sig.UNIV && x != Sig.SIGINT && x != Sig.SEQIDX) {
            ts = (sol.eval(x));
            for (A4Tuple t : ts.minus(ts2)) Util.encodeXMLs(out, "   <atom label=\"", t.atom(0), "\"/>\n");
        }
    } catch (Throwable ex) {
        throw new ErrorFatal("Error evaluating sig " + x.label, ex);
    }
    if (x instanceof SubsetSig)
        for (Sig p : ((SubsetSig) x).parents) Util.encodeXMLs(out, "   <type ID=\"", map(p), "\"/>\n");
    out.print("</sig>\n");
    for (Field field : x.getFields()) writeField(field);
    return ts;
}
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) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 15 with Field

use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.

the class CompModule method rejectNameClash.

// ============================================================================================================================//
private static void rejectNameClash(final List<CompModule> modules) throws Err {
    // The Alloy language forbids two overlapping sigs from having fields
    // with the same name.
    // In other words: if 2 fields have the same name, then their type's
    // first column must not intersect.
    final Map<String, List<Field>> fieldname2fields = new LinkedHashMap<String, List<Field>>();
    for (CompModule m : modules) {
        for (Sig sig : m.sigs.values()) {
            for (Field field : sig.getFields()) {
                List<Field> peers = fieldname2fields.get(field.label);
                if (peers == null) {
                    peers = new ArrayList<Field>();
                    fieldname2fields.put(field.label, peers);
                }
                for (Field field2 : peers) if (field.type().firstColumnOverlaps(field2.type()))
                    throw new ErrorType(field.pos, "Two overlapping signatures cannot have\n" + "two fields with the same name \"" + field.label + "\":\n\n1) one is in sig \"" + field.sig + "\"\n" + field.pos + "\n\n2) the other is in sig \"" + field2.sig + "\"\n" + field2.pos);
                peers.add(field);
            }
        }
    }
}
Also used : PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig) Sig(edu.mit.csail.sdg.ast.Sig) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Field(edu.mit.csail.sdg.ast.Sig.Field) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Util.asList(edu.mit.csail.sdg.alloy4.Util.asList) SafeList(edu.mit.csail.sdg.alloy4.SafeList) TempList(edu.mit.csail.sdg.alloy4.ConstList.TempList) List(java.util.List) ExprList(edu.mit.csail.sdg.ast.ExprList) ArrayList(java.util.ArrayList) JoinableList(edu.mit.csail.sdg.alloy4.JoinableList) ConstList(edu.mit.csail.sdg.alloy4.ConstList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Field (edu.mit.csail.sdg.ast.Sig.Field)19 Sig (edu.mit.csail.sdg.ast.Sig)17 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)12 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)9 Expr (edu.mit.csail.sdg.ast.Expr)8 ExprVar (edu.mit.csail.sdg.ast.ExprVar)7 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 TupleSet (kodkod.instance.TupleSet)5 Err (edu.mit.csail.sdg.alloy4.Err)4 Map (java.util.Map)4 ConstList (edu.mit.csail.sdg.alloy4.ConstList)3 SafeList (edu.mit.csail.sdg.alloy4.SafeList)3 Decl (edu.mit.csail.sdg.ast.Decl)3 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)3 ExprUnary (edu.mit.csail.sdg.ast.ExprUnary)3 IdentityHashMap (java.util.IdentityHashMap)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)2 Pair (edu.mit.csail.sdg.alloy4.Pair)2 Pos (edu.mit.csail.sdg.alloy4.Pos)2