-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathAbstractSolverContext.java
More file actions
208 lines (185 loc) · 8.08 KB
/
AbstractSolverContext.java
File metadata and controls
208 lines (185 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2020 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.basicimpl;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.sosy_lab.java_smt.api.FormulaManager;
import org.sosy_lab.java_smt.api.InterpolatingProverEnvironment;
import org.sosy_lab.java_smt.api.OptimizationProverEnvironment;
import org.sosy_lab.java_smt.api.ProverEnvironment;
import org.sosy_lab.java_smt.api.SolverContext;
import org.sosy_lab.java_smt.basicimpl.withAssumptionsWrapper.InterpolatingProverWithAssumptionsWrapper;
import org.sosy_lab.java_smt.basicimpl.withAssumptionsWrapper.ProverWithAssumptionsWrapper;
public abstract class AbstractSolverContext implements SolverContext {
private final FormulaManager fmgr;
protected AbstractSolverContext(FormulaManager fmgr) {
this.fmgr = fmgr;
}
@Override
public final FormulaManager getFormulaManager() {
return fmgr;
}
@SuppressWarnings("resource")
@Override
public final ProverEnvironment newProverEnvironment(ProverOptions... options) {
ProverEnvironment out = newProverEnvironment0(toSet(options));
if (!supportsAssumptionSolving()) {
// In the case we do not already have a prover environment with assumptions,
// we add a wrapper to it
out = new ProverWithAssumptionsWrapper(out);
}
return out;
}
protected abstract ProverEnvironment newProverEnvironment0(Set<ProverOptions> options);
@SuppressWarnings("resource")
@Override
public final InterpolatingProverEnvironment<?> newProverEnvironmentWithInterpolation(
ProverOptions... options) {
// TODO: unify InterpolatingProverDelegate with IndependentInterpolatingSolverDelegate and
// InterpolatingSolverDelegate!
InterpolatingProverEnvironment<?> out =
new InterpolatingProverDelegate<>(newProverEnvironmentWithInterpolation1(toSet(options)));
if (!supportsAssumptionSolving()) {
// In the case we do not already have a prover environment with assumptions,
// we add a wrapper to it
out = new InterpolatingProverWithAssumptionsWrapper<>(out, fmgr);
}
return out;
}
@SuppressWarnings({"ResultOfMethodCallIgnored", "resource"})
private InterpolatingProverEnvironment<?> newProverEnvironmentWithInterpolation1(
Set<ProverOptions> options) {
InterpolatingProverEnvironment<?> out;
// Try to get a new prover environment w native interpolation with the current options
try {
out = newProverEnvironmentWithInterpolation0(options);
} catch (UnsupportedOperationException e) {
// Check if QuantifiedFormulaManager is available before attempting independent interpolation
try {
getFormulaManager().getQuantifiedFormulaManager();
} catch (UnsupportedOperationException error) {
e.addSuppressed(error);
throw e;
}
// If native interpolation is not available, we wrap a normal prover such that it returns
// interpolation points
ProverEnvironment normalProver = newProverEnvironment0(options);
// TODO: only allow this if there is a quantified formula manager available!
out = new InterpolatingSolverDelegate(normalProver, options);
}
// TODO: do we need the assumptions inside of the interpolation delegate?
return new IndependentInterpolatingSolverDelegate<>(this, out, options);
}
protected abstract InterpolatingProverEnvironment<?> newProverEnvironmentWithInterpolation0(
Set<ProverOptions> pSet);
@SuppressWarnings("resource")
@Override
public final OptimizationProverEnvironment newOptimizationProverEnvironment(
ProverOptions... options) {
return newOptimizationProverEnvironment0(toSet(options));
}
protected abstract OptimizationProverEnvironment newOptimizationProverEnvironment0(
Set<ProverOptions> pSet);
/**
* Whether the solver supports solving under some given assumptions (with all corresponding
* features) by itself, i.e., whether {@link
* ProverEnvironment#isUnsatWithAssumptions(java.util.Collection)} and {@link
* InterpolatingProverEnvironment#isUnsatWithAssumptions(java.util.Collection)} are fully
* implemented.
*
* <p>Otherwise, i.e., if this method returns {@code false}, the solver does not need to support
* this feature and may simply {@code throw UnsupportedOperationException} in the respective
* methods. This class will wrap the prover environments and provide an implementation of the
* feature.
*
* <p>This method is expected to always return the same value. Otherwise, the behavior of this
* class is undefined.
*/
protected abstract boolean supportsAssumptionSolving();
private static final Set<ProverOptions> ALL_INDEPENDENT_INTERPOLATION_STRATEGIES =
ImmutableSet.of(
ProverOptions.GENERATE_PROJECTION_BASED_INTERPOLANTS,
ProverOptions.GENERATE_UNIFORM_BACKWARD_INTERPOLANTS,
ProverOptions.GENERATE_UNIFORM_FORWARD_INTERPOLANTS);
protected boolean useNativeInterpolation(Set<ProverOptions> options) {
return getIndependentInterpolationStrategy(options) == null;
}
@SuppressWarnings("CheckReturnValue")
protected @Nullable ProverOptions getIndependentInterpolationStrategy(
Set<ProverOptions> options) {
List<ProverOptions> itpStrat = new ArrayList<>(options);
itpStrat.retainAll(ALL_INDEPENDENT_INTERPOLATION_STRATEGIES);
if (itpStrat.isEmpty()) {
return null;
} else if (itpStrat.size() != 1) {
throw new IllegalArgumentException(
"Only a single independent interpolation strategy can be"
+ " chosen for a prover, but chosen were: "
+ Joiner.on(", ").join(options));
}
ProverOptions interpolationOption = itpStrat.get(0);
try {
fmgr.getQuantifiedFormulaManager();
} catch (UnsupportedOperationException e) {
throw new UnsupportedOperationException(
"Solver does not support independent interpolation based on the current strategy, as"
+ " it is lacking quantifier support.");
}
return interpolationOption;
}
private static Set<ProverOptions> toSet(ProverOptions... options) {
Set<ProverOptions> opts = EnumSet.noneOf(ProverOptions.class);
Collections.addAll(opts, options);
return opts;
}
/**
* This method loads the given libraries.
*
* <p>If the first list of libraries can not be loaded, the second list is used as a fallback. The
* two lists of libraries can be used to differ between libraries specific to Unix/Linux and
* Windows.
*
* <p>If the first try aborts after a few steps, we do not clean up partially loaded libraries,
* but directly start the second try.
*
* <p>Each list is applied in the given ordering.
*
* @param loader the loading mechanism that will be used for loading each single library.
* @param librariesForFirstTry list of library names that will be loaded, if possible.
* @param librariesForSecondTry list of library names that will be loaded, after the first attempt
* (using librariesForFirstTry) has failed.
* @throws UnsatisfiedLinkError if neither the first nor second try returned a successful loading
* process.
*/
protected static void loadLibrariesWithFallback(
Consumer<String> loader,
List<String> librariesForFirstTry,
List<String> librariesForSecondTry)
throws UnsatisfiedLinkError {
Preconditions.checkNotNull(librariesForFirstTry);
Preconditions.checkNotNull(librariesForSecondTry);
try {
librariesForFirstTry.forEach(loader);
} catch (UnsatisfiedLinkError e1) {
try {
librariesForSecondTry.forEach(loader);
} catch (UnsatisfiedLinkError e2) {
e1.addSuppressed(e2);
throw e1;
}
}
}
}