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(" " + nb);
104 nbclauses = nb;
105 fixedNbClauses = true;
106 }
107
108 public IConstr addClause(IVecInt literals) throws ContradictionException {
109 if (firstConstr) {
110 if (!fixedNbClauses) {
111 firstCharPos = 7 + Integer.toString(nbvars).length();
112 out.append(" ");
113 out.append("\n");
114 nbclauses = 0;
115 }
116 firstConstr = false;
117 }
118 if (!fixedNbClauses)
119 nbclauses++;
120
121 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();)
122 out.append(iterator.next()).append(" ");
123 out.append("0\n");
124 return null;
125 }
126
127 public boolean removeConstr(IConstr c) {
128 throw new UnsupportedOperationException();
129 }
130
131 public void addAllClauses(IVec<IVecInt> clauses)
132 throws ContradictionException {
133 throw new UnsupportedOperationException();
134 }
135
136 public IConstr addAtMost(IVecInt literals, int degree)
137 throws ContradictionException {
138 if (degree > 1) {
139 throw new UnsupportedOperationException(
140 "Not a clausal problem! degree " + degree);
141 }
142 assert degree == 1;
143 if (firstConstr) {
144 firstCharPos = 0;
145 out.append(" ");
146 out.append("\n");
147 nbclauses = 0;
148 firstConstr = false;
149 }
150
151 for (int i = 0; i <= literals.size(); i++) {
152 for (int j = i + 1; j < literals.size(); j++) {
153 if (!fixedNbClauses)
154 nbclauses++;
155 out.append("" + (-literals.get(i)) + " " + (-literals.get(j))
156 + " 0\n");
157 }
158 }
159 return null;
160 }
161
162 public IConstr addAtLeast(IVecInt literals, int degree)
163 throws ContradictionException {
164 if (degree > 1) {
165 throw new UnsupportedOperationException(
166 "Not a clausal problem! degree " + degree);
167 }
168 assert degree == 1;
169 return addClause(literals);
170 }
171
172 public void setTimeout(int t) {
173
174
175 }
176
177 public void setTimeoutMs(long t) {
178
179 }
180
181 public int getTimeout() {
182 return 0;
183 }
184
185
186
187
188 public long getTimeoutMs() {
189 return 0L;
190 }
191
192 public void reset() {
193 fixedNbClauses = false;
194 firstConstr = true;
195 out = new StringBuffer(initBuilderSize);
196 maxvarid = 0;
197 }
198
199 public void printStat(PrintStream output, String prefix) {
200
201 }
202
203 public void printStat(PrintWriter output, String prefix) {
204
205
206 }
207
208 public Map<String, Number> getStat() {
209
210 return null;
211 }
212
213 public String toString(String prefix) {
214 return "Dimacs output solver";
215 }
216
217 public void clearLearntClauses() {
218
219
220 }
221
222 public int[] model() {
223 throw new UnsupportedOperationException();
224 }
225
226 public boolean model(int var) {
227 throw new UnsupportedOperationException();
228 }
229
230 public boolean isSatisfiable() throws TimeoutException {
231 throw new TimeoutException("There is no real solver behind!");
232 }
233
234 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
235 throw new TimeoutException("There is no real solver behind!");
236 }
237
238 public int[] findModel() throws TimeoutException {
239 throw new UnsupportedOperationException();
240 }
241
242 public int[] findModel(IVecInt assumps) throws TimeoutException {
243 throw new UnsupportedOperationException();
244 }
245
246 public int nConstraints() {
247 return nbclauses;
248 }
249
250 public int nVars() {
251 return maxvarid;
252 }
253
254 @Override
255 public String toString() {
256
257
258
259
260
261 out.insert(firstCharPos, "p cnf " + maxvarid + " " + nbclauses);
262 return out.toString();
263 }
264
265 public void expireTimeout() {
266
267
268 }
269
270 public boolean isSatisfiable(IVecInt assumps, boolean global)
271 throws TimeoutException {
272 throw new TimeoutException("There is no real solver behind!");
273 }
274
275 public boolean isSatisfiable(boolean global) throws TimeoutException {
276 throw new TimeoutException("There is no real solver behind!");
277 }
278
279 public void printInfos(PrintWriter output, String prefix) {
280 }
281
282 public void setTimeoutOnConflicts(int count) {
283
284 }
285
286 public boolean isDBSimplificationAllowed() {
287 return false;
288 }
289
290 public void setDBSimplificationAllowed(boolean status) {
291
292 }
293
294
295
296
297 public void setSearchListener(SearchListener sl) {
298 }
299
300
301
302
303 public int nextFreeVarId(boolean reserve) {
304 if (reserve) {
305 maxvarid++;
306 return maxvarid;
307 }
308 return maxvarid;
309 }
310
311
312
313
314 public boolean removeSubsumedConstr(IConstr c) {
315 return false;
316 }
317
318
319
320
321 public IConstr addBlockingClause(IVecInt literals)
322 throws ContradictionException {
323 throw new UnsupportedOperationException();
324 }
325 }