Z3
 
Loading...
Searching...
No Matches
Solver.java
Go to the documentation of this file.
1
19package com.microsoft.z3;
20
21import com.microsoft.z3.enumerations.Z3_lbool;
22
23import java.lang.ref.ReferenceQueue;
24import java.util.*;
25
29@SuppressWarnings("unchecked")
30public class Solver extends Z3Object {
34 public String getHelp()
35 {
36 return Native.solverGetHelp(getContext().nCtx(), getNativeObject());
37 }
38
44 public void setParameters(Params value)
45 {
46 getContext().checkContextMatch(value);
47 Native.solverSetParams(getContext().nCtx(), getNativeObject(),
48 value.getNativeObject());
49 }
50
57 {
58 return new ParamDescrs(getContext(), Native.solverGetParamDescrs(
59 getContext().nCtx(), getNativeObject()));
60 }
61
67 public int getNumScopes()
68 {
69 return Native
70 .solverGetNumScopes(getContext().nCtx(), getNativeObject());
71 }
72
77 public void push()
78 {
79 Native.solverPush(getContext().nCtx(), getNativeObject());
80 }
81
86 public void pop()
87 {
88 pop(1);
89 }
90
98 public void pop(int n)
99 {
100 Native.solverPop(getContext().nCtx(), getNativeObject(), n);
101 }
102
108 public void reset()
109 {
110 Native.solverReset(getContext().nCtx(), getNativeObject());
111 }
112
118 public void interrupt()
119 {
120 Native.solverInterrupt(getContext().nCtx(), getNativeObject());
121 }
122
128 public void add(Expr<BoolSort>... constraints)
129 {
130 getContext().checkContextMatch(constraints);
131 for (Expr<BoolSort> a : constraints)
132 {
133 Native.solverAssert(getContext().nCtx(), getNativeObject(),
134 a.getNativeObject());
135 }
136 }
137
138
153 public void assertAndTrack(Expr<BoolSort>[] constraints, Expr<BoolSort>[] ps)
154 {
155 getContext().checkContextMatch(constraints);
156 getContext().checkContextMatch(ps);
157 if (constraints.length != ps.length) {
158 throw new Z3Exception("Argument size mismatch");
159 }
160
161 for (int i = 0; i < constraints.length; i++) {
162 Native.solverAssertAndTrack(getContext().nCtx(), getNativeObject(),
163 constraints[i].getNativeObject(), ps[i].getNativeObject());
164 }
165 }
166
181 {
182 getContext().checkContextMatch(constraint);
183 getContext().checkContextMatch(p);
184
185 Native.solverAssertAndTrack(getContext().nCtx(), getNativeObject(),
186 constraint.getNativeObject(), p.getNativeObject());
187 }
188
192 public void fromFile(String file)
193 {
194 Native.solverFromFile(getContext().nCtx(), getNativeObject(), file);
195 }
196
200 public void fromString(String str)
201 {
202 Native.solverFromString(getContext().nCtx(), getNativeObject(), str);
203 }
204
205
211 public int getNumAssertions()
212 {
213 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
214 return assrts.size();
215 }
216
223 {
224 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
225 return assrts.ToBoolExprArray();
226 }
227
235 @SafeVarargs
236 public final Status check(Expr<BoolSort>... assumptions)
237 {
238 Z3_lbool r;
239 if (assumptions == null) {
240 r = Z3_lbool.fromInt(Native.solverCheck(getContext().nCtx(),
241 getNativeObject()));
242 } else {
243 r = Z3_lbool.fromInt(Native.solverCheckAssumptions(getContext()
244 .nCtx(), getNativeObject(), assumptions.length, AST
245 .arrayToNative(assumptions)));
246 }
247 return lboolToStatus(r);
248 }
249
257 @SuppressWarnings("rawtypes")
258 public Status check()
259 {
260 return check((Expr[]) null);
261 }
262
274 public Status getConsequences(Expr<BoolSort>[] assumptions, Expr<?>[] variables, List<Expr<BoolSort>> consequences)
275 {
276 ASTVector result = new ASTVector(getContext());
277 ASTVector asms = new ASTVector(getContext());
278 ASTVector vars = new ASTVector(getContext());
279 for (Expr<BoolSort> asm : assumptions) asms.push(asm);
280 for (Expr<?> v : variables) vars.push(v);
281 int r = Native.solverGetConsequences(getContext().nCtx(), getNativeObject(), asms.getNativeObject(), vars.getNativeObject(), result.getNativeObject());
282 for (int i = 0; i < result.size(); ++i) consequences.add((BoolExpr) Expr.create(getContext(), result.get(i).getNativeObject()));
283 return lboolToStatus(Z3_lbool.fromInt(r));
284 }
285
286
297 {
298 long x = Native.solverGetModel(getContext().nCtx(), getNativeObject());
299 if (x == 0) {
300 return null;
301 } else {
302 return new Model(getContext(), x);
303 }
304 }
305
316 {
317 long x = Native.solverGetProof(getContext().nCtx(), getNativeObject());
318 if (x == 0) {
319 return null;
320 } else {
321 return Expr.create(getContext(), x);
322 }
323 }
324
335 {
336
337 ASTVector core = new ASTVector(getContext(), Native.solverGetUnsatCore(getContext().nCtx(), getNativeObject()));
338 return core.ToBoolExprArray();
339 }
340
350 {
351 ASTVector units = new ASTVector(getContext(), Native.solverGetUnits(getContext().nCtx(), getNativeObject()));
352 return units.ToBoolExprArray();
353 }
354
363 {
364 ASTVector nonUnits = new ASTVector(getContext(), Native.solverGetNonUnits(getContext().nCtx(), getNativeObject()));
365 return nonUnits.ToBoolExprArray();
366 }
367
378 {
379 ASTVector trail = new ASTVector(getContext(), Native.solverGetTrail(getContext().nCtx(), getNativeObject()));
380 return trail.ToBoolExprArray();
381 }
382
387 public String getReasonUnknown()
388 {
389 return Native.solverGetReasonUnknown(getContext().nCtx(),
390 getNativeObject());
391 }
392
402 public int[] getTrailLevels()
403 {
404 ASTVector trailVector = new ASTVector(getContext(), Native.solverGetTrail(getContext().nCtx(), getNativeObject()));
405 int[] levels = new int[trailVector.size()];
406 Native.solverGetLevels(getContext().nCtx(), getNativeObject(), trailVector.getNativeObject(), trailVector.size(), levels);
407 return levels;
408 }
409
422 public java.util.Iterator<BoolExpr[]> cube(Expr<?>[] vars, int cutoff)
423 {
424 ASTVector cubeVars = new ASTVector(getContext());
425 if (vars != null) {
426 for (Expr<?> v : vars) {
427 cubeVars.push(v);
428 }
429 }
430
431 return new java.util.Iterator<BoolExpr[]>() {
432 private BoolExpr[] nextCube = computeNext();
433
434 private BoolExpr[] computeNext() {
435 ASTVector result = new ASTVector(getContext(),
436 Native.solverCube(getContext().nCtx(), getNativeObject(),
437 cubeVars.getNativeObject(), cutoff));
438 BoolExpr[] cube = result.ToBoolExprArray();
439
440 // Check for termination conditions
441 if (cube.length == 1 && cube[0].isFalse()) {
442 return null; // No more cubes
443 }
444 if (cube.length == 0) {
445 return null; // Search space exhausted
446 }
447 return cube;
448 }
449
450 @Override
451 public boolean hasNext() {
452 return nextCube != null;
453 }
454
455 @Override
456 public BoolExpr[] next() {
457 if (nextCube == null) {
458 throw new java.util.NoSuchElementException();
459 }
460 BoolExpr[] current = nextCube;
461 nextCube = computeNext();
462 return current;
463 }
464 };
465 }
466
475 public void setInitialValue(Expr<?> var, Expr<?> value)
476 {
477 getContext().checkContextMatch(var);
478 getContext().checkContextMatch(value);
479 Native.solverSetInitialValue(getContext().nCtx(), getNativeObject(),
480 var.getNativeObject(), value.getNativeObject());
481 }
482
487 {
488 return new Solver(ctx, Native.solverTranslate(getContext().nCtx(), getNativeObject(), ctx.nCtx()));
489 }
490
497 {
498 return new Statistics(getContext(), Native.solverGetStatistics(
499 getContext().nCtx(), getNativeObject()));
500 }
501
505 @Override
506 public String toString()
507 {
508 return Native
509 .solverToString(getContext().nCtx(), getNativeObject());
510 }
511
515 private Status lboolToStatus(Z3_lbool r)
516 {
517 switch (r)
518 {
519 case Z3_L_TRUE:
520 return Status.SATISFIABLE;
521 case Z3_L_FALSE:
522 return Status.UNSATISFIABLE;
523 default:
524 return Status.UNKNOWN;
525 }
526 }
527
528 Solver(Context ctx, long obj)
529 {
530 super(ctx, obj);
531 }
532
533 @Override
534 void incRef() {
535 Native.solverIncRef(getContext().nCtx(), getNativeObject());
536 }
537
538 @Override
539 void addToReferenceQueue() {
540 getContext().getReferenceQueue().storeReference(this, SolverRef::new);
541 }
542
543 private static class SolverRef extends Z3ReferenceQueue.Reference<Solver> {
544
545 private SolverRef(Solver referent, ReferenceQueue<Z3Object> q) {
546 super(referent, q);
547 }
548
549 @Override
550 void decRef(Context ctx, long z3Obj) {
551 Native.solverDecRef(ctx.nCtx(), z3Obj);
552 }
553 }
554}
boolean isFalse()
Definition Expr.java:336
void setInitialValue(Expr<?> var, Expr<?> value)
Definition Solver.java:475
Solver translate(Context ctx)
Definition Solver.java:486
Status getConsequences(Expr< BoolSort >[] assumptions, Expr<?>[] variables, List< Expr< BoolSort > > consequences)
Definition Solver.java:274
ParamDescrs getParameterDescriptions()
Definition Solver.java:56
final Status check(Expr< BoolSort >... assumptions)
Definition Solver.java:236
void assertAndTrack(Expr< BoolSort > constraint, Expr< BoolSort > p)
Definition Solver.java:180
void setParameters(Params value)
Definition Solver.java:44
BoolExpr[] getUnsatCore()
Definition Solver.java:334
BoolExpr[] getUnits()
Definition Solver.java:349
java.util.Iterator< BoolExpr[]> cube(Expr<?>[] vars, int cutoff)
Definition Solver.java:422
BoolExpr[] getAssertions()
Definition Solver.java:222
void fromString(String str)
Load solver assertions from a string.
Definition Solver.java:200
BoolExpr[] getNonUnits()
Definition Solver.java:362
void add(Expr< BoolSort >... constraints)
Definition Solver.java:128
BoolExpr[] getTrail()
Definition Solver.java:377
void fromFile(String file)
Load solver assertions from a file.
Definition Solver.java:192
void assertAndTrack(Expr< BoolSort >[] constraints, Expr< BoolSort >[] ps)
Definition Solver.java:153
Statistics getStatistics()
Definition Solver.java:496
static long[] arrayToNative(Z3Object[] a)
Definition Z3Object.java:73
static Status fromInt(int v)
Definition Status.java:41
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
@ Z3_L_TRUE
Definition z3_api.h:61
@ Z3_L_FALSE
Definition z3_api.h:59