use of edu.mit.csail.sdg.translator.A4Solution in project org.alloytools.alloy by AlloyTools.
the class SimpleReporter method resultSAT.
/**
* {@inheritDoc}
*/
@Override
public void resultSAT(Object command, long solvingTime, Object solution) {
if (!(solution instanceof A4Solution) || !(command instanceof Command))
return;
A4Solution sol = (A4Solution) solution;
Command cmd = (Command) command;
String formula = recordKodkod ? sol.debugExtractKInput() : "";
String filename = tempfile + ".xml";
synchronized (SimpleReporter.class) {
try {
cb("R3", " Writing the XML file...");
if (latestModule != null)
writeXML(this, latestModule, filename, sol, latestKodkodSRC);
} catch (Throwable ex) {
cb("bold", "\n" + (ex.toString().trim()) + "\nStackTrace:\n" + (MailBug.dump(ex).trim()) + "\n");
return;
}
latestKodkods.clear();
latestKodkods.add(sol.toString());
latestKodkod = sol;
latestKodkodXML = filename;
}
String formulafilename = "";
if (formula.length() > 0 && tempfile != null) {
formulafilename = tempfile + ".java";
try {
Util.writeAll(formulafilename, formula);
formulafilename = "CNF: " + formulafilename;
} catch (Throwable ex) {
formulafilename = "";
}
}
cb("sat", cmd.check, cmd.expects, filename, formulafilename, System.currentTimeMillis() - lastTime);
}
use of edu.mit.csail.sdg.translator.A4Solution in project org.alloytools.alloy by AlloyTools.
the class SimpleReporter method resultUNSAT.
/**
* {@inheritDoc}
*/
@Override
public void resultUNSAT(Object command, long solvingTime, Object solution) {
if (!(solution instanceof A4Solution) || !(command instanceof Command))
return;
A4Solution sol = (A4Solution) solution;
Command cmd = (Command) command;
String originalFormula = recordKodkod ? sol.debugExtractKInput() : "";
String corefilename = "", formulafilename = "";
if (originalFormula.length() > 0 && tempfile != null) {
formulafilename = tempfile + ".java";
try {
Util.writeAll(formulafilename, originalFormula);
formulafilename = "CNF: " + formulafilename;
} catch (Throwable ex) {
formulafilename = "";
}
}
Pair<Set<Pos>, Set<Pos>> core = sol.highLevelCore();
if ((core.a.size() > 0 || core.b.size() > 0) && tempfile != null) {
corefilename = tempfile + ".core";
OutputStream fs = null;
ObjectOutputStream os = null;
try {
fs = new FileOutputStream(corefilename);
os = new ObjectOutputStream(fs);
os.writeObject(core);
os.writeObject(sol.lowLevelCore());
corefilename = "CORE: " + corefilename;
} catch (Throwable ex) {
corefilename = "";
} finally {
Util.close(os);
Util.close(fs);
}
}
if (minimized == 0)
cb("unsat", cmd.check, cmd.expects, (System.currentTimeMillis() - lastTime), formulafilename);
else
cb("unsat", cmd.check, cmd.expects, minimized - lastTime, formulafilename, corefilename, minimizedBefore, minimizedAfter, (System.currentTimeMillis() - minimized));
}
use of edu.mit.csail.sdg.translator.A4Solution 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.translator.A4Solution 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();
}
}
use of edu.mit.csail.sdg.translator.A4Solution in project org.alloytools.alloy by AlloyTools.
the class ExampleUsingTheCompiler method main.
/*
* Execute every command in every file. This method parses every file, then
* execute every command. If there are syntax or type errors, it may throw a
* ErrorSyntax or ErrorType or ErrorAPI or ErrorFatal exception. You should
* catch them and display them, and they may contain filename/line/column
* information.
*/
public static void main(String[] args) throws Err {
// The visualizer (We will initialize it to nonnull when we visualize an
// Alloy solution)
VizGUI viz = null;
// Alloy4 sends diagnostic messages and progress reports to the
// A4Reporter.
// By default, the A4Reporter ignores all these events (but you can
// extend the A4Reporter to display the event for the user)
A4Reporter rep = new A4Reporter() {
// For example, here we choose to display each "warning" by printing
// it to System.out
@Override
public void warning(ErrorWarning msg) {
System.out.print("Relevance Warning:\n" + (msg.toString().trim()) + "\n\n");
System.out.flush();
}
};
for (String filename : args) {
// Parse+typecheck the model
System.out.println("=========== Parsing+Typechecking " + filename + " =============");
Module world = CompUtil.parseEverything_fromFile(rep, null, filename);
// Choose some default options for how you want to execute the
// commands
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.SAT4J;
for (Command command : world.getAllCommands()) {
// Execute the command
System.out.println("============ Command " + command + ": ============");
A4Solution ans = TranslateAlloyToKodkod.execute_command(rep, world.getAllReachableSigs(), command, options);
// Print the outcome
System.out.println(ans);
// If satisfiable...
if (ans.satisfiable()) {
// You can query "ans" to find out the values of each set or
// type.
// This can be useful for debugging.
//
// You can also write the outcome to an XML file
ans.writeXML("alloy_example_output.xml");
// You can then visualize the XML file by calling this:
if (viz == null) {
viz = new VizGUI(false, "alloy_example_output.xml", null);
} else {
viz.loadXML("alloy_example_output.xml", true);
}
}
}
}
}
Aggregations