use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseType.
/**
* Parse type.
*/
private Expr parseType(XMLNode node) throws IOException, Err {
Expr expr = null;
if (!node.is("types"))
throw new IOException("<types>...</type> expected");
for (XMLNode n : node) if (n.is("type")) {
Sig sig = parseSig(n.getAttribute("ID"), 0);
if (expr == null)
expr = sig;
else
expr = expr.product(sig);
}
if (expr == null)
throw new IOException("<type ID=../> expected");
return expr;
}
use of edu.mit.csail.sdg.ast.Sig 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 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 in project org.alloytools.alloy by AlloyTools.
the class StaticInstanceReader method sigMETA.
/**
* Returns the AlloyType corresponding to the given sig; create an AlloyType for
* it if none existed before.
*/
private void sigMETA(SubsetSig s) throws Err {
AlloyAtom atom;
AlloyType type = sig2type.get(s);
if (type != null)
return;
type = makeType(s.label, s.isOne != null, s.isAbstract != null, false, s.isPrivate != null, s.isMeta != null, s.isEnum != null);
atom = new AlloyAtom(type, Integer.MAX_VALUE, s.label);
atom2sets.put(atom, new LinkedHashSet<AlloySet>());
sig2atom.put(s, atom);
sig2type.put(s, type);
ts.put(type, AlloyType.SET);
for (Sig p : s.parents) {
if (p instanceof SubsetSig)
sigMETA((SubsetSig) p);
else
sigMETA((PrimSig) p);
ins.add(new AlloyTuple(atom, sig2atom.get(p)));
}
}
use of edu.mit.csail.sdg.ast.Sig 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