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 package org.sat4j.tools;
29
30 import java.io.PrintStream;
31 import java.io.PrintWriter;
32 import java.util.Map;
33
34 import org.sat4j.specs.ContradictionException;
35 import org.sat4j.specs.IConstr;
36 import org.sat4j.specs.ISolver;
37 import org.sat4j.specs.IVec;
38 import org.sat4j.specs.IVecInt;
39 import org.sat4j.specs.IteratorInt;
40 import org.sat4j.specs.SearchListener;
41 import org.sat4j.specs.TimeoutException;
42
43
44
45
46
47
48
49
50
51
52 public class DimacsStringSolver implements ISolver {
53
54
55
56
57 private static final long serialVersionUID = 1L;
58
59 private StringBuffer out;
60
61 private int nbvars;
62
63 private int nbclauses;
64
65 private boolean fixedNbClauses = false;
66
67 private boolean firstConstr = true;
68
69 private int firstCharPos;
70
71 private final int initBuilderSize;
72
73 private int maxvarid = 0;
74
75 public DimacsStringSolver() {
76 this(16);
77 }
78
79 public DimacsStringSolver(int initSize) {
80 out = new StringBuffer(initSize);
81 initBuilderSize = initSize;
82 }
83
84 public StringBuffer getOut() {
85 return out;
86 }
87
88 public int newVar() {
89 return 0;
90 }
91
92 public int newVar(int howmany) {
93 setNbVars(howmany);
94 return howmany;
95 }
96
97 protected void setNbVars(int howmany) {
98 nbvars = howmany;
99 maxvarid = howmany;
100 }
101
102 public void setExpectedNumberOfClauses(int nb) {
103 out.append(" ");
104 out.append(nb);
105 nbclauses = nb;
106 fixedNbClauses = true;
107 }
108
109 public IConstr addClause(IVecInt literals) throws ContradictionException {
110 if (firstConstr) {
111 if (!fixedNbClauses) {
112 firstCharPos = 7 + Integer.toString(nbvars).length();
113 out.append(" ");
114 out.append("\n");
115 nbclauses = 0;
116 }
117 firstConstr = false;
118 }
119 if (!fixedNbClauses) {
120 nbclauses++;
121 }
122 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
123 out.append(iterator.next()).append(" ");
124 }
125 out.append("0\n");
126 return null;
127 }
128
129 public boolean removeConstr(IConstr c) {
130 throw new UnsupportedOperationException();
131 }
132
133 public void addAllClauses(IVec<IVecInt> clauses)
134 throws ContradictionException {
135 throw new UnsupportedOperationException();
136 }
137
138 public IConstr addAtMost(IVecInt literals, int degree)
139 throws ContradictionException {
140 if (degree > 1) {
141 throw new UnsupportedOperationException(
142 "Not a clausal problem! degree " + degree);
143 }
144 assert degree == 1;
145 if (firstConstr) {
146 firstCharPos = 0;
147 out.append(" ");
148 out.append("\n");
149 nbclauses = 0;
150 firstConstr = false;
151 }
152
153 for (int i = 0; i <= literals.size(); i++) {
154 for (int j = i + 1; j < literals.size(); j++) {
155 if (!fixedNbClauses) {
156 nbclauses++;
157 }
158 out.append(-literals.get(i));
159 out.append(" ");
160 out.append(-literals.get(j));
161 out.append(" 0\n");
162 }
163 }
164 return null;
165 }
166
167 public IConstr addAtLeast(IVecInt literals, int degree)
168 throws ContradictionException {
169 if (degree > 1) {
170 throw new UnsupportedOperationException(
171 "Not a clausal problem! degree " + degree);
172 }
173 assert degree == 1;
174 return addClause(literals);
175 }
176
177 public void setTimeout(int t) {
178
179
180 }
181
182 public void setTimeoutMs(long t) {
183
184 }
185
186 public int getTimeout() {
187 return 0;
188 }
189
190
191
192
193 public long getTimeoutMs() {
194 return 0L;
195 }
196
197 public void reset() {
198 fixedNbClauses = false;
199 firstConstr = true;
200 out = new StringBuffer(initBuilderSize);
201 maxvarid = 0;
202 }
203
204 public void printStat(PrintStream output, String prefix) {
205
206 }
207
208 public void printStat(PrintWriter output, String prefix) {
209
210
211 }
212
213 public Map<String, Number> getStat() {
214
215 return null;
216 }
217
218 public String toString(String prefix) {
219 return "Dimacs output solver";
220 }
221
222 public void clearLearntClauses() {
223
224
225 }
226
227 public int[] model() {
228 throw new UnsupportedOperationException();
229 }
230
231 public boolean model(int var) {
232 throw new UnsupportedOperationException();
233 }
234
235 public boolean isSatisfiable() throws TimeoutException {
236 throw new TimeoutException("There is no real solver behind!");
237 }
238
239 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
240 throw new TimeoutException("There is no real solver behind!");
241 }
242
243 public int[] findModel() throws TimeoutException {
244 throw new UnsupportedOperationException();
245 }
246
247 public int[] findModel(IVecInt assumps) throws TimeoutException {
248 throw new UnsupportedOperationException();
249 }
250
251 public int nConstraints() {
252 return nbclauses;
253 }
254
255 public int nVars() {
256 return maxvarid;
257 }
258
259 @Override
260 public String toString() {
261
262
263
264
265
266 out.insert(firstCharPos, "p cnf " + maxvarid + " " + nbclauses);
267 return out.toString();
268 }
269
270 public void expireTimeout() {
271
272
273 }
274
275 public boolean isSatisfiable(IVecInt assumps, boolean global)
276 throws TimeoutException {
277 throw new TimeoutException("There is no real solver behind!");
278 }
279
280 public boolean isSatisfiable(boolean global) throws TimeoutException {
281 throw new TimeoutException("There is no real solver behind!");
282 }
283
284 public void printInfos(PrintWriter output, String prefix) {
285 }
286
287 public void setTimeoutOnConflicts(int count) {
288
289 }
290
291 public boolean isDBSimplificationAllowed() {
292 return false;
293 }
294
295 public void setDBSimplificationAllowed(boolean status) {
296
297 }
298
299
300
301
302 public void setSearchListener(SearchListener sl) {
303 }
304
305
306
307
308 public int nextFreeVarId(boolean reserve) {
309 if (reserve) {
310 maxvarid++;
311 return maxvarid;
312 }
313 return maxvarid;
314 }
315
316
317
318
319 public boolean removeSubsumedConstr(IConstr c) {
320 return false;
321 }
322
323
324
325
326 public IConstr addBlockingClause(IVecInt literals)
327 throws ContradictionException {
328 throw new UnsupportedOperationException();
329 }
330
331
332
333
334 public SearchListener getSearchListener() {
335 throw new UnsupportedOperationException();
336 }
337
338
339
340
341 public boolean isVerbose() {
342 return true;
343 }
344
345
346
347
348 public void setVerbose(boolean value) {
349
350 }
351
352
353
354
355 public void setLogPrefix(String prefix) {
356
357
358 }
359
360
361
362
363 public String getLogPrefix() {
364 return "";
365 }
366
367
368
369
370 public IVecInt unsatExplanation() {
371 throw new UnsupportedOperationException();
372 }
373 }