use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class CompModule method resolveParams.
/**
* Every param in every module will now point to a nonnull Sig.
*/
private static void resolveParams(A4Reporter rep, List<CompModule> modules) throws Err {
while (true) {
boolean chg = false;
Open missing = null;
String missingName = "";
for (CompModule mod : modules) for (Open open : mod.opens.values()) {
CompModule sub = open.realModule;
if (open.args.size() != sub.params.size())
throw new ErrorSyntax(open.pos, "You supplied " + open.args.size() + " arguments to the open statement, but the imported module requires " + sub.params.size() + " arguments.");
int i = 0;
for (Map.Entry<String, Sig> p : sub.params.entrySet()) {
Sig old = p.getValue();
String kn = p.getKey(), vn = open.args.get(i);
i++;
Sig vv = mod.getRawSIG(open.pos, vn);
if (vv == null) {
if (old == null) {
missing = open;
missingName = vn;
}
continue;
}
if (old == vv)
continue;
if (old != null)
throw new ErrorFatal(open.pos, "Internal error (module re-instantiated with different arguments)");
if (vv == NONE)
throw new ErrorSyntax(open.pos, "You cannot use \"none\" as an instantiating argument.");
chg = true;
p.setValue(vv);
rep.parse("RESOLVE: " + (sub.path.length() == 0 ? "this/" : sub.path) + "/" + kn + " := " + vv + "\n");
}
}
if (!chg && missing == null)
return;
if (!chg)
throw new ErrorSyntax(missing.pos, "The signature name \"" + missingName + "\" cannot be found.");
}
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class CompModule method unique.
/**
* Throw an exception if there are more than 1 match; return nonnull if only one
* match; return null if no match.
*/
private Object unique(Pos pos, String name, List<Object> objs) throws Err {
if (objs.size() == 0)
return null;
if (objs.size() == 1)
return objs.get(0);
StringBuilder msg = new StringBuilder("The name \"").append(name);
msg.append("\" is ambiguous.\n" + "There are ").append(objs.size()).append(" choices:");
for (int i = 0; i < objs.size(); i++) {
msg.append("\n\n#").append(i + 1).append(": ");
Object x = objs.get(i);
if (x instanceof Sig) {
Sig y = (Sig) x;
msg.append("sig ").append(y.label).append("\n" + "at ").append(y.pos.toShortString());
} else if (x instanceof Func) {
Func y = (Func) x;
msg.append(y.isPred ? "pred " : "fun ").append(y.label).append("\n" + "at ").append(y.pos.toShortString());
} else if (x instanceof Expr) {
Expr y = (Expr) x;
msg.append("assertion at ").append(y.pos.toShortString());
}
}
throw new ErrorSyntax(pos, msg.toString());
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class SimInstance method read.
/**
* Construct a new simulation context by reading the given file.
*/
public static synchronized SimInstance read(Module root, String filename, List<ExprVar> vars) throws Err, IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(filename);
bis = new BufferedInputStream(fis);
// read maxseq
if (!readkey(bis).equals("maxseq"))
throw new IOException("Expecting maxseq = ...");
int maxseq = readNonNegativeIntThenLinebreak(bis);
// read bitwidth
if (!readkey(bis).equals("bitwidth"))
throw new IOException("Expecting bitwidth = ...");
int bitwidth = readNonNegativeIntThenLinebreak(bis);
// construct the SimInstance object with no atoms and no relations
SimInstance ans = new SimInstance(root, bitwidth, maxseq);
// parse all the relations
Map<String, SimTupleset> sfs = new HashMap<String, SimTupleset>();
while (true) {
String key = readkey(bis);
if (key.length() == 0)
// we don't expect any more data after this
break;
sfs.put(key, SimTupleset.read(bis));
}
// assign its value in the new SimInstance's sfs map
for (final Sig s : root.getAllReachableSigs()) if (!s.builtin) {
SimTupleset ts = sfs.get("sig " + s.label);
if (ts != null)
ans.sfs.put(s, ts);
for (final Field f : s.getFields()) if (!f.defined) {
ts = sfs.get("field " + s.label + " " + f.label);
if (ts != null)
ans.sfs.put(f, ts);
}
}
// assign its value in the new SimInstance's sfs map
if (vars != null)
for (ExprVar v : vars) {
SimTupleset ts = sfs.get("var " + v.label);
if (ts != null)
ans.sfs.put(v, ts);
}
// close the files then return the answer
bis.close();
bis = null;
fis.close();
fis = null;
return ans;
} finally {
// free the temporary array
readcache = null;
// if an exception occurred, we'll try to close to files anyway,
// since open file descriptors is a scarce resource
Util.close(bis);
Util.close(fis);
}
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class SimInstance method write.
/**
* Write the bitwidth, maxseq, set of all atoms, and map of all sig/field/var
* into the given file.
*/
private void write(BufferedOutputStream out) throws IOException {
write(out, "maxseq = " + maxseq + ("\n" + "bitwidth = ") + bitwidth + "\n");
for (Map.Entry<Expr, SimTupleset> entry : sfs.entrySet()) {
Expr e = entry.getKey();
if (e instanceof Sig)
write(out, "sig " + ((Sig) e).label + " = ");
else if (e instanceof Field)
write(out, "field " + ((Field) e).sig.label + " " + ((Field) e).label + " = ");
else if (e instanceof ExprVar)
write(out, "var " + ((ExprVar) e).label + " = ");
else
continue;
entry.getValue().write(out);
out.write('\n');
}
}
use of edu.mit.csail.sdg.ast.Sig 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 + ">");
}
}
Aggregations