use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.
the class SimInstance method visit.
/**
* {@inheritDoc}
*/
@Override
public SimTupleset visit(Field x) throws Err {
if (x.defined) {
final ExprVar v = (ExprVar) (x.sig.decl.get());
final Expr b = x.decl().expr;
final Env<ExprVar, Object> oldenv = env;
env = new Env<ExprVar, Object>();
if (!b.hasVar(v)) {
SimTupleset ans = cset(x.sig).product(cset(b));
env = oldenv;
return ans;
}
SimTupleset ans = SimTupleset.EMPTY;
for (SimTuple a : visit(x.sig)) {
SimTupleset left = SimTupleset.make(a);
env.put(v, left);
SimTupleset right = cset(b);
env.remove(v);
ans = left.product(right).union(ans);
}
env = oldenv;
return ans;
}
Object ans = sfs.get(x);
if (ans instanceof SimTupleset)
return (SimTupleset) ans;
else
throw new ErrorFatal("Unknown field " + x + " encountered during evaluation.");
}
use of edu.mit.csail.sdg.ast.ExprVar 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.ast.ExprVar 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 + ">");
}
}
use of edu.mit.csail.sdg.ast.ExprVar 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;
}
use of edu.mit.csail.sdg.ast.ExprVar in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method visit.
/**
* {@inheritDoc}
*/
@Override
public Object visit(ExprCall x) throws Err {
final Func f = x.fun;
final Object candidate = f.count() == 0 ? cacheForConstants.get(f) : null;
if (candidate != null)
return candidate;
final Expr body = f.getBody();
if (body.type().arity() < 0 || body.type().arity() != f.returnDecl.type().arity())
throw new ErrorType(body.span(), "Function return value not fully resolved.");
final int n = f.count();
int maxRecursion = unrolls;
for (Func ff : current_function) if (ff == f) {
if (maxRecursion < 0) {
throw new ErrorSyntax(x.span(), "" + f + " cannot call itself recursively!");
}
if (maxRecursion == 0) {
Type t = f.returnDecl.type();
if (t.is_bool)
return Formula.FALSE;
if (t.is_int())
return IntConstant.constant(0);
int i = t.arity();
Expression ans = Expression.NONE;
while (i > 1) {
ans = ans.product(Expression.NONE);
i--;
}
return ans;
}
maxRecursion--;
}
Env<ExprVar, Object> newenv = new Env<ExprVar, Object>();
for (int i = 0; i < n; i++) newenv.put(f.get(i), cset(x.args.get(i)));
Env<ExprVar, Object> oldenv = env;
env = newenv;
current_function.add(f);
Object ans = visitThis(body);
env = oldenv;
current_function.remove(current_function.size() - 1);
if (ans instanceof Formula)
k2pos((Formula) ans, x);
if (f.count() == 0)
cacheForConstants.put(f, ans);
return ans;
}
Aggregations