use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class SimInstance method init.
/**
* Initializes the given field to be associated with the given unary value;
* should only be called at the beginning.
* <p>
* The resulting instance may or may not satisfy all facts, and should be
* checked for consistency.
*/
public void init(Field field, SimTupleset value) throws Err {
if (value == null) {
sfs.remove(field);
return;
}
if (!value.empty() && value.arity() != field.type().arity())
throw new ErrorType("Evaluator encountered an error: field " + field.label + " arity must not be " + value.arity());
if (field.defined)
throw new ErrorAPI("Evaluator cannot prebind the value of a defined field.");
sfs.put(field, value);
cacheUNIV = null;
cacheSTRING = null;
cacheForConstants.clear();
}
use of edu.mit.csail.sdg.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class A4Solution method eval.
/**
* Return the A4TupleSet for the given field (if solution not yet solved, or
* unsatisfiable, or field not found, then return an empty tupleset)
*/
public A4TupleSet eval(Field field) {
try {
if (!solved || eval == null)
return new A4TupleSet(factory.noneOf(field.type().arity()), this);
A4TupleSet ans = evalCache.get(field);
if (ans != null)
return ans;
TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, field));
ans = new A4TupleSet(ts, this);
evalCache.put(field, ans);
return ans;
} catch (Err er) {
return new A4TupleSet(factory.noneOf(field.type().arity()), this);
}
}
use of edu.mit.csail.sdg.alloy4.Err 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.alloy4.Err in project org.alloytools.alloy by AlloyTools.
the class A4Solution method eval.
/**
* Return the A4TupleSet for the given sig (if solution not yet solved, or
* unsatisfiable, or sig not found, then return an empty tupleset)
*/
public A4TupleSet eval(Sig sig) {
try {
if (!solved || eval == null)
return new A4TupleSet(factory.noneOf(1), this);
A4TupleSet ans = evalCache.get(sig);
if (ans != null)
return ans;
TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, sig));
ans = new A4TupleSet(ts, this);
evalCache.put(sig, ans);
return ans;
} catch (Err er) {
return new A4TupleSet(factory.noneOf(1), this);
}
}
use of edu.mit.csail.sdg.alloy4.Err 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;
}
Aggregations