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 package org.sat4j.tools;
31
32 import java.util.Collection;
33
34 import org.sat4j.core.VecInt;
35 import org.sat4j.specs.ISolver;
36 import org.sat4j.specs.IVecInt;
37 import org.sat4j.specs.IteratorInt;
38 import org.sat4j.specs.TimeoutException;
39
40 public abstract class AbstractClauseSelectorSolver<T extends ISolver> extends
41 SolverDecorator<T> {
42
43 private static final long serialVersionUID = 1L;
44 private int lastCreatedVar;
45 private boolean pooledVarId = false;
46
47 private interface SelectorState {
48
49 boolean isSatisfiable(boolean global) throws TimeoutException;
50
51 boolean isSatisfiable() throws TimeoutException;
52
53 boolean isSatisfiable(IVecInt assumps) throws TimeoutException;
54
55 boolean isSatisfiable(IVecInt assumps, boolean global)
56 throws TimeoutException;
57
58 }
59
60 private final SelectorState external = new SelectorState() {
61
62 private IVecInt getNegatedSelectors() {
63 IVecInt assumps = new VecInt();
64 for (int var : getAddedVars()) {
65 assumps.push(-var);
66 }
67 return assumps;
68 }
69
70 public boolean isSatisfiable(boolean global) throws TimeoutException {
71 return decorated().isSatisfiable(getNegatedSelectors(), global);
72 }
73
74 public boolean isSatisfiable(IVecInt assumps, boolean global)
75 throws TimeoutException {
76 IVecInt all = getNegatedSelectors();
77 assumps.copyTo(all);
78 return decorated().isSatisfiable(all, global);
79 }
80
81 public boolean isSatisfiable() throws TimeoutException {
82 return decorated().isSatisfiable(getNegatedSelectors());
83 }
84
85 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
86 IVecInt all = getNegatedSelectors();
87 assumps.copyTo(all);
88 return decorated().isSatisfiable(all);
89 }
90
91 };
92
93 private final SelectorState internal = new SelectorState() {
94
95 public boolean isSatisfiable(boolean global) throws TimeoutException {
96 return decorated().isSatisfiable(global);
97 }
98
99 public boolean isSatisfiable() throws TimeoutException {
100 return decorated().isSatisfiable();
101 }
102
103 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
104 return decorated().isSatisfiable(assumps);
105 }
106
107 public boolean isSatisfiable(IVecInt assumps, boolean global)
108 throws TimeoutException {
109 return decorated().isSatisfiable(assumps, global);
110 }
111 };
112
113 private SelectorState selectedState = external;
114
115 public AbstractClauseSelectorSolver(T solver) {
116 super(solver);
117 }
118
119 public abstract Collection<Integer> getAddedVars();
120
121
122
123
124
125
126
127 protected int createNewVar(IVecInt literals) {
128 for (IteratorInt it = literals.iterator(); it.hasNext();) {
129 if (Math.abs(it.next()) > nextFreeVarId(false)) {
130 throw new IllegalStateException(
131 "Please call newVar(int) before adding constraints!!!");
132 }
133 }
134 if (this.pooledVarId) {
135 this.pooledVarId = false;
136 return this.lastCreatedVar;
137 }
138 this.lastCreatedVar = nextFreeVarId(true);
139 return this.lastCreatedVar;
140 }
141
142 protected void discardLastestVar() {
143 this.pooledVarId = true;
144 }
145
146 @Override
147 public boolean isSatisfiable(boolean global) throws TimeoutException {
148 return selectedState.isSatisfiable(global);
149 }
150
151 @Override
152 public boolean isSatisfiable(IVecInt assumps, boolean global)
153 throws TimeoutException {
154 return selectedState.isSatisfiable(assumps, global);
155 }
156
157 @Override
158 public boolean isSatisfiable() throws TimeoutException {
159 return selectedState.isSatisfiable();
160 }
161
162 @Override
163 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
164 return selectedState.isSatisfiable(assumps);
165 }
166
167
168
169
170
171
172
173 public void internalState() {
174 this.selectedState = internal;
175 }
176
177
178
179
180
181
182
183 public void externalState() {
184 this.selectedState = external;
185 }
186 }