Package edu.mit.csail.sdg.alloy4compiler.translator

Examples of edu.mit.csail.sdg.alloy4compiler.translator.A4Solution


      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);
        int cont = 1;
        long tempoAntes = System.currentTimeMillis();

        while (ans != null) {
           System.out.println("Geracao: " + i);
          ans = ans.next();
          if (ans.satisfiable())
            cont++;

          else
            break;
          i++;
View Full Code Here


    }

    void runFor3(Expr expr) throws Err {
        A4Options opt = new A4Options();
        Command cmd = new Command(false, 3, 3, 3, expr.and(fact));
        A4Solution sol = TranslateAlloyToKodkod.execute_command(NOP, sigs, cmd, opt);
        System.out.println(sol.toString().trim());
        if (sol.satisfiable()) {
            System.out.println("In particular, File = " + sol.eval(file));
            System.out.println("In particular, Dir = " + sol.eval(dir));
            System.out.println("In particular, contains = " + sol.eval(contains));
            System.out.println("In particular, parent = " + sol.eval(parent));
        }
        System.out.println();
    }
View Full Code Here

    }

    /** {@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) {
View Full Code Here

    @Override public void minimized(Object command, int before, int after) { minimizedBefore=before; minimizedAfter=after; }

    /** {@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);
View Full Code Here

        public transient WorkerCallback out = null;
        private void cb(Object... objs) throws Exception { out.callback(objs); }
        public void run(WorkerCallback out) throws Exception {
            this.out = out;
            cb("S2", "Enumerating...\n");
            A4Solution sol;
            Module mod;
            synchronized(SimpleReporter.class) {
                if (latestMetamodelXML!=null && latestMetamodelXML.equals(filename))
                   {cb("pop", "You cannot enumerate a metamodel.\n"); return;}
                if (latestKodkodXML==null || !latestKodkodXML.equals(filename))
                   {cb("pop", "You can only enumerate the solutions of the most-recently-solved command."); return;}
                if (latestKodkod==null || latestModule==null || latestKodkodSRC==null)
                   {cb("pop", "Error: the SAT solver that generated the instance has exited,\nso we cannot enumerate unless you re-solve that command.\n"); return;}
                sol=latestKodkod;
                mod=latestModule;
            }
            if (!sol.satisfiable())
                {cb("pop", "Error: This command is unsatisfiable,\nso there are no solutions to enumerate."); return;}
            if (!sol.isIncremental())
                {cb("pop", "Error: This solution was not generated by an incremental SAT solver.\n" +
                "Currently only MiniSat and SAT4J are supported."); return;}
            int tries=0;
            while(true) {
                sol=sol.next();
                if (!sol.satisfiable())
                   {cb("pop", "There are no more satisfying instances.\n\n" +
                   "Note: due to symmetry breaking and other optimizations,\n" +
                   "some equivalent solutions may have been omitted."); return;}
                String toString = sol.toString();
                synchronized(SimpleReporter.class) {
                    if (!latestKodkods.add(toString)) if (tries<100) { tries++; continue; }
                    // The counter is needed to avoid a Kodkod bug where sometimes we might repeat the same solution infinitely number of times; this at least allows the user to keep going
                    writeXML(null, mod, filename, sol, latestKodkodSRC); latestKodkod=sol;
                }
View Full Code Here

                final String tempXML=tempdir+File.separatorChar+i+".cnf.xml";
                final String tempCNF=tempdir+File.separatorChar+i+".cnf";
                final Command cmd=cmds.get(i);
                rep.tempfile=tempCNF;
                cb(out, "bold", "Executing \""+cmd+"\"\n");
                A4Solution ai=TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), cmd, options);
                if (ai==null) result.add(null);
                else if (ai.satisfiable()) result.add(tempXML);
                else if (ai.highLevelCore().a.size()>0) result.add(tempCNF+".core");
                else result.add("");
            }
            (new File(tempdir)).delete(); // In case it was UNSAT, or canceled...
            if (result.size()>1) {
                rep.cb("bold", "" + result.size() + " commands were executed. The results are:\n");
View Full Code Here

        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());
    }
View Full Code Here

            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 {
View Full Code Here

   private StaticInstanceReader(XMLNode root) throws Err {
      XMLNode inst = null;
      for(XMLNode sub: root) if (sub.is("instance")) { inst=sub; break; }
      if (inst==null) throw new ErrorSyntax("The XML file must contain an <instance> element.");
      boolean isMeta = "yes".equals(inst.getAttribute("metamodel"));
      A4Solution sol = A4SolutionReader.read(new ArrayList<Sig>(), root);
      for (Sig s:sol.getAllReachableSigs()) if (s instanceof PrimSig && ((PrimSig)s).parent==Sig.UNIV) toplevels.add((PrimSig)s);
      if (!isMeta) {
         sig2type.put(Sig.UNIV, AlloyType.UNIV);
         sig2type.put(Sig.SIGINT, AlloyType.INT);
         sig2type.put(Sig.SEQIDX, AlloyType.SEQINT);
         sig2type.put(Sig.STRING, AlloyType.STRING);
         ts.put(AlloyType.SEQINT, AlloyType.INT);
         for(int i=sol.min(), max=sol.max(), maxseq=sol.getMaxSeq(); i<=max; i++) {
            AlloyAtom at = new AlloyAtom(i>=0 && i<maxseq ? AlloyType.SEQINT : AlloyType.INT, i, ""+i);
            atom2sets.put(at, new LinkedHashSet<AlloySet>());
            string2atom.put(""+i, at);
         }
         for(Sig s:sol.getAllReachableSigs()) if (!s.builtin && s instanceof PrimSig) sig((PrimSig)s);
         for(Sig s:toplevels)                 if (!s.builtin || s==Sig.STRING)        atoms(sol, (PrimSig)s);
         for(Sig s:sol.getAllReachableSigs()) if (s instanceof SubsetSig)             setOrRel(sol, s.label, s, s.isPrivate!=null, s.isMeta!=null);
         for(Sig s:sol.getAllReachableSigs()) for(Field f:s.getFields())              setOrRel(sol, f.label, f, f.isPrivate!=null, f.isMeta!=null);
         for(ExprVar s:sol.getAllSkolems())   setOrRel(sol, s.label, s, false, false);
      }
      if (isMeta) {
         sigMETA(Sig.UNIV);
         for(Sig s:sol.getAllReachableSigs()) if (s instanceof SubsetSig) sigMETA((SubsetSig)s);
         for(Sig s:sol.getAllReachableSigs()) for(Field f:s.getFields()) {
            for(List<PrimSig> ps:f.type().fold()) {
               List<AlloyType> types = new ArrayList<AlloyType>(ps.size());
               AlloyAtom[] tuple = new AlloyAtom[ps.size()];
               for(int i=0; i<ps.size(); i++) {
                  types.add(sig(ps.get(i)));
                  tuple[i] = sig2atom.get(ps.get(i));
               }
               AlloyRelation rel = makeRel(f.label, f.isPrivate!=null, false, types);
               rels.put(rel, Util.asSet(new AlloyTuple(tuple)));
            }
         }
         if (ins.size()>0) { sig2type.put(null, AlloyType.SET); rels.put(AlloyRelation.IN, ins); }
         AlloyAtom univAtom = sig2atom.get(Sig.UNIV);
         AlloyAtom intAtom = sig2atom.get(Sig.SIGINT);
         AlloyAtom seqAtom = sig2atom.get(Sig.SEQIDX);
         AlloyAtom strAtom = sig2atom.get(Sig.STRING);
         for(Set<AlloyTuple> t: rels.values()) for(AlloyTuple at: t) if (at.getAtoms().contains(univAtom)) { univAtom=null; break; }
         for(Set<AlloyTuple> t: rels.values()) for(AlloyTuple at: t) if (at.getAtoms().contains(intAtom)) { intAtom=null; break; }
         for(Set<AlloyTuple> t: rels.values()) for(AlloyTuple at: t) if (at.getAtoms().contains(seqAtom)) { seqAtom=null; break; }
         for(Set<AlloyTuple> t: rels.values()) for(AlloyTuple at: t) if (at.getAtoms().contains(strAtom)) { strAtom=null; break; }
         if (univAtom!=null) {
            for(Iterator<AlloyTuple> it=exts.iterator(); it.hasNext();) {
               AlloyTuple at=it.next();
               if (at.getStart()==univAtom || at.getEnd()==univAtom) it.remove();
            }
            atom2sets.remove(univAtom);
         }
         if (strAtom!=null) {
            for(Iterator<AlloyTuple> it=exts.iterator(); it.hasNext();) {
               AlloyTuple at=it.next();
               if (at.getStart()==strAtom || at.getEnd()==strAtom) it.remove();
            }
            atom2sets.remove(strAtom);
         }
         if (intAtom!=null && seqAtom!=null) {
            for(Iterator<AlloyTuple> it=exts.iterator(); it.hasNext();) {
               AlloyTuple at=it.next();
               if (at.getStart()==intAtom || at.getEnd()==intAtom || at.getStart()==seqAtom || at.getEnd()==seqAtom) it.remove();
            }
            atom2sets.remove(intAtom);
            atom2sets.remove(seqAtom);
         }
         if (exts.size()>0) { rels.put(AlloyRelation.EXTENDS, exts); }
      }
      AlloyModel am = new AlloyModel(sig2type.values(), sets, rels.keySet(), ts);
      ans=new AlloyInstance(sol, sol.getOriginalFilename(), sol.getOriginalCommand(), am, atom2sets, rels, isMeta);
   }
View Full Code Here

TOP

Related Classes of edu.mit.csail.sdg.alloy4compiler.translator.A4Solution

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.