Guest User

Untitled

a guest
Jun 15th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import java.util.*;
  2. import com.microsoft.z3.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.BufferedReader;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.nio.charset.StandardCharsets;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14.  
  15.  
  16.  
  17.  
  18. class JavaExample1
  19. {
  20.      @SuppressWarnings("serial")
  21.         class TestFailedException extends Exception
  22.         {
  23.         public TestFailedException()
  24.         {
  25.             super("Check FAILED");
  26.         }
  27.         };
  28.    
  29.     Model check(Context ctx, BoolExpr f, Status sat) throws TestFailedException
  30.         {
  31.         Solver s = ctx.mkSolver();
  32.         s.add(f);
  33.         if (s.check() != sat)
  34.             throw new TestFailedException();
  35.         if (sat == Status.SATISFIABLE)
  36.             return s.getModel();
  37.         else
  38.             return null;
  39.         }
  40.  
  41.  //  Shows how to read an SMT2 file.
  42.  
  43. void smt2FileTest(String filename)
  44.     {
  45.         Date before = new Date();
  46.  
  47.         System.out.println("SMT2 File test ");
  48.         System.gc();
  49.  
  50.     System.out.println("Filename ="+filename);
  51.  
  52.  
  53.         {
  54.             HashMap<String, String> cfg = new HashMap<String, String>();
  55.             cfg.put("model", "true");
  56.             Context ctx = new Context(cfg);
  57.             BoolExpr a = ctx.mkAnd(ctx.parseSMTLIB2File(filename, null, null, null, null));
  58.  
  59.             long t_diff = ((new Date()).getTime() - before.getTime()) / 1000;
  60.  
  61.             System.out.println("SMT2 file read time: " + t_diff + " sec");
  62.  
  63.             // Iterate over the formula.
  64.  
  65.             LinkedList<Expr> q = new LinkedList<Expr>();
  66.             q.add(a);
  67.             int cnt = 0;
  68.             while (q.size() > 0)
  69.             {
  70.                 AST cur = (AST) q.removeFirst();
  71.                 cnt++;
  72.  
  73.                 if (cur.getClass() == Expr.class)
  74.                     if (!(cur.isVar()))
  75.                         for (Expr c : ((Expr) cur).getArgs())
  76.                             q.add(c);
  77.             }
  78.             System.out.println(cnt + " ASTs");
  79.         }
  80.  
  81.         long t_diff = ((new Date()).getTime() - before.getTime()) / 1000;
  82.         System.out.println("SMT2 file test took " + t_diff + " sec");
  83.     }
  84.  
  85. //  Demonstrates how to use the SMTLIB parser.
  86.  
  87.     public void parserExample1(Context ctx) throws TestFailedException
  88.     {
  89.         System.out.println("ParserExample1");
  90.         Log.append("ParserExample1");
  91.  
  92.         BoolExpr f = ctx.parseSMTLIB2String(
  93.                 "(declare-const x Int) (declare-const y Int) (assert (and (> x y) (> x 0)))",
  94.                 null, null, null, null)[0];
  95.         System.out.println("formula " + f);
  96.  
  97.         @SuppressWarnings("unused")
  98.         Model m = check(ctx, f, Status.SATISFIABLE);
  99.     }
  100.  
  101.     public void parserExample2(Context ctx) throws TestFailedException
  102.     {
  103.         System.out.println("ParserExample2");
  104.         Log.append("ParserExample2");
  105.  
  106.         Symbol[] declNames = { ctx.mkSymbol("a"), ctx.mkSymbol("b") };
  107.         FuncDecl a = ctx.mkConstDecl(declNames[0], ctx.mkIntSort());
  108.         FuncDecl b = ctx.mkConstDecl(declNames[1], ctx.mkIntSort());
  109.         FuncDecl[] decls = new FuncDecl[] { a, b };
  110.  
  111.         BoolExpr f = ctx.parseSMTLIB2String("(assert (> a b))", null, null,
  112.                 declNames, decls)[0];
  113.         System.out.println("formula: " + f);
  114.         check(ctx, f, Status.SATISFIABLE);
  115.     }
  116.  
  117.  
  118.     public static void main(String[] args)
  119.     {
  120.         JavaExample1 p = new JavaExample1();
  121.        
  122.        
  123.             com.microsoft.z3.Global.ToggleWarningMessages(true);
  124.             Log.open("test.log");
  125.  
  126.             HashMap<String, String> cfg = new HashMap<String, String>();
  127.                 cfg.put("model", "true");
  128.                 Context ctx = new Context(cfg);
  129.            
  130.             String contents="";
  131.             try
  132.             {
  133.              contents = new String(Files.readAllBytes(Paths.get("file.txt")));
  134.             }
  135.             catch(IOException e)
  136.             {
  137.             e.printStackTrace();
  138.             }
  139.  
  140.             System.out.println(contents);
  141.            
  142.             try
  143.             {
  144.                 p.smt2FileTest(contents);
  145.             }
  146.             catch(Exception e)
  147.             {
  148.                 e.printStackTrace();
  149.             }
  150.  
  151.  
  152.     }
  153.  
  154.  
  155. }
Add Comment
Please, Sign In to add comment