-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAbstractProver.java
167 lines (139 loc) · 5.29 KB
/
AbstractProver.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
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
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2023 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.basicimpl;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.sosy_lab.java_smt.api.BasicProverEnvironment;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.Evaluator;
import org.sosy_lab.java_smt.api.SolverContext.ProverOptions;
public abstract class AbstractProver<T> implements BasicProverEnvironment<T> {
private final boolean generateModels;
private final boolean generateAllSat;
protected final boolean generateUnsatCores;
private final boolean generateUnsatCoresOverAssumptions;
protected final boolean enableSL;
protected boolean closed = false;
protected boolean useBinary;
private final Set<Evaluator> evaluators = new LinkedHashSet<>();
/** This data-structure tracks all formulas that were asserted on different levels. */
private final List<Map<BooleanFormula, T>> assertedFormulas = new ArrayList<>();
private static final String TEMPLATE = "Please set the prover option %s.";
protected AbstractProver(Set<ProverOptions> pOptions) {
generateModels = pOptions.contains(ProverOptions.GENERATE_MODELS);
generateAllSat = pOptions.contains(ProverOptions.GENERATE_ALL_SAT);
generateUnsatCores = pOptions.contains(ProverOptions.GENERATE_UNSAT_CORE);
generateUnsatCoresOverAssumptions =
pOptions.contains(ProverOptions.GENERATE_UNSAT_CORE_OVER_ASSUMPTIONS);
enableSL = pOptions.contains(ProverOptions.ENABLE_SEPARATION_LOGIC);
useBinary = pOptions.contains(ProverOptions.USE_BINARY);
assertedFormulas.add(new LinkedHashMap<>());
}
protected final void checkGenerateModels() {
Preconditions.checkState(generateModels, TEMPLATE, ProverOptions.GENERATE_MODELS);
}
protected final void checkGenerateAllSat() {
Preconditions.checkState(generateAllSat, TEMPLATE, ProverOptions.GENERATE_ALL_SAT);
}
protected final void checkGenerateUnsatCores() {
Preconditions.checkState(generateUnsatCores, TEMPLATE, ProverOptions.GENERATE_UNSAT_CORE);
}
protected final void checkGenerateUnsatCoresOverAssumptions() {
Preconditions.checkState(
generateUnsatCoresOverAssumptions,
TEMPLATE,
ProverOptions.GENERATE_UNSAT_CORE_OVER_ASSUMPTIONS);
}
protected final void checkEnableSeparationLogic() {
Preconditions.checkState(enableSL, TEMPLATE, ProverOptions.ENABLE_SEPARATION_LOGIC);
}
@Override
public int size() {
checkState(!closed);
return assertedFormulas.size() - 1;
}
@Override
public final void push() throws InterruptedException {
checkState(!closed);
if (Generator.isLoggingEnabled()) {
Generator.logPush();
}
pushImpl();
assertedFormulas.add(new LinkedHashMap<>());
}
protected abstract void pushImpl() throws InterruptedException;
@Override
public final void pop() {
checkState(!closed);
checkState(assertedFormulas.size() > 1, "initial level must remain until close");
if (Generator.isLoggingEnabled()) {
Generator.logPop();
}
assertedFormulas.remove(assertedFormulas.size() - 1); // remove last
popImpl();
}
protected abstract void popImpl();
@Override
@CanIgnoreReturnValue
public final @Nullable T addConstraint(BooleanFormula constraint) throws InterruptedException {
checkState(!closed);
if (Generator.isLoggingEnabled()) {
Generator.assembleConstraint(constraint);
}
T t = addConstraintImpl(constraint);
Iterables.getLast(assertedFormulas).put(constraint, t);
return t;
}
protected abstract @Nullable T addConstraintImpl(BooleanFormula constraint)
throws InterruptedException;
protected ImmutableSet<BooleanFormula> getAssertedFormulas() {
ImmutableSet.Builder<BooleanFormula> builder = ImmutableSet.builder();
for (Map<BooleanFormula, T> level : assertedFormulas) {
builder.addAll(level.keySet());
}
return builder.build();
}
protected ImmutableSet<T> getAssertedConstraintIds() {
ImmutableSet.Builder<T> builder = ImmutableSet.builder();
for (Map<BooleanFormula, T> level : assertedFormulas) {
builder.addAll(level.values());
}
return builder.build();
}
/**
* This method registers the Evaluator to be cleaned up before the next change on the prover
* stack.
*/
protected <E extends Evaluator> E registerEvaluator(E pEvaluator) {
evaluators.add(pEvaluator);
return pEvaluator;
}
protected void unregisterEvaluator(Evaluator pEvaluator) {
evaluators.remove(pEvaluator);
}
protected void closeAllEvaluators() {
evaluators.forEach(Evaluator::close);
evaluators.clear();
}
@Override
public void close() {
assertedFormulas.clear();
closeAllEvaluators();
closed = true;
}
}