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, 5); cmd2 = cmd2.change(B, true, 6); A4Solution sol2 = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd2, opt); System.out.println("[Solution2]:"); System.out.println(sol2.toString()); }
/* * Execute all the commands in the first file and place any counterexamples * into the output file. * * 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, FileNotFoundException { if (args.length != 2) { System.out.print("Usage: <alloy file> <output file>\n"); System.exit(1); } // 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(); } @Override public void solve(int primaryVars, int totalVars, int clauses) { System.out.println( "... solving (" + Integer.toString(primaryVars) + " primary variables)"); System.out.flush(); } @Override public void translate( java.lang.String solver, int bitwidth, int maxseq, int skolemDepth, int symmetry) { System.out.println( "... generating CNF for " + solver + " (" + Integer.toString(bitwidth) + " bitwidth)"); System.out.flush(); } }; String filename = args[0]; PrintStream output = new PrintStream(new File(args[1])); // Identify yourself System.out.println( "Alloy Analyzer " + Version.version() + " (build: " + Version.buildNumber() + " date: " + Version.buildDate() + ")"); try { // Parse+typecheck the model System.out.println("Parsing and 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; boolean foundCounterExamples = false; for (Command command : world.getAllCommands()) { // Execute the command System.out.println("... Executing command " + command + ":"); A4Solution ans = TranslateAlloyToKodkod.execute_command( rep, world.getAllReachableSigs(), command, options); // Print the outcome output.println(command + ":"); output.println(ans); // If satisfiable... if (ans.satisfiable()) { foundCounterExamples = true; // 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); } */ } } if (!foundCounterExamples) System.out.println("\nFound no counterexamples, procedure may be correct."); } catch (Err error) { System.out.println( "Alloy has encountered an error at: " + error.pos.toString() + ": " + error.msg); } }