-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathSimpleUserPropagator.java
220 lines (191 loc) · 8.65 KB
/
SimpleUserPropagator.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
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
209
210
211
212
213
214
215
216
217
218
219
220
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2024 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.example;
import com.google.common.base.Verify;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import org.sosy_lab.common.ShutdownNotifier;
import org.sosy_lab.common.configuration.Configuration;
import org.sosy_lab.common.configuration.InvalidConfigurationException;
import org.sosy_lab.common.log.BasicLogManager;
import org.sosy_lab.common.log.LogManager;
import org.sosy_lab.java_smt.SolverContextFactory;
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.BooleanFormulaManager;
import org.sosy_lab.java_smt.api.PropagatorBackend;
import org.sosy_lab.java_smt.api.ProverEnvironment;
import org.sosy_lab.java_smt.api.SolverContext;
import org.sosy_lab.java_smt.api.SolverContext.ProverOptions;
import org.sosy_lab.java_smt.api.SolverException;
import org.sosy_lab.java_smt.basicimpl.AbstractUserPropagator;
/** Example of a simple user propagator that prohibits variables/expressions to be set to true. */
public final class SimpleUserPropagator {
private SimpleUserPropagator() {}
public static void main(String[] args)
throws InvalidConfigurationException, InterruptedException, SolverException {
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
try (SolverContext context =
SolverContextFactory.createSolverContext(config, logger, notifier, Solvers.Z3)) {
final BooleanFormulaManager bmgr = context.getFormulaManager().getBooleanFormulaManager();
testWithBlockedLiterals(context, bmgr, logger);
testWithBlockedClause(context, bmgr, logger);
testWithBlockedSubclauses(context, bmgr, logger);
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
logger.logUserException(Level.INFO, e, "Z3 is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
private static void testWithBlockedLiterals(
SolverContext context, BooleanFormulaManager bmgr, LogManager logger)
throws InterruptedException, SolverException {
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
// assert "p or q or r or s"
BooleanFormula p = bmgr.makeVariable("p");
BooleanFormula q = bmgr.makeVariable("q");
BooleanFormula r = bmgr.makeVariable("r");
BooleanFormula s = bmgr.makeVariable("s");
BooleanFormula clause = bmgr.or(p, q, r, s);
prover.addConstraint(clause);
logger.log(
Level.INFO,
"========== Checking satisfiability of",
clause,
"while blocking " + "all literals ==========");
// Create user propagator that prohibits variables to be set to true
MyUserPropagator myUserPropagator = new MyUserPropagator(logger);
Verify.verify(prover.registerUserPropagator(myUserPropagator));
myUserPropagator.registerExpression(p);
myUserPropagator.registerExpression(q);
myUserPropagator.registerExpression(r);
myUserPropagator.registerExpression(s);
// Instance should be UNSAT now due to the user propagator
boolean isUnsat = prover.isUnsat();
logger.log(Level.INFO, "Formula", clause, "is", isUnsat ? "UNSAT" : "SAT");
}
}
private static void testWithBlockedClause(
SolverContext context, BooleanFormulaManager bmgr, LogManager logger)
throws InterruptedException, SolverException {
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
// assert "p or q or r or s"
BooleanFormula p = bmgr.makeVariable("p");
BooleanFormula q = bmgr.makeVariable("q");
BooleanFormula r = bmgr.makeVariable("r");
BooleanFormula s = bmgr.makeVariable("s");
BooleanFormula clause = bmgr.or(p, q, r, s);
prover.addConstraint(clause);
logger.log(
Level.INFO,
"========== Checking satisfiability of",
clause,
"while blocking " + "the full clause ==========");
// Create user propagator that prohibits the full clause to be set to true.
MyUserPropagator myUserPropagator = new MyUserPropagator(logger);
Verify.verify(prover.registerUserPropagator(myUserPropagator));
myUserPropagator.registerExpression(clause);
// Instance should be UNSAT now due to the user propagator
boolean isUnsat = prover.isUnsat();
logger.log(Level.INFO, "Formula", clause, "is", isUnsat ? "UNSAT" : "SAT");
}
}
private static void testWithBlockedSubclauses(
SolverContext context, BooleanFormulaManager bmgr, LogManager logger)
throws InterruptedException, SolverException {
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
// assert "p or q or r or s"
BooleanFormula p = bmgr.makeVariable("p");
BooleanFormula q = bmgr.makeVariable("q");
BooleanFormula r = bmgr.makeVariable("r");
BooleanFormula s = bmgr.makeVariable("s");
BooleanFormula clause = bmgr.or(p, q, r, s);
BooleanFormula subclause1 = bmgr.or(p, q);
BooleanFormula subclause2 = bmgr.or(r, s);
prover.addConstraint(clause);
logger.log(
Level.INFO,
"========== Checking satisfiability of",
clause,
"while blocking " + "the subclauses",
subclause1,
"and",
subclause2,
"==========");
// Create user propagator that prohibits (sub)clauses to be set to true.
// Note that the subclauses are not directly asserted in the original input formula.
MyUserPropagator myUserPropagator = new MyUserPropagator(logger);
Verify.verify(prover.registerUserPropagator(myUserPropagator));
myUserPropagator.registerExpression(subclause1);
myUserPropagator.registerExpression(subclause2);
// Instance should be UNSAT now due to the user propagator
boolean isUnsat = prover.isUnsat();
logger.log(Level.INFO, "Formula", clause, "is", isUnsat ? "UNSAT" : "SAT");
}
}
/** This user propagator will raise a conflict whenever a registered expression is set to true. */
private static class MyUserPropagator extends AbstractUserPropagator {
private final List<BooleanFormula> disabledExpressions = new ArrayList<>();
private final LogManager logger;
MyUserPropagator(LogManager logger) {
this.logger = logger;
}
@Override
public void onPush() {
logger.log(Level.INFO, "Solver pushed");
}
@Override
public void onPop(int numPoppedLevels) {
logger.log(Level.INFO, "Solver popped", numPoppedLevels, "levels");
}
@Override
public void onKnownValue(BooleanFormula expr, boolean value) {
logger.log(Level.INFO, "Solver assigned", expr, "to", value);
if (value && disabledExpressions.contains(expr)) {
logger.log(Level.INFO, "User propagator raised conflict on", expr);
getBackend().propagateConflict(new BooleanFormula[] {expr});
}
}
@Override
public void onDecision(BooleanFormula expr, boolean value) {
// NOTE 1: This part just serves to show the ability to affect solver decision.
// In this case, we force the solver to run into conflicts as early as possible.
// NOTE 2: The same code could be executed in `onKnownValue` to influence the next(!)
// decision the solver would make.
for (BooleanFormula disExpr : disabledExpressions) {
final boolean decisionValue = true;
if (getBackend().propagateNextDecision(disExpr, Optional.of(decisionValue))) {
// The above call returns "true" if the provided literal is yet undecided, otherwise
// false.
logger.log(
Level.INFO,
String.format(
"User propagator overwrites decision from '%s = %s' to '%s = %s'",
expr, value, disExpr, decisionValue));
break;
}
}
}
@Override
public void initializeWithBackend(PropagatorBackend backend) {
super.initializeWithBackend(backend);
// Enable callbacks
backend.notifyOnKnownValue();
backend.notifyOnDecision();
}
@Override
public void registerExpression(BooleanFormula theoryExpr) {
disabledExpressions.add(theoryExpr);
super.registerExpression(theoryExpr);
}
}
}