use of edu.mit.csail.sdg.alloy4.XMLNode 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.alloy4.XMLNode in project org.alloytools.alloy by AlloyTools.
the class SimpleCLI method main.
public static void main(String[] args) throws Exception {
final boolean sat4j = "yes".equals(System.getProperty("sat4j"));
final boolean minisat = "yes".equals(System.getProperty("minisat"));
SatSolver solver = A4Options.SatSolver.make("mem", "mem", "/zweb/sat/mem");
final SimpleReporter rep = new SimpleReporter();
final StringBuilder sb = rep.sb;
for (String filename : args) {
try {
// Parse+Typecheck
rep.sb.append("\n\nMain file = " + filename + "\n");
if (db)
db("Parsing+Typechecking...");
Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
if (db)
db(" ok\n");
List<Command> cmds = world.getAllCommands();
for (ErrorWarning msg : rep.warnings) rep.sb.append("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
rep.warnings.clear();
// Do a detailed dump if we will not be executing the commands
if (args.length != 1) {
for (Module m : world.getAllReachableModules()) {
for (Sig x : m.getAllSigs()) {
sb.append("\nSig ").append(x.label).append(" at position ").append(x.pos).append("\n");
for (Decl d : x.getFieldDecls()) for (ExprHasName f : d.names) {
sb.append("\nField ").append(f.label).append(" with type ").append(f.type()).append("\n");
d.expr.toString(sb, 2);
}
rep.flush();
}
for (Func x : m.getAllFunc()) {
sb.append("\nFun/pred ").append(x.label).append(" at position ").append(x.pos).append("\n");
for (Decl d : x.decls) for (ExprHasName v : d.names) {
v.toString(sb, 2);
d.expr.toString(sb, 4);
}
x.returnDecl.toString(sb, 2);
x.getBody().toString(sb, 4);
rep.flush();
}
for (Pair<String, Expr> x : m.getAllFacts()) {
sb.append("\nFact ").append(x.a).append("\n");
x.b.toString(sb, 4);
rep.flush();
}
for (Pair<String, Expr> x : m.getAllAssertions()) {
sb.append("\nAssertion ").append(x.a).append("\n");
x.b.toString(sb, 4);
rep.flush();
}
if (m == world)
for (Command x : m.getAllCommands()) {
sb.append("\nCommand ").append(x.label).append("\n");
x.formula.toString(sb, 4);
rep.flush();
}
}
continue;
}
// Validate the metamodel generation code
StringWriter metasb = new StringWriter();
PrintWriter meta = new PrintWriter(metasb);
Util.encodeXMLs(meta, "\n<alloy builddate=\"", Version.buildDate(), "\">\n\n");
A4SolutionWriter.writeMetamodel(world.getAllReachableSigs(), filename, meta);
Util.encodeXMLs(meta, "\n</alloy>");
meta.flush();
metasb.flush();
String metaxml = metasb.toString();
A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new StringReader(metaxml)));
StaticInstanceReader.parseInstance(new StringReader(metaxml));
// Okay, now solve the commands
A4Options options = new A4Options();
options.originalFilename = filename;
options.solverDirectory = "/zweb/zweb/tmp/alloy4/x86-freebsd";
options.solver = sat4j ? A4Options.SatSolver.SAT4J : (minisat ? A4Options.SatSolver.MiniSatJNI : solver);
for (int i = 0; i < cmds.size(); i++) {
Command c = cmds.get(i);
if (db) {
String cc = c.toString();
if (cc.length() > 60)
cc = cc.substring(0, 55);
db("Executing " + cc + "...\n");
}
rep.sb.append("Executing \"" + c + "\"\n");
options.skolemDepth = 0;
A4Solution s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
if (s.satisfiable()) {
validate(s);
if (s.isIncremental()) {
s = s.next();
if (s.satisfiable())
validate(s);
}
}
options.skolemDepth = 2;
s = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), c, options);
if (s.satisfiable()) {
validate(s);
if (s.isIncremental()) {
s = s.next();
if (s.satisfiable())
validate(s);
}
}
}
} catch (Throwable ex) {
rep.sb.append("\n\nException: " + ex);
}
if (db) {
if (args.length != 1)
db(" ERROR!\n");
else
db("\n\n");
}
}
rep.close();
}
use of edu.mit.csail.sdg.alloy4.XMLNode in project org.alloytools.alloy by AlloyTools.
the class A4SolutionReader method parseSig.
/**
* Parse sig/set.
*/
private Sig parseSig(String id, int depth) throws IOException, Err {
Sig ans = id2sig.get(id);
if (ans != null)
return ans;
XMLNode node = nmap.get(id);
if (node == null)
throw new IOException("Unknown SigID " + id + " encountered.");
if (!node.is("sig"))
throw new IOException("ID " + id + " is not a sig.");
String label = label(node);
Attr isAbstract = yes(node, "abstract") ? Attr.ABSTRACT : null;
Attr isOne = yes(node, "one") ? Attr.ONE : null;
Attr isLone = yes(node, "lone") ? Attr.LONE : null;
Attr isSome = yes(node, "some") ? Attr.SOME : null;
Attr isPrivate = yes(node, "private") ? Attr.PRIVATE : null;
Attr isMeta = yes(node, "meta") ? Attr.META : null;
Attr isEnum = yes(node, "enum") ? Attr.ENUM : null;
Attr isExact = yes(node, "exact") ? Attr.EXACT : null;
if (yes(node, "builtin")) {
if (label.equals(UNIV.label)) {
id2sig.put(id, UNIV);
return UNIV;
}
if (label.equals(SIGINT.label)) {
id2sig.put(id, SIGINT);
return SIGINT;
}
if (label.equals(SEQIDX.label)) {
id2sig.put(id, SEQIDX);
return SEQIDX;
}
if (label.equals(STRING.label)) {
id2sig.put(id, STRING);
return STRING;
}
throw new IOException("Unknown builtin sig: " + label + " (id=" + id + ")");
}
if (depth > nmap.size())
throw new IOException("Sig " + label + " (id=" + id + ") is in a cyclic inheritance relationship.");
List<Sig> parents = null;
TupleSet ts = factory.noneOf(1);
for (XMLNode sub : node) {
if (sub.is("atom")) {
ts.add(factory.tuple(sub.getAttribute("label")));
continue;
}
if (!sub.is("type"))
continue;
Sig parent = parseSig(sub.getAttribute("ID"), depth + 1);
if (parents == null)
parents = new ArrayList<Sig>();
parents.add(parent);
}
if (parents == null) {
String parentID = node.getAttribute("parentID");
Sig parent = parseSig(parentID, depth + 1);
if (!(parent instanceof PrimSig))
throw new IOException("Parent of sig " + label + " (id=" + id + ") must not be a subset sig.");
for (Expr choice : choices) if (choice instanceof PrimSig && parent == ((PrimSig) choice).parent && ((Sig) choice).label.equals(label)) {
ans = (Sig) choice;
choices.remove(choice);
break;
}
if (ans == null) {
ans = new PrimSig(label, (PrimSig) parent, isAbstract, isLone, isOne, isSome, isPrivate, isMeta, isEnum);
allsigs.add(ans);
}
} else {
for (Expr choice : choices) if (choice instanceof SubsetSig && ((Sig) choice).label.equals(label) && sameset(parents, ((SubsetSig) choice).parents)) {
ans = (Sig) choice;
choices.remove(choice);
break;
}
if (ans == null) {
ans = new SubsetSig(label, parents, isExact, isLone, isOne, isSome, isPrivate, isMeta);
allsigs.add(ans);
}
}
id2sig.put(id, ans);
expr2ts.put(ans, ts);
if (ans instanceof PrimSig) {
// Add the atoms in this SIG into all parent sigs
for (PrimSig ans2 = ((PrimSig) ans).parent; ans2 != null && !ans2.builtin; ans2 = ans2.parent) {
TupleSet ts2 = expr2ts.get(ans2);
if (ts2 == null)
ts2 = ts.clone();
else {
ts2 = ts2.clone();
ts2.addAll(ts);
}
expr2ts.put(ans2, ts2);
}
}
return ans;
}
use of edu.mit.csail.sdg.alloy4.XMLNode in project org.alloytools.alloy by AlloyTools.
the class StaticThemeReaderWriter method parseAlloyRelation.
/*
* ========================================================= ================
* ===================
*/
/**
* Return null if the element is malformed.
*/
private static AlloyRelation parseAlloyRelation(VizState now, XMLNode x) {
/*
* <relation name="name"> 2 or more <type name=".."/> </relation>
*/
List<AlloyType> ans = new ArrayList<AlloyType>();
if (!x.is("relation"))
return null;
String name = x.getAttribute("name");
if (name.length() == 0)
return null;
for (XMLNode sub : x.getChildren("type")) {
String typename = sub.getAttribute("name");
if (typename.length() == 0)
return null;
AlloyType t = now.getCurrentModel().hasType(typename);
if (t == null)
return null;
ans.add(t);
}
if (ans.size() < 2)
return null;
else
return now.getCurrentModel().hasRelation(name, ans);
}
use of edu.mit.csail.sdg.alloy4.XMLNode in project org.alloytools.alloy by AlloyTools.
the class SimpleReporter method validate.
/**
* Validate the given filename to see if it is a valid Alloy XML instance file.
*/
private static void validate(String filename) throws Exception {
A4SolutionReader.read(new ArrayList<Sig>(), new XMLNode(new File(filename))).toString();
StaticInstanceReader.parseInstance(new File(filename));
}
Aggregations