use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class BookExamples method trial.
/**
* This tries a particular solution against the formula.
*/
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
try {
frame.kr2typeCLEAR();
Bounds b = null;
TupleSet ts = null;
for (int i = 1; i < t.length; i++) {
Object x = t[i];
if (x == null)
return null;
if (x instanceof String && ((String) x).length() > 0) {
// This
// means
// it's
// a
// unary
// Tuple
// containing
// the
// given
// atom
Tuple xx = fac.tuple((String) x);
if (ts == null)
ts = fac.noneOf(1);
ts.add(xx);
continue;
}
if (x instanceof Tuple) {
// This means it's a Tuple
Tuple xx = (Tuple) x;
if (ts == null)
ts = fac.noneOf(xx.arity());
ts.add(xx);
continue;
}
if (x instanceof String) {
// The empty string means the sig
// name follows here
i++;
if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
return null;
String sigName = (String) (t[i]);
i++;
String fieldName = (String) (t[i]);
Sig first = hasSig(sigs, sigName);
if (first == null)
return null;
Expression expr = null;
if (fieldName.length() == 0) {
expr = frame.a2k(first);
} else {
for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
expr = frame.a2k(field);
while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
break;
}
}
if (!(expr instanceof Relation))
return null;
if (b == null)
// We delay the expansive
b = frame.getBounds();
// really find a possible match
if (ts == null)
ts = fac.noneOf(expr.arity());
if (!ts.containsAll(b.lowerBound((Relation) expr)))
// Sanity check
return null;
if (!b.upperBound((Relation) expr).containsAll(ts))
// Sanity check
return null;
b.boundExactly((Relation) expr, ts);
ts = null;
continue;
}
}
SATFactory sat = solver.options().solver();
Solution sol;
try {
solver.options().setSolver(SATFactory.DefaultSAT4J);
sol = solver.solve(f, b);
} finally {
solver.options().setSolver(sat);
}
if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
return null;
if (rep != null)
rep.debug("Comment: " + t[0] + "\n");
return sol;
} catch (Throwable ex) {
return null;
}
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class InternalTest method test1.
public void test1() throws Exception {
XMLNode xml = new XMLNode(new StringReader("<alloy builddate='unknown'>" + "<instance bitwidth='2' maxseq='1' command='Run Deadlock' filename='dijkstra.als'>" + "<sig label='univ' ID='0' builtin='yes'> <atom label='-2'/> <atom label='-1'/> <atom label='0'/> <atom label='1'/> <atom label='State$0'/> <atom label='State$1'/> <atom label='State$2'/> </sig>" + "<sig label='Int' ID='1' parentID='0' builtin='yes'> <atom label='-2'/> <atom label='-1'/> <atom label='0'/> <atom label='1'/> </sig>" + "<sig label='seq/Int' ID='2' parentID='1' builtin='yes'> <atom label='0'/> </sig>" + "<sig label='State' ID='5' parentID='0'> <atom label='State$0'/> <atom label='State$1'/> <atom label='State$2'/> </sig>" + "<field label='len' parentID='5' ID='17'>" + " <tuple> <atom label='State$1'/> <atom label='-2'/> </tuple>" + " <tuple> <atom label='State$1'/> <atom label='-1'/> </tuple>" + " <types> <type ID='5'/> <type ID='1'/> </types>" + "</field>" + "<skolem label='$Deadlock_s' ID='16'>" + " <tuple> <atom label='State$0'/> </tuple>" + " <types> <type ID='5'/> </types>" + "</skolem>" + "</instance>" + "</alloy>"));
Sig state = new Sig.PrimSig("State");
A4Solution sol = A4SolutionReader.read(Arrays.asList(state), xml);
SafeList<ExprVar> skolems = new SafeList<ExprVar>(sol.getAllSkolems());
check(skolems.size() == 1);
check(skolems.get(0).label, "$Deadlock_s");
check(skolems.get(0).type(), state.type());
//
Sig state2 = new Sig.PrimSig("State");
Field field2 = state2.addField("len", Sig.SIGINT);
sol = A4SolutionReader.read(Arrays.asList(state2), xml);
SafeList<ExprVar> skolems2 = new SafeList<ExprVar>(sol.getAllSkolems());
check(skolems2.size() == 1);
check(skolems2.get(0).label, "$Deadlock_s");
check(skolems2.get(0).type(), state2.type());
check("" + sol.eval(field2.cardinality()), "-2");
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class InternalTest method test2.
public void test2() throws Exception {
test1();
XMLNode xml = new XMLNode(new StringReader("<alloy builddate='unknown'>" + "<instance bitwidth='2' maxseq='1' command='Run Deadlock' filename='dijkstra.als'>" + "<sig label='univ' ID='0' builtin='yes'> <atom label='-2'/> <atom label='-1'/> <atom label='0'/> <atom label='1'/> <atom label='State$0'/> <atom label='State$1'/> <atom label='State$2'/> </sig>" + "<sig label='Int' ID='1' parentID='0' builtin='yes'> <atom label='-2'/> <atom label='-1'/> <atom label='0'/> <atom label='1'/> </sig>" + "<sig label='seq/Int' ID='2' parentID='1' builtin='yes'> <atom label='0'/> </sig>" + "<sig label='Act' ID='5' parentID='0'> <atom label='Act$0'/> <atom label='Act$1'/> <atom label='Act$2'/> </sig>" + "<skolem label='$x' ID='16'>" + " <tuple> <atom label='0'/> <atom label='Act$1'/> </tuple>" + " <types> <type ID='2'/> <type ID='5'/> </types>" + "</skolem>" + "</instance>" + "</alloy>"));
Sig activity = new Sig.PrimSig("Act");
A4Solution sol = A4SolutionReader.read(Arrays.asList(activity), xml);
SafeList<ExprVar> skolems = new SafeList<ExprVar>(sol.getAllSkolems());
check(skolems.size() == 1);
check(skolems.get(0).label, "$x");
check(skolems.get(0).type(), Sig.SEQIDX.type().product(activity.type()));
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class Helper method atom2sig.
/**
* Given an A4Solution, return a map that maps every atom to its most specific
* signature.
* <p>
* For example, suppose we have "sig Animal { }" and "sig Dog, Cat extends
* Animal { }". Suppose the solution says Animal={A$1, A$2, A$3, A$4} and
* Dog={A$1} and Cat={A$2, A$3}. This method will return a map that maps A$1 to
* Dog, A$2 to Cat, A$3 to Cat, and A$4 to Animal. (A$1 is both an Animal and a
* Dog, but Dog is a subtype of Animal, so Dog is A$1's most specific signature)
*/
public static Map<String, PrimSig> atom2sig(A4Solution solution) throws Err {
Map<String, PrimSig> map = new HashMap<String, PrimSig>();
for (Sig s : solution.getAllReachableSigs()) if (s instanceof PrimSig)
for (A4Tuple t : solution.eval(s)) {
// We skip over SubsetSig and only care about PrimSig
String atom = t.atom(0);
PrimSig old = map.get(atom);
if (old == null || ((PrimSig) s).isSameOrDescendentOf(old)) {
map.put(atom, (PrimSig) s);
}
}
return map;
}
use of edu.mit.csail.sdg.ast.Sig in project org.alloytools.alloy by AlloyTools.
the class ExampleCompilingFromSource method compileModuleAndRun.
public static void compileModuleAndRun(String model) throws Err {
A4Reporter rep = new A4Reporter();
// parse model from string
CompModule world = CompUtil.parseEverything_fromString(rep, model);
ConstList<Command> commands = world.getAllCommands();
if (commands.size() != 1)
throw new ErrorAPI("Must specify exactly one command; number of commands found: " + commands.size());
Command cmd = commands.get(0);
A4Options opt = new A4Options();
opt.solver = A4Options.SatSolver.SAT4J;
// solve
A4Solution sol = TranslateAlloyToKodkod.execute_command(rep, world.getAllSigs(), cmd, opt);
// print solution
System.out.println(sol);
for (Sig sig : world.getAllReachableSigs()) {
System.out.println("traversing sig: " + sig);
SafeList<Field> fields = sig.getFields();
for (Field field : fields) {
System.out.println(" traversing field: " + field);
A4TupleSet ts = (sol.eval(field));
for (A4Tuple t : ts) {
System.out.print(" [");
for (int i = 0; i < t.arity(); i++) System.out.print(t.atom(i) + " ");
System.out.println("]");
}
}
}
}
Aggregations