Skip to content

Commit ecc8a9c

Browse files
authored
Merge pull request #407 from sosy-lab/406-exception-when-z3-is-interrupted-during-modelevaluate
Add InterruptedExceptions to Model Generation
2 parents 2f26a45 + b675770 commit ecc8a9c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+354
-239
lines changed

src/org/sosy_lab/java_smt/api/BasicProverEnvironment.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface BasicProverEnvironment<T> extends AutoCloseable {
3232
*/
3333
@Nullable
3434
@CanIgnoreReturnValue
35-
default T push(BooleanFormula f) throws InterruptedException {
35+
default T push(BooleanFormula f) throws InterruptedException, SolverException {
3636
push();
3737
return addConstraint(f);
3838
}
@@ -46,7 +46,7 @@ default T push(BooleanFormula f) throws InterruptedException {
4646
/** Add a constraint to the latest backtracking point. */
4747
@Nullable
4848
@CanIgnoreReturnValue
49-
T addConstraint(BooleanFormula constraint) throws InterruptedException;
49+
T addConstraint(BooleanFormula constraint) throws InterruptedException, SolverException;
5050

5151
/**
5252
* Create a new backtracking point, i.e., a new level on the assertion stack. Each level can hold
@@ -55,7 +55,7 @@ default T push(BooleanFormula f) throws InterruptedException {
5555
* <p>If formulas are added before creating the first backtracking point, they can not be removed
5656
* via a POP-operation.
5757
*/
58-
void push() throws InterruptedException;
58+
void push() throws InterruptedException, SolverException;
5959

6060
/**
6161
* Get the number of backtracking points/levels on the current stack.
@@ -87,15 +87,15 @@ boolean isUnsatWithAssumptions(Collection<BooleanFormula> assumptions)
8787
* <p>A model might contain additional symbols with their evaluation, if a solver uses its own
8888
* temporary symbols. There should be at least a value-assignment for each free symbol.
8989
*/
90-
Model getModel() throws SolverException;
90+
Model getModel() throws SolverException, InterruptedException;
9191

9292
/**
9393
* Get a temporary view on the current satisfying assignment. This should be called only
9494
* immediately after an {@link #isUnsat()} call that returned <code>false</code>. The evaluator
9595
* should no longer be used as soon as any constraints are added to, pushed, or popped from the
9696
* prover stack.
9797
*/
98-
default Evaluator getEvaluator() throws SolverException {
98+
default Evaluator getEvaluator() throws InterruptedException, SolverException {
9999
return getModel();
100100
}
101101

@@ -107,7 +107,8 @@ default Evaluator getEvaluator() throws SolverException {
107107
* <p>Note that if you need to iterate multiple times over the model it may be more efficient to
108108
* use this method instead of {@link #getModel()} (depending on the solver).
109109
*/
110-
default ImmutableList<Model.ValueAssignment> getModelAssignments() throws SolverException {
110+
default ImmutableList<Model.ValueAssignment> getModelAssignments()
111+
throws SolverException, InterruptedException {
111112
try (Model model = getModel()) {
112113
return model.asList();
113114
}

src/org/sosy_lab/java_smt/api/Evaluator.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public interface Evaluator extends AutoCloseable {
4747
* @return evaluation of the given formula or <code>null</code> if the solver does not provide a
4848
* better evaluation.
4949
*/
50-
@Nullable <T extends Formula> T eval(T formula);
50+
@Nullable <T extends Formula> T eval(T formula) throws InterruptedException, SolverException;
5151

5252
/**
5353
* Evaluate a given formula substituting the values from the model.
@@ -62,56 +62,60 @@ public interface Evaluator extends AutoCloseable {
6262
* @return Either of: - Number (Rational/Double/BigInteger/Long/Integer) - Boolean
6363
* @throws IllegalArgumentException if a formula has unexpected type, e.g. Array.
6464
*/
65-
@Nullable Object evaluate(Formula formula);
65+
@Nullable Object evaluate(Formula formula) throws InterruptedException, SolverException;
6666

6767
/**
6868
* Type-safe evaluation for integer formulas.
6969
*
7070
* <p>The formula does not need to be a variable, we also allow complex expression.
7171
*/
72-
@Nullable BigInteger evaluate(IntegerFormula formula);
72+
@Nullable BigInteger evaluate(IntegerFormula formula)
73+
throws InterruptedException, SolverException;
7374

7475
/**
7576
* Type-safe evaluation for rational formulas.
7677
*
7778
* <p>The formula does not need to be a variable, we also allow complex expression.
7879
*/
79-
@Nullable Rational evaluate(RationalFormula formula);
80+
@Nullable Rational evaluate(RationalFormula formula) throws InterruptedException, SolverException;
8081

8182
/**
8283
* Type-safe evaluation for boolean formulas.
8384
*
8485
* <p>The formula does not need to be a variable, we also allow complex expression.
8586
*/
86-
@Nullable Boolean evaluate(BooleanFormula formula);
87+
@Nullable Boolean evaluate(BooleanFormula formula) throws InterruptedException, SolverException;
8788

8889
/**
8990
* Type-safe evaluation for bitvector formulas.
9091
*
9192
* <p>The formula does not need to be a variable, we also allow complex expression.
9293
*/
93-
@Nullable BigInteger evaluate(BitvectorFormula formula);
94+
@Nullable BigInteger evaluate(BitvectorFormula formula)
95+
throws InterruptedException, SolverException;
9496

9597
/**
9698
* Type-safe evaluation for string formulas.
9799
*
98100
* <p>The formula does not need to be a variable, we also allow complex expression.
99101
*/
100-
@Nullable String evaluate(StringFormula formula);
102+
@Nullable String evaluate(StringFormula formula) throws InterruptedException, SolverException;
101103

102104
/**
103105
* Type-safe evaluation for enumeration formulas.
104106
*
105107
* <p>The formula does not need to be a variable, we also allow complex expression.
106108
*/
107-
@Nullable String evaluate(EnumerationFormula formula);
109+
@Nullable String evaluate(EnumerationFormula formula)
110+
throws InterruptedException, SolverException;
108111

109112
/**
110113
* Type-safe evaluation for floating-point formulas.
111114
*
112115
* <p>The formula does not need to be a variable, we also allow complex expression.
113116
*/
114-
@Nullable FloatingPointNumber evaluate(FloatingPointFormula formula);
117+
@Nullable FloatingPointNumber evaluate(FloatingPointFormula formula)
118+
throws InterruptedException, SolverException;
115119

116120
/**
117121
* Free resources associated with this evaluator (existing {@link Formula} instances stay valid,

src/org/sosy_lab/java_smt/api/FormulaManager.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ <T extends Formula> T makeApplication(
162162
* Apply a tactic which performs formula transformation. The available tactics depend on the used
163163
* solver.
164164
*/
165-
BooleanFormula applyTactic(BooleanFormula input, Tactic tactic) throws InterruptedException;
165+
BooleanFormula applyTactic(BooleanFormula input, Tactic tactic)
166+
throws InterruptedException, SolverException;
166167

167168
/**
168169
* Simplify an input formula, while ensuring equivalence.
@@ -172,7 +173,7 @@ <T extends Formula> T makeApplication(
172173
* @param input The input formula
173174
* @return Simplified version of the formula
174175
*/
175-
<T extends Formula> T simplify(T input) throws InterruptedException;
176+
<T extends Formula> T simplify(T input) throws InterruptedException, SolverException;
176177

177178
/**
178179
* Visit the formula with a given visitor.

src/org/sosy_lab/java_smt/api/Model.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ public interface Model extends Evaluator, Iterable<ValueAssignment>, AutoCloseab
5252
*/
5353
@Override
5454
default Iterator<ValueAssignment> iterator() {
55-
return asList().iterator();
55+
try {
56+
return asList().iterator();
57+
} catch (InterruptedException | SolverException e) {
58+
throw new RuntimeException(e);
59+
}
5660
}
5761

5862
/** Build a list of assignments that stays valid after closing the model. */
59-
ImmutableList<ValueAssignment> asList();
63+
ImmutableList<ValueAssignment> asList() throws InterruptedException, SolverException;
6064

6165
/**
6266
* Pretty-printing of the model values.

src/org/sosy_lab/java_smt/api/OptimizationProverEnvironment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public interface OptimizationProverEnvironment extends BasicProverEnvironment<Vo
6868
* (epsilon is irrelevant here and can be zero). The model returns the optimal assignment x=9.
6969
*/
7070
@Override
71-
Model getModel() throws SolverException;
71+
Model getModel() throws SolverException, InterruptedException;
7272

7373
/** Status of the optimization problem. */
7474
enum OptStatus {

src/org/sosy_lab/java_smt/basicimpl/AbstractEvaluator.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.sosy_lab.java_smt.api.Formula;
2323
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
2424
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
25+
import org.sosy_lab.java_smt.api.SolverException;
2526
import org.sosy_lab.java_smt.api.StringFormula;
2627

2728
@SuppressWarnings("ClassTypeParameterName")
@@ -40,22 +41,22 @@ protected AbstractEvaluator(
4041
@SuppressWarnings("unchecked")
4142
@Nullable
4243
@Override
43-
public final <T extends Formula> T eval(T f) {
44+
public final <T extends Formula> T eval(T f) throws InterruptedException, SolverException {
4445
Preconditions.checkState(!isClosed());
4546
TFormulaInfo evaluation = evalImpl(creator.extractInfo(f));
4647
return evaluation == null ? null : (T) creator.encapsulateWithTypeOf(evaluation);
4748
}
4849

4950
@Nullable
5051
@Override
51-
public final BigInteger evaluate(IntegerFormula f) {
52+
public final BigInteger evaluate(IntegerFormula f) throws InterruptedException, SolverException {
5253
Preconditions.checkState(!isClosed());
5354
return (BigInteger) evaluateImpl(creator.extractInfo(f));
5455
}
5556

5657
@Nullable
5758
@Override
58-
public Rational evaluate(RationalFormula f) {
59+
public Rational evaluate(RationalFormula f) throws InterruptedException, SolverException {
5960
Object value = evaluateImpl(creator.extractInfo(f));
6061
if (value instanceof BigInteger) {
6162
// We simplified the value internally. Here, we need to convert it back to Rational.
@@ -67,41 +68,43 @@ public Rational evaluate(RationalFormula f) {
6768

6869
@Nullable
6970
@Override
70-
public final Boolean evaluate(BooleanFormula f) {
71+
public final Boolean evaluate(BooleanFormula f) throws InterruptedException, SolverException {
7172
Preconditions.checkState(!isClosed());
7273
return (Boolean) evaluateImpl(creator.extractInfo(f));
7374
}
7475

7576
@Nullable
7677
@Override
77-
public final String evaluate(StringFormula f) {
78+
public final String evaluate(StringFormula f) throws InterruptedException, SolverException {
7879
Preconditions.checkState(!isClosed());
7980
return (String) evaluateImpl(creator.extractInfo(f));
8081
}
8182

8283
@Nullable
8384
@Override
84-
public final String evaluate(EnumerationFormula f) {
85+
public final String evaluate(EnumerationFormula f) throws InterruptedException, SolverException {
8586
Preconditions.checkState(!isClosed());
8687
return (String) evaluateImpl(creator.extractInfo(f));
8788
}
8889

8990
@Override
90-
public final @Nullable FloatingPointNumber evaluate(FloatingPointFormula f) {
91+
public final @Nullable FloatingPointNumber evaluate(FloatingPointFormula f)
92+
throws InterruptedException, SolverException {
9193
Preconditions.checkState(!isClosed());
9294
return (FloatingPointNumber) evaluateImpl(creator.extractInfo(f));
9395
}
9496

9597
@Nullable
9698
@Override
97-
public final BigInteger evaluate(BitvectorFormula f) {
99+
public final BigInteger evaluate(BitvectorFormula f)
100+
throws InterruptedException, SolverException {
98101
Preconditions.checkState(!isClosed());
99102
return (BigInteger) evaluateImpl(creator.extractInfo(f));
100103
}
101104

102105
@Nullable
103106
@Override
104-
public final Object evaluate(Formula f) {
107+
public final Object evaluate(Formula f) throws InterruptedException, SolverException {
105108
Preconditions.checkState(!isClosed());
106109
Preconditions.checkArgument(
107110
!(f instanceof ArrayFormula),
@@ -114,15 +117,16 @@ public final Object evaluate(Formula f) {
114117
* set in the model and evaluation aborts, return <code>null</code>.
115118
*/
116119
@Nullable
117-
protected abstract TFormulaInfo evalImpl(TFormulaInfo formula);
120+
protected abstract TFormulaInfo evalImpl(TFormulaInfo formula)
121+
throws InterruptedException, SolverException;
118122

119123
/**
120124
* Simplify the given formula and replace all symbols with their model values. If a symbol is not
121125
* set in the model and evaluation aborts, return <code>null</code>. Afterwards convert the
122126
* formula into a Java object as far as possible, i.e., try to match a primitive or simple type.
123127
*/
124128
@Nullable
125-
protected final Object evaluateImpl(TFormulaInfo f) {
129+
protected final Object evaluateImpl(TFormulaInfo f) throws InterruptedException, SolverException {
126130
Preconditions.checkState(!isClosed());
127131
TFormulaInfo evaluatedF = evalImpl(f);
128132
return evaluatedF == null ? null : creator.convertValue(f, evaluatedF);

src/org/sosy_lab/java_smt/basicimpl/AbstractFormulaManager.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.sosy_lab.java_smt.api.IntegerFormulaManager;
4141
import org.sosy_lab.java_smt.api.RationalFormulaManager;
4242
import org.sosy_lab.java_smt.api.SLFormulaManager;
43+
import org.sosy_lab.java_smt.api.SolverException;
4344
import org.sosy_lab.java_smt.api.StringFormulaManager;
4445
import org.sosy_lab.java_smt.api.Tactic;
4546
import org.sosy_lab.java_smt.api.visitors.FormulaTransformationVisitor;
@@ -358,7 +359,8 @@ public final TFormulaInfo extractInfo(Formula f) {
358359
}
359360

360361
@Override
361-
public BooleanFormula applyTactic(BooleanFormula f, Tactic tactic) throws InterruptedException {
362+
public BooleanFormula applyTactic(BooleanFormula f, Tactic tactic)
363+
throws InterruptedException, SolverException {
362364
switch (tactic) {
363365
case ACKERMANNIZATION:
364366
return applyUFEImpl(f);
@@ -373,12 +375,8 @@ public BooleanFormula applyTactic(BooleanFormula f, Tactic tactic) throws Interr
373375
}
374376
}
375377

376-
/**
377-
* Eliminate UFs from the given input formula.
378-
*
379-
* @throws InterruptedException Can be thrown by the native code.
380-
*/
381-
protected BooleanFormula applyUFEImpl(BooleanFormula pF) throws InterruptedException {
378+
/** Eliminate UFs from the given input formula. */
379+
protected BooleanFormula applyUFEImpl(BooleanFormula pF) {
382380
return SolverUtils.ufElimination(this).eliminateUfs(pF);
383381
}
384382

@@ -388,8 +386,10 @@ protected BooleanFormula applyUFEImpl(BooleanFormula pF) throws InterruptedExcep
388386
* <p>This is the light version that does not need to eliminate all quantifiers.
389387
*
390388
* @throws InterruptedException Can be thrown by the native code.
389+
* @throws SolverException Can be thrown by the native code.
391390
*/
392-
protected BooleanFormula applyQELightImpl(BooleanFormula pF) throws InterruptedException {
391+
protected BooleanFormula applyQELightImpl(BooleanFormula pF)
392+
throws InterruptedException, SolverException {
393393

394394
// Returning the untouched formula is valid according to QE_LIGHT contract.
395395
// TODO: substitution-based implementation.
@@ -401,8 +401,10 @@ protected BooleanFormula applyQELightImpl(BooleanFormula pF) throws InterruptedE
401401
*
402402
* @param pF Input to apply the CNF transformation to.
403403
* @throws InterruptedException Can be thrown by the native code.
404+
* @throws SolverException Can be thrown by the native code.
404405
*/
405-
protected BooleanFormula applyCNFImpl(BooleanFormula pF) throws InterruptedException {
406+
protected BooleanFormula applyCNFImpl(BooleanFormula pF)
407+
throws InterruptedException, SolverException {
406408

407409
// TODO: generic implementation.
408410
throw new UnsupportedOperationException(
@@ -413,13 +415,15 @@ protected BooleanFormula applyCNFImpl(BooleanFormula pF) throws InterruptedExcep
413415
* Apply negation normal form (NNF) transformation to the given input formula.
414416
*
415417
* @throws InterruptedException Can be thrown by the native code.
418+
* @throws SolverException Can be thrown by the native code.
416419
*/
417-
protected BooleanFormula applyNNFImpl(BooleanFormula input) throws InterruptedException {
420+
protected BooleanFormula applyNNFImpl(BooleanFormula input)
421+
throws InterruptedException, SolverException {
418422
return getBooleanFormulaManager().transformRecursively(input, new NNFVisitor(this));
419423
}
420424

421425
@Override
422-
public <T extends Formula> T simplify(T f) throws InterruptedException {
426+
public <T extends Formula> T simplify(T f) throws InterruptedException, SolverException {
423427
return formulaCreator.encapsulate(formulaCreator.getFormulaType(f), simplify(extractInfo(f)));
424428
}
425429

@@ -429,8 +433,9 @@ public <T extends Formula> T simplify(T f) throws InterruptedException {
429433
* <p>This does not need to change something, but it might simplify the formula.
430434
*
431435
* @throws InterruptedException Can be thrown by the native code.
436+
* @throws SolverException Can be thrown by the native code.
432437
*/
433-
protected TFormulaInfo simplify(TFormulaInfo f) throws InterruptedException {
438+
protected TFormulaInfo simplify(TFormulaInfo f) throws InterruptedException, SolverException {
434439
return f;
435440
}
436441

0 commit comments

Comments
 (0)