Search in sources :

Example 6 with SubsetSig

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

the class CompModule method addSig.

Sig addSig(String name, ExprVar par, List<ExprVar> parents, List<Decl> fields, Expr fact, Attr... attributes) throws Err {
    Sig obj;
    Pos pos = Pos.UNKNOWN.merge(WHERE.find(attributes));
    status = 3;
    dup(pos, name, true);
    String full = (path.length() == 0) ? "this/" + name : path + "/" + name;
    Pos subset = null, subsig = null;
    boolean exact = false;
    if (par != null) {
        if (par.label.equals("extends")) {
            subsig = par.span().merge(parents.get(0).span());
        } else {
            exact = !par.label.equals("in");
            subset = par.span();
            for (ExprVar p : parents) subset = p.span().merge(subset);
        }
    }
    attributes = Util.append(attributes, exact ? Attr.EXACT : null);
    if (subset != null) {
        attributes = Util.append(attributes, SUBSET.makenull(subset));
        List<Sig> newParents = new ArrayList<Sig>(parents == null ? 0 : parents.size());
        if (parents != null)
            for (ExprVar p : parents) newParents.add(new PrimSig(p.label, WHERE.make(p.pos)));
        obj = new SubsetSig(full, newParents, attributes);
    } else {
        attributes = Util.append(attributes, SUBSIG.makenull(subsig));
        PrimSig newParent = (parents != null && parents.size() > 0) ? (new PrimSig(parents.get(0).label, WHERE.make(parents.get(0).pos))) : UNIV;
        obj = new PrimSig(full, newParent, attributes);
    }
    sigs.put(name, obj);
    old2fields.put(obj, fields);
    old2appendedfacts.put(obj, fact);
    return obj;
}
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) ExprVar(edu.mit.csail.sdg.ast.ExprVar) SubsetSig(edu.mit.csail.sdg.ast.Sig.SubsetSig) Pos(edu.mit.csail.sdg.alloy4.Pos) ArrayList(java.util.ArrayList) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 7 with SubsetSig

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

the class A4SolutionReader method parseSig.

/**
 * Parse sig/set.
 */
private Sig parseSig(String id, int depth) throws IOException, Err {
    Sig ans = id2sig.get(id);
    if (ans != null)
        return ans;
    XMLNode node = nmap.get(id);
    if (node == null)
        throw new IOException("Unknown SigID " + id + " encountered.");
    if (!node.is("sig"))
        throw new IOException("ID " + id + " is not a sig.");
    String label = label(node);
    Attr isAbstract = yes(node, "abstract") ? Attr.ABSTRACT : null;
    Attr isOne = yes(node, "one") ? Attr.ONE : null;
    Attr isLone = yes(node, "lone") ? Attr.LONE : null;
    Attr isSome = yes(node, "some") ? Attr.SOME : null;
    Attr isPrivate = yes(node, "private") ? Attr.PRIVATE : null;
    Attr isMeta = yes(node, "meta") ? Attr.META : null;
    Attr isEnum = yes(node, "enum") ? Attr.ENUM : null;
    Attr isExact = yes(node, "exact") ? Attr.EXACT : null;
    if (yes(node, "builtin")) {
        if (label.equals(UNIV.label)) {
            id2sig.put(id, UNIV);
            return UNIV;
        }
        if (label.equals(SIGINT.label)) {
            id2sig.put(id, SIGINT);
            return SIGINT;
        }
        if (label.equals(SEQIDX.label)) {
            id2sig.put(id, SEQIDX);
            return SEQIDX;
        }
        if (label.equals(STRING.label)) {
            id2sig.put(id, STRING);
            return STRING;
        }
        throw new IOException("Unknown builtin sig: " + label + " (id=" + id + ")");
    }
    if (depth > nmap.size())
        throw new IOException("Sig " + label + " (id=" + id + ") is in a cyclic inheritance relationship.");
    List<Sig> parents = null;
    TupleSet ts = factory.noneOf(1);
    for (XMLNode sub : node) {
        if (sub.is("atom")) {
            ts.add(factory.tuple(sub.getAttribute("label")));
            continue;
        }
        if (!sub.is("type"))
            continue;
        Sig parent = parseSig(sub.getAttribute("ID"), depth + 1);
        if (parents == null)
            parents = new ArrayList<Sig>();
        parents.add(parent);
    }
    if (parents == null) {
        String parentID = node.getAttribute("parentID");
        Sig parent = parseSig(parentID, depth + 1);
        if (!(parent instanceof PrimSig))
            throw new IOException("Parent of sig " + label + " (id=" + id + ") must not be a subset sig.");
        for (Expr choice : choices) if (choice instanceof PrimSig && parent == ((PrimSig) choice).parent && ((Sig) choice).label.equals(label)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new PrimSig(label, (PrimSig) parent, isAbstract, isLone, isOne, isSome, isPrivate, isMeta, isEnum);
            allsigs.add(ans);
        }
    } else {
        for (Expr choice : choices) if (choice instanceof SubsetSig && ((Sig) choice).label.equals(label) && sameset(parents, ((SubsetSig) choice).parents)) {
            ans = (Sig) choice;
            choices.remove(choice);
            break;
        }
        if (ans == null) {
            ans = new SubsetSig(label, parents, isExact, isLone, isOne, isSome, isPrivate, isMeta);
            allsigs.add(ans);
        }
    }
    id2sig.put(id, ans);
    expr2ts.put(ans, ts);
    if (ans instanceof PrimSig) {
        // Add the atoms in this SIG into all parent sigs
        for (PrimSig ans2 = ((PrimSig) ans).parent; ans2 != null && !ans2.builtin; ans2 = ans2.parent) {
            TupleSet ts2 = expr2ts.get(ans2);
            if (ts2 == null)
                ts2 = ts.clone();
            else {
                ts2 = ts2.clone();
                ts2.addAll(ts);
            }
            expr2ts.put(ans2, ts2);
        }
    }
    return ans;
}
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) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) Expr(edu.mit.csail.sdg.ast.Expr) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Attr(edu.mit.csail.sdg.ast.Attr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Aggregations

Sig (edu.mit.csail.sdg.ast.Sig)7 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)7 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)7 ArrayList (java.util.ArrayList)3 Pos (edu.mit.csail.sdg.alloy4.Pos)2 Expr (edu.mit.csail.sdg.ast.Expr)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 TupleSet (kodkod.instance.TupleSet)2 Err (edu.mit.csail.sdg.alloy4.Err)1 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)1 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)1 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)1 Pair (edu.mit.csail.sdg.alloy4.Pair)1 XMLNode (edu.mit.csail.sdg.alloy4.XMLNode)1 Attr (edu.mit.csail.sdg.ast.Attr)1 Decl (edu.mit.csail.sdg.ast.Decl)1 ExprBinary (edu.mit.csail.sdg.ast.ExprBinary)1 ExprHasName (edu.mit.csail.sdg.ast.ExprHasName)1 ExprUnary (edu.mit.csail.sdg.ast.ExprUnary)1 ExprVar (edu.mit.csail.sdg.ast.ExprVar)1