Search in sources :

Example 21 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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 22 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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 23 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class A4Solution method addSig.

/**
 * Add a new sig to this solution and associate it with the given expression
 * (and if s.isTopLevel then add this expression into Sig.UNIV). <br>
 * The expression must contain only constant Relations or Relations that are
 * already bound in this solution. <br>
 * (If the sig was already added by a previous call to addSig(), then this call
 * will return immediately without altering what it is associated with)
 */
void addSig(Sig s, Expression expr) throws ErrorFatal {
    if (solved)
        throw new ErrorFatal("Cannot add an additional sig since solve() has completed.");
    if (expr.arity() != 1)
        throw new ErrorFatal("Sig " + s + " must be associated with a unary relational value.");
    if (a2k.containsKey(s))
        return;
    a2k.put(s, expr);
    sigs.add(s);
    if (s.isTopLevel())
        a2k.put(UNIV, a2k.get(UNIV).union(expr));
}
Also used : ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal)

Example 24 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal in project org.alloytools.alloy by AlloyTools.

the class A4SolutionWriter method writeExpr.

/**
 * Write the given Expr and its Type.
 */
private boolean writeExpr(String prefix, Expr expr) throws Err {
    Type type = expr.type();
    if (!type.hasTuple())
        return false;
    if (sol != null) {
        // Check to see if the tupleset is *really* fully contained inside
        // "type".
        // If not, then grow "type" until the tupleset is fully contained
        // inside "type"
        Expr sum = type.toExpr();
        int lastSize = (-1);
        while (true) {
            A4TupleSet ts = (A4TupleSet) (sol.eval(expr.minus(sum)));
            int n = ts.size();
            if (n <= 0)
                break;
            if (lastSize > 0 && lastSize <= n)
                throw new ErrorFatal("An internal error occurred in the evaluator.");
            lastSize = n;
            Type extra = ts.iterator().next().type();
            type = type.merge(extra);
            sum = sum.plus(extra.toExpr());
        }
        // Now, write out the tupleset
        A4TupleSet ts = (A4TupleSet) (sol.eval(expr));
        for (A4Tuple t : ts) {
            if (prefix.length() > 0) {
                out.print(prefix);
                prefix = "";
            }
            out.print("   <tuple>");
            for (int i = 0; i < t.arity(); i++) Util.encodeXMLs(out, " <atom label=\"", t.atom(i), "\"/>");
            out.print(" </tuple>\n");
        }
    }
    // Now, write out the type
    if (prefix.length() > 0)
        return false;
    for (List<PrimSig> ps : type.fold()) {
        out.print("   <types>");
        for (PrimSig sig : ps) Util.encodeXMLs(out, " <type ID=\"", map(sig), "\"/>");
        out.print(" </types>\n");
    }
    return true;
}
Also used : Type(edu.mit.csail.sdg.ast.Type) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) Expr(edu.mit.csail.sdg.ast.Expr) PrimSig(edu.mit.csail.sdg.ast.Sig.PrimSig)

Example 25 with ErrorFatal

use of edu.mit.csail.sdg.alloy4.ErrorFatal 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)

Aggregations

ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)29 Err (edu.mit.csail.sdg.alloy4.Err)8 Expr (edu.mit.csail.sdg.ast.Expr)6 PrimSig (edu.mit.csail.sdg.ast.Sig.PrimSig)6 Pos (edu.mit.csail.sdg.alloy4.Pos)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 ErrorSyntax (edu.mit.csail.sdg.alloy4.ErrorSyntax)3 ErrorType (edu.mit.csail.sdg.alloy4.ErrorType)3 ExprVar (edu.mit.csail.sdg.ast.ExprVar)3 Sig (edu.mit.csail.sdg.ast.Sig)3 IOException (java.io.IOException)3 LinkedHashSet (java.util.LinkedHashSet)3 BinaryExpression (kodkod.ast.BinaryExpression)3 CapacityExceededException (kodkod.engine.CapacityExceededException)3 HigherOrderDeclException (kodkod.engine.fol2sat.HigherOrderDeclException)3 ErrorAPI (edu.mit.csail.sdg.alloy4.ErrorAPI)2 Field (edu.mit.csail.sdg.ast.Sig.Field)2 SubsetSig (edu.mit.csail.sdg.ast.Sig.SubsetSig)2 A4Tuple (edu.mit.csail.sdg.translator.A4Tuple)2