-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathAbstractSolverContext.java
139 lines (123 loc) · 5.14 KB
/
AbstractSolverContext.java
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
// 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.Preconditions;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
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) {
InterpolatingProverEnvironment<?> out = newProverEnvironmentWithInterpolation0(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;
}
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 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;
}
}
}
}