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.");
}
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);
}
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));
}
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;
}
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;
}
Aggregations