use of edu.mit.csail.sdg.ast.Expr 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.Expr in project org.alloytools.alloy by AlloyTools.
the class EvaluatorExample method main.
public static void main(String[] args) throws Exception {
A4Reporter rep = new A4Reporter();
File tmpAls = CompUtil.flushModelToFile(model, null);
{
Module world = CompUtil.parseEverything_fromString(rep, model);
A4Options opt = new A4Options();
opt.originalFilename = tmpAls.getAbsolutePath();
opt.solver = A4Options.SatSolver.SAT4J;
Command cmd = world.getAllCommands().get(0);
A4Solution sol = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), cmd, opt);
assert sol.satisfiable();
sol.writeXML(outputfilename);
// eval with existing A4Solution
Expr e = CompUtil.parseOneExpression_fromString(world, "univ");
System.out.println(sol.eval(e));
e = CompUtil.parseOneExpression_fromString(world, "Point");
System.out.println(sol.eval(e));
}
// reload everything from files
{
XMLNode xmlNode = new XMLNode(new File(outputfilename));
String alloySourceFilename = xmlNode.iterator().next().getAttribute("filename");
Module ansWorld = CompUtil.parseEverything_fromFile(rep, null, alloySourceFilename);
A4Solution ans = A4SolutionReader.read(ansWorld.getAllReachableSigs(), xmlNode);
Expr e = CompUtil.parseOneExpression_fromString(ansWorld, "univ");
System.out.println(ans.eval(e));
e = CompUtil.parseOneExpression_fromString(ansWorld, "Point");
System.out.println(ans.eval(e));
}
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class StaticInstanceReader method atoms.
/**
* Constructs the atoms corresponding to the given sig.
*/
private void atoms(A4Solution sol, PrimSig s) throws Err {
Expr sum = Sig.NONE;
for (PrimSig c : s.children()) {
sum = sum.plus(c);
atoms(sol, c);
}
// This ensures
A4TupleSet ts = (A4TupleSet) (sol.eval(s.minus(sum)));
// specific sig
for (A4Tuple z : ts) {
String atom = z.atom(0);
int i, dollar = atom.lastIndexOf('$');
try {
i = Integer.parseInt(dollar >= 0 ? atom.substring(dollar + 1) : atom);
} catch (NumberFormatException ex) {
i = Integer.MAX_VALUE;
}
AlloyAtom at = new AlloyAtom(sig(s), ts.size() == 1 ? Integer.MAX_VALUE : i, atom);
atom2sets.put(at, new LinkedHashSet<AlloySet>());
string2atom.put(atom, at);
}
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class StaticInstanceReader method setOrRel.
/**
* Construct an AlloySet or AlloyRelation corresponding to the given expression.
*/
private void setOrRel(A4Solution sol, String label, Expr expr, boolean isPrivate, boolean isMeta) throws Err {
for (List<PrimSig> ps : expr.type().fold()) {
if (ps.size() == 1) {
PrimSig t = ps.get(0);
AlloySet set = makeSet(label, isPrivate, isMeta, sig(t));
sets.add(set);
for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(t)))) {
atom2sets.get(string2atom.get(tp.atom(0))).add(set);
}
} else {
Expr mask = null;
List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
for (int i = 0; i < ps.size(); i++) {
types.add(sig(ps.get(i)));
if (mask == null)
mask = ps.get(i);
else
mask = mask.product(ps.get(i));
}
AlloyRelation rel = makeRel(label, isPrivate, isMeta, types);
Set<AlloyTuple> ts = new LinkedHashSet<AlloyTuple>();
for (A4Tuple tp : (A4TupleSet) (sol.eval(expr.intersect(mask)))) {
AlloyAtom[] atoms = new AlloyAtom[tp.arity()];
for (int i = 0; i < tp.arity(); i++) {
atoms[i] = string2atom.get(tp.atom(i));
if (atoms[i] == null)
throw new ErrorFatal("Unexpected XML inconsistency: cannot resolve atom " + tp.atom(i));
}
ts.add(new AlloyTuple(atoms));
}
rels.put(rel, ts);
}
}
}
use of edu.mit.csail.sdg.ast.Expr in project org.alloytools.alloy by AlloyTools.
the class ExampleUsingTheAPI method main.
public static void main(String[] args) throws Err {
// Chooses the Alloy4 options
A4Options opt = new A4Options();
opt.solver = A4Options.SatSolver.SAT4J;
// abstract sig A {}
PrimSig A = new PrimSig("A", Attr.ABSTRACT);
// sig B {}
PrimSig B = new PrimSig("B");
// one sig A1 extends A {}
PrimSig A1 = new PrimSig("A1", A, Attr.ONE);
// one sig A2 extends A {}
PrimSig A2 = new PrimSig("A2", A, Attr.ONE);
// A { f: B lone->lone B }
Expr f = A.addField("f", B.lone_arrow_lone(B));
// Since (B lone->lone B) is not unary, the default is "setOf", meaning
// "f:set (B lone->lone B)"
// A { g: B }
Expr g = A.addField("g", B);
// The line above is the same as: A.addField(null, "g", B.oneOf()) since
// B is unary.
// If you want "setOf", you need: A.addField(null, "g", B.setOf())
// pred someG { some g }
Func someG = new Func(null, "SomeG", null, null, g.some());
// pred atMostThree[x:univ, y:univ] { #(x+y) >= 3 }
Decl x = UNIV.oneOf("x");
Decl y = UNIV.oneOf("y");
Expr body = x.get().plus(y.get()).cardinality().lte(ExprConstant.makeNUMBER(3));
Func atMost3 = new Func(null, "atMost3", Util.asList(x, y), null, body);
List<Sig> sigs = Arrays.asList(new Sig[] { A, B, A1, A2 });
// run { some A && atMostThree[B,B] } for 3 but 3 int, 3 seq
Expr expr1 = A.some().and(atMost3.call(B, B));
Command cmd1 = new Command(false, 3, 3, 3, expr1);
A4Solution sol1 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd1, opt);
System.out.println("[Solution1]:");
System.out.println(sol1.toString());
// run { some f && SomeG[] } for 3 but 2 int, 1 seq, 5 A, exactly 6 B
Expr expr2 = f.some().and(someG.call());
Command cmd2 = new Command(false, 3, 2, 1, expr2);
cmd2 = cmd2.change(A, false, 1);
cmd2 = cmd2.change(B, true, 1);
A4Solution sol2 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd2, opt);
while (sol2.satisfiable()) {
System.out.println("[Solution2]:");
System.out.println(sol2.toString());
sol2 = sol2.next();
}
}
Aggregations