Z3() { expressionCache = new HashMap<Integer, BoolExpr>(); try { context = new Context(); satTactic = context.ParAndThen(context.MkTactic("sat-preprocess"), context.MkTactic("sat")); solver = context.MkSolver(satTactic); } catch (Z3Exception e) { throw new RuntimeException(e); } this.vars = 0; this.clauses = 0; this.last_status = null; this.last_model = null; checkpoints = new Stack<Checkpoint>(); }
/** * {@inheritDoc} * * @see kodkod.engine.satlab.SATSolver#free() */ public final synchronized void free() { checkpoints = null; solver = null; context.Dispose(); context = null; expressionCache = null; }
/** * {@inheritDoc} * * @see kodkod.engine.satlab.SATSolver#addClause(int[]) */ public boolean addClause(int[] lits) { if (lits == null) { throw new NullPointerException(); } try { BoolExpr[] literals = new BoolExpr[lits.length]; for (int i = 0; i < lits.length; i += 1) { int lit = Math.abs(lits[i]); if (lit == 0 || lit > vars) { throw new IllegalArgumentException("Illegal variable: " + lit); } literals[i] = getExpressionForLiteral(lits[i]); } BoolExpr clause = context.MkOr(literals); solver.Assert(clause); clauses += 1; } catch (Z3Exception e) { throw new RuntimeException(e); } return true; }
private BoolExpr getExpressionForLiteral(int literal) throws Z3Exception { if (literal == 0) { throw new IllegalArgumentException("Parameter must be nonzero."); } BoolExpr expr = expressionCache.get(literal); if (expr == null) { if (literal > 0) { Symbol sym = context.MkSymbol(literal); expr = context.MkBoolConst(sym); } else { expr = context.MkNot(getExpressionForLiteral(-literal)); } expressionCache.put(literal, expr); } return expr; }
private boolean checkQueryWithLibrary(String query, int timeout) { boolean result = false; try { com.microsoft.z3.Context context = new com.microsoft.z3.Context(); Solver solver = context.mkSolver(); Params params = context.mkParams(); params.add("timeout", timeout); solver.setParameters(params); solver.add(context.parseSMTLIB2String(SMT_PRELUDE + query, null, null, null, null)); result = solver.check() == Status.UNSATISFIABLE; context.dispose(); } catch (Z3Exception e) { kem.registerCriticalWarning("failed to translate smtlib expression:\n" + SMT_PRELUDE + query); } catch (UnsatisfiedLinkError e) { System.err.println(System.getProperty("java.library.path")); throw e; } return result; }