use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method makeFacts.
/**
* Conjoin the constraints for "field declarations" and "fact" paragraphs
*/
private void makeFacts(Expr facts) throws Err {
rep.debug("Generating facts...\n");
// convert into a form that hopefully gives better unsat core
facts = (new ConvToConjunction()).visitThis(facts);
// add the field facts and appended facts
for (Sig s : frame.getAllReachableSigs()) {
for (Decl d : s.getFieldDecls()) {
k2pos_enabled = false;
for (ExprHasName n : d.names) {
Field f = (Field) n;
Expr form = s.decl.get().join(f).in(d.expr);
form = s.isOne == null ? form.forAll(s.decl) : ExprLet.make(null, (ExprVar) (s.decl.get()), s, form);
frame.addFormula(cform(form), f);
// subset of s.
if (s.isOne == null) {
Expression sr = a2k(s), fr = a2k(f);
for (int i = f.type().arity(); i > 1; i--) fr = fr.join(Expression.UNIV);
frame.addFormula(fr.in(sr), f);
}
}
if (s.isOne == null && d.disjoint2 != null)
for (ExprHasName f : d.names) {
Decl that = s.oneOf("that");
Expr formula = s.decl.get().equal(that.get()).not().implies(s.decl.get().join(f).intersect(that.get().join(f)).no());
frame.addFormula(cform(formula.forAll(that).forAll(s.decl)), d.disjoint2);
}
if (d.names.size() > 1 && d.disjoint != null) {
frame.addFormula(cform(ExprList.makeDISJOINT(d.disjoint, null, d.names)), d.disjoint);
}
}
k2pos_enabled = true;
for (Expr f : s.getFacts()) {
Expr form = s.isOne == null ? f.forAll(s.decl) : ExprLet.make(null, (ExprVar) (s.decl.get()), s, f);
frame.addFormula(cform(form), f);
}
}
k2pos_enabled = true;
recursiveAddFormula(facts);
}
use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseField.
/**
* Parse field.
*/
private Field parseField(String id) throws IOException, Err {
final XMLNode node = nmap.get(id);
if (node == null)
throw new IOException("Unknown FieldID " + id + " encountered.");
if (!node.is("field"))
throw new IOException("ID " + id + " is not a field.");
String label = label(node);
Pos isPrivate = yes(node, "private") ? Pos.UNKNOWN : null;
Pos isMeta = yes(node, "meta") ? Pos.UNKNOWN : null;
Expr type = null;
for (XMLNode sub : node) if (sub.is("types")) {
Expr t = parseType(sub);
if (type == null)
type = t;
else
type = type.plus(t);
}
int arity;
if (type == null || (arity = type.type().arity()) < 2)
throw new IOException("Field " + label + " is maltyped.");
String parentID = node.getAttribute("parentID");
Sig parent = id2sig.get(parentID);
if (parent == null)
throw new IOException("ID " + parentID + " is not a sig.");
Field field = null;
for (Field f : parent.getFields()) if (f.label.equals(label) && f.type().arity() == arity && choices.contains(f)) {
field = f;
choices.remove(f);
break;
}
if (field == null)
field = parent.addTrickyField(Pos.UNKNOWN, isPrivate, null, null, isMeta, new String[] { label }, UNIV.join(type))[0];
TupleSet ts = parseTuples(node, arity);
expr2ts.put(field, ts);
return field;
}
use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.
the class TableView method toTable.
/**
* Format a solution to a string
*
* @param solution
* @param instance
* @param sigs
* @return
*/
public static Map<String, Table> toTable(A4Solution solution, Instance instance, SafeList<Sig> sigs) {
Map<String, Table> map = new HashMap<String, Table>();
for (Sig s : sigs) {
if (!s.label.startsWith("this/"))
continue;
TupleSet instanceTuples = instance.tuples(s.label);
if (instanceTuples != null) {
SimTupleset sigInstances = toSimTupleset(instanceTuples);
Table table = new Table(sigInstances.size() + 1, s.getFields().size() + 1, 1);
table.set(0, 0, s.label);
if (s.getFields().size() == 0 && sigInstances.size() <= 1)
continue;
int c = 1;
for (Field f : s.getFields()) {
table.set(0, c++, f.label);
}
map.put(s.label, table);
int r = 1;
for (SimTuple sigInstance : sigInstances) {
assert sigInstance.arity() == 1;
SimTupleset leftJoin = SimTupleset.make(sigInstance);
table.set(r, 0, sigInstance.get(0));
c = 1;
for (Field f : s.getFields()) {
SimTupleset relations = toSimTupleset(solution.eval(f));
SimTupleset joined = leftJoin.join(relations);
Table relationTable = toTable(joined);
table.set(r, c++, relationTable);
}
r++;
}
}
}
return map;
}
use of edu.mit.csail.sdg.ast.Sig.Field in project org.alloytools.alloy by AlloyTools.
the class CompModule method populate.
/**
* Resolve the name based on the current context and this module.
*/
private Expr populate(TempList<Expr> ch, TempList<String> re, Decl rootfield, Sig rootsig, boolean rootfunparam, Func rootfunbody, Pos pos, String fullname, Expr THIS) {
// Return object can be Func(with > 0 arguments) or Expr
final String name = (fullname.charAt(0) == '@') ? fullname.substring(1) : fullname;
boolean fun = (rootsig != null && (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF)) || (rootsig == null && !rootfunparam);
if (name.equals("univ"))
return ExprUnary.Op.NOOP.make(pos, UNIV);
if (name.equals("Int"))
return ExprUnary.Op.NOOP.make(pos, SIGINT);
if (name.equals("seq/Int"))
return ExprUnary.Op.NOOP.make(pos, SEQIDX);
if (name.equals("String"))
return ExprUnary.Op.NOOP.make(pos, STRING);
if (name.equals("none"))
return ExprUnary.Op.NOOP.make(pos, NONE);
if (name.equals("iden"))
return ExprConstant.Op.IDEN.make(pos, 0);
if (name.equals("sig$") || name.equals("field$"))
if (world != null) {
Sig s = world.sigs.get(name);
if (s != null)
return ExprUnary.Op.NOOP.make(pos, s);
}
final List<Object> ans = name.indexOf('/') >= 0 ? getRawQS(fun ? 5 : 1, name) : getRawNQS(this, fun ? 5 : 1, name);
final Sig param = params.get(name);
if (param != null && !ans.contains(param))
ans.add(param);
for (Object x : ans) {
if (x instanceof Sig) {
Sig y = (Sig) x;
ch.add(ExprUnary.Op.NOOP.make(pos, y, null, 0));
re.add("sig " + y.label);
} else if (x instanceof Func) {
Func f = (Func) x;
int fn = f.count();
int penalty = 0;
if (resolution == 1 && fn > 0 && rootsig != null && THIS != null && THIS.type().hasArity(1) && f.get(0).type().intersects(THIS.type())) {
// If we're inside a sig, and there is a unary variable
// bound to "this",
// we should consider it as a possible FIRST ARGUMENT of a
// fun/pred call
ConstList<Expr> t = Util.asList(THIS);
// penalty
ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 1 + penalty) : ExprBadCall.make(pos, null, f, t, 1 + penalty));
// of
// 1
re.add((f.isPred ? "pred this." : "fun this.") + f.label);
}
if (resolution == 1) {
ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, penalty) : ExprBadCall.make(pos, null, f, null, penalty));
re.add((f.isPred ? "pred " : "fun ") + f.label);
}
if (resolution == 2 && f != rootfunbody && THIS != null && fullname.charAt(0) != '@' && fn > 0 && f.get(0).type().intersects(THIS.type())) {
// If there is some value bound to "this", we should
// consider it as a possible FIRST ARGUMENT of a fun/pred
// call
ConstList<Expr> t = Util.asList(THIS);
ch.add(fn == 1 ? ExprCall.make(pos, null, f, t, 0) : ExprBadCall.make(pos, null, f, t, 0));
re.add((f.isPred ? "pred this." : "fun this.") + f.label);
}
if (resolution != 1) {
ch.add(fn == 0 ? ExprCall.make(pos, null, f, null, 0) : ExprBadCall.make(pos, null, f, null, 0));
re.add((f.isPred ? "pred " : "fun ") + f.label);
}
}
}
// All else: we can call, and can refer to anything visible.
for (CompModule m : getAllNameableModules()) for (Sig s : m.sigs.values()) if (m == this || s.isPrivate == null)
for (Field f : s.getFields()) if (f.isMeta == null && (m == this || f.isPrivate == null) && f.label.equals(name))
if (resolution == 1) {
Expr x = null;
if (rootsig == null) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
} else if (rootsig.isSameOrDescendentOf(f.sig)) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
if (fullname.charAt(0) != '@')
x = THIS.join(x);
} else if (rootfield == null || rootfield.expr.mult() == ExprUnary.Op.EXACTLYOF) {
x = ExprUnary.Op.NOOP.make(pos, f, null, 1);
}
// penalty of 1
if (x != null) {
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
} else if (rootfield == null || rootsig.isSameOrDescendentOf(f.sig)) {
Expr x0 = ExprUnary.Op.NOOP.make(pos, f, null, 0);
if (resolution == 2 && THIS != null && fullname.charAt(0) != '@' && f.type().firstColumnOverlaps(THIS.type())) {
ch.add(THIS.join(x0));
re.add("field " + f.sig.label + " <: this." + f.label);
if (rootsig != null)
continue;
}
ch.add(x0);
re.add("field " + f.sig.label + " <: " + f.label);
}
if (metaSig() != null && (rootsig == null || rootfield == null)) {
SafeList<PrimSig> children = null;
try {
children = metaSig().children();
} catch (Err err) {
return null;
}
// exception NOT possible
for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
}
if (metaField() != null && (rootsig == null || rootfield == null)) {
SafeList<PrimSig> children = null;
try {
children = metaField().children();
} catch (Err err) {
return null;
}
// exception NOT possible
for (PrimSig s : children) for (Field f : s.getFields()) if (f.label.equals(name)) {
Expr x = ExprUnary.Op.NOOP.make(pos, f, null, 0);
ch.add(x);
re.add("field " + f.sig.label + " <: " + f.label);
}
}
return null;
}
use of edu.mit.csail.sdg.ast.Sig.Field 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);
}
}
Aggregations