use of edu.mit.csail.sdg.ast.Sig.Field 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.Field 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.Sig.Field 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.Sig.Field 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;
}
use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.
the class CompModule method rejectNameClash.
// ============================================================================================================================//
private static void rejectNameClash(final List<CompModule> modules) throws Err {
// The Alloy language forbids two overlapping sigs from having fields
// with the same name.
// In other words: if 2 fields have the same name, then their type's
// first column must not intersect.
final Map<String, List<Field>> fieldname2fields = new LinkedHashMap<String, List<Field>>();
for (CompModule m : modules) {
for (Sig sig : m.sigs.values()) {
for (Field field : sig.getFields()) {
List<Field> peers = fieldname2fields.get(field.label);
if (peers == null) {
peers = new ArrayList<Field>();
fieldname2fields.put(field.label, peers);
}
for (Field field2 : peers) if (field.type().firstColumnOverlaps(field2.type()))
throw new ErrorType(field.pos, "Two overlapping signatures cannot have\n" + "two fields with the same name \"" + field.label + "\":\n\n1) one is in sig \"" + field.sig + "\"\n" + field.pos + "\n\n2) the other is in sig \"" + field2.sig + "\"\n" + field2.pos);
peers.add(field);
}
}
}
}
Aggregations