|
42 | 42 | import java.util.Map; |
43 | 43 | import java.util.Optional; |
44 | 44 | import java.util.Set; |
| 45 | +import org.sosy_lab.common.ShutdownManager; |
45 | 46 | import org.sosy_lab.common.ShutdownNotifier; |
46 | 47 | import org.sosy_lab.java_smt.api.BooleanFormula; |
47 | 48 | import org.sosy_lab.java_smt.api.Evaluator; |
|
51 | 52 | import org.sosy_lab.java_smt.basicimpl.AbstractProver; |
52 | 53 | import org.sosy_lab.java_smt.basicimpl.CachingModel; |
53 | 54 | import org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.AllSatModelCallback; |
| 55 | +import org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.TerminationCallback; |
54 | 56 |
|
55 | 57 | /** Common base class for {@link Mathsat5TheoremProver} and {@link Mathsat5InterpolatingProver}. */ |
56 | 58 | abstract class Mathsat5AbstractProver<T2> extends AbstractProver<T2> { |
57 | 59 |
|
58 | | - protected final Mathsat5SolverContext context; |
59 | 60 | protected final long curEnv; |
60 | 61 | private final long curConfig; |
61 | 62 | protected final Mathsat5FormulaCreator creator; |
62 | | - private final ShutdownNotifier shutdownNotifier; |
63 | 63 |
|
64 | 64 | protected Mathsat5AbstractProver( |
65 | 65 | Mathsat5SolverContext pContext, |
66 | 66 | Set<ProverOptions> pOptions, |
67 | 67 | Mathsat5FormulaCreator pCreator, |
68 | 68 | ShutdownNotifier pShutdownNotifier) { |
69 | | - super(pOptions); |
70 | | - context = pContext; |
| 69 | + super(pShutdownNotifier, pOptions); |
71 | 70 | creator = pCreator; |
72 | 71 | curConfig = buildConfig(pOptions); |
73 | | - curEnv = context.createEnvironment(curConfig); |
74 | | - shutdownNotifier = pShutdownNotifier; |
| 72 | + curEnv = pContext.createEnvironment(curConfig); |
75 | 73 | } |
76 | 74 |
|
77 | 75 | private long buildConfig(Set<ProverOptions> opts) { |
@@ -100,21 +98,33 @@ private long buildConfig(Set<ProverOptions> opts) { |
100 | 98 | public boolean isUnsat() throws InterruptedException, SolverException { |
101 | 99 | Preconditions.checkState(!closed); |
102 | 100 |
|
103 | | - final long hook = msat_set_termination_callback(curEnv, context.getTerminationTest()); |
| 101 | + final long hook = msat_set_termination_callback(curEnv, getTerminationTest()); |
104 | 102 | try { |
105 | 103 | return !msat_check_sat(curEnv); |
106 | 104 | } finally { |
107 | 105 | msat_free_termination_callback(hook); |
108 | 106 | } |
109 | 107 | } |
110 | 108 |
|
| 109 | + /** |
| 110 | + * Get a termination callback for the current prover (who's parent is the contexts callback). The |
| 111 | + * callback can be registered upfront, i.e., before calling a possibly expensive computation in |
| 112 | + * the solver to allow a proper shutdown. |
| 113 | + */ |
| 114 | + private TerminationCallback getTerminationTest() { |
| 115 | + return () -> { |
| 116 | + proverShutdownManager.getNotifier().shutdownIfNecessary(); |
| 117 | + return false; |
| 118 | + }; |
| 119 | + } |
| 120 | + |
111 | 121 | @Override |
112 | 122 | public boolean isUnsatWithAssumptions(Collection<BooleanFormula> pAssumptions) |
113 | 123 | throws SolverException, InterruptedException { |
114 | 124 | Preconditions.checkState(!closed); |
115 | 125 | checkForLiterals(pAssumptions); |
116 | 126 |
|
117 | | - final long hook = msat_set_termination_callback(curEnv, context.getTerminationTest()); |
| 127 | + final long hook = msat_set_termination_callback(curEnv, getTerminationTest()); |
118 | 128 | try { |
119 | 129 | return !msat_check_sat_with_assumptions(curEnv, getMsatTerm(pAssumptions)); |
120 | 130 | } finally { |
@@ -273,10 +283,15 @@ class MathsatAllSatCallback<T> implements AllSatModelCallback { |
273 | 283 |
|
274 | 284 | @Override |
275 | 285 | public void callback(long[] model) throws InterruptedException { |
276 | | - shutdownNotifier.shutdownIfNecessary(); |
| 286 | + proverShutdownManager.getNotifier().shutdownIfNecessary(); |
277 | 287 | clientCallback.apply( |
278 | 288 | Collections.unmodifiableList( |
279 | 289 | Lists.transform(Longs.asList(model), creator::encapsulateBoolean))); |
280 | 290 | } |
281 | 291 | } |
| 292 | + |
| 293 | + @Override |
| 294 | + protected ShutdownManager getShutdownManagerForProverImpl() throws UnsupportedOperationException { |
| 295 | + return proverShutdownManager; |
| 296 | + } |
282 | 297 | } |
0 commit comments