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.TimeoutException;
41
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 public DimacsStringSolver() {
74 this(16);
75 }
76
77 public DimacsStringSolver(int initSize) {
78 out = new StringBuffer(initSize);
79 initBuilderSize = initSize;
80 }
81
82 public StringBuffer getOut(){
83 return out;
84 }
85
86 public int newVar() {
87 return 0;
88 }
89
90 public int newVar(int howmany) {
91 out.append("p cnf " + howmany);
92 setNbVars(howmany);
93 return howmany;
94 }
95
96 protected void setNbVars(int howmany){
97 nbvars = howmany;
98 }
99
100 public void setExpectedNumberOfClauses(int nb) {
101 out.append(" " + nb);
102 nbclauses = nb;
103 fixedNbClauses = true;
104 }
105
106 public IConstr addClause(IVecInt literals) throws ContradictionException {
107 if (firstConstr) {
108 if (!fixedNbClauses) {
109 firstCharPos = 7 + Integer.toString(nbvars).length();
110 out.append(" ");
111 out.append("\n");
112 nbclauses=0;
113 }
114 firstConstr = false;
115 }
116 if (!fixedNbClauses) nbclauses++;
117
118 for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
119 out.append(iterator.next()).append(" ");
120 out.append("0\n");
121 return null;
122 }
123
124 public boolean removeConstr(IConstr c) {
125 throw new UnsupportedOperationException();
126 }
127
128 public void addAllClauses(IVec<IVecInt> clauses)
129 throws ContradictionException {
130 throw new UnsupportedOperationException();
131 }
132
133 public IConstr addAtMost(IVecInt literals, int degree)
134 throws ContradictionException {
135 if (degree > 1) {
136 throw new UnsupportedOperationException(
137 "Not a clausal problem! degree " + degree);
138 }
139 assert degree == 1;
140 if (firstConstr) {
141 if (!fixedNbClauses) {
142 firstCharPos = 7 + Integer.toString(nbvars).length();
143 out.append(" ");
144 out.append("\n");
145 nbclauses=0;
146 }
147 firstConstr = false;
148 }
149
150 for (int i = 0; i <= literals.size(); i++) {
151 for (int j = i + 1; j < literals.size(); j++) {
152 if (!fixedNbClauses) nbclauses++;
153 out.append("" + (-literals.get(i)) + " " + (-literals.get(j))
154 + " 0\n");
155 }
156 }
157 return null;
158 }
159
160 public IConstr addAtLeast(IVecInt literals, int degree)
161 throws ContradictionException {
162 if (degree > 1) {
163 throw new UnsupportedOperationException(
164 "Not a clausal problem! degree " + degree);
165 }
166 assert degree == 1;
167 return addClause(literals);
168 }
169
170 public void setTimeout(int t) {
171
172
173 }
174
175 public void setTimeoutMs(long t) {
176
177 }
178
179 public int getTimeout() {
180 return 0;
181 }
182
183 public long getTimeoutMs() {
184 return 0L;
185 }
186
187 public void reset() {
188 fixedNbClauses = false;
189 firstConstr = true;
190 out = new StringBuffer(initBuilderSize);
191 }
192
193 public void printStat(PrintStream output, String prefix) {
194
195 }
196
197 public void printStat(PrintWriter output, String prefix) {
198
199
200 }
201
202 public Map<String, Number> getStat() {
203
204 return null;
205 }
206
207 public String toString(String prefix) {
208 return "Dimacs output solver";
209 }
210
211 public void clearLearntClauses() {
212
213
214 }
215
216 public int[] model() {
217 throw new UnsupportedOperationException();
218 }
219
220 public boolean model(int var) {
221 throw new UnsupportedOperationException();
222 }
223
224 public boolean isSatisfiable() throws TimeoutException {
225 throw new TimeoutException("There is no real solver behind!");
226 }
227
228 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
229 throw new TimeoutException("There is no real solver behind!");
230 }
231
232 public int[] findModel() throws TimeoutException {
233 throw new UnsupportedOperationException();
234 }
235
236 public int[] findModel(IVecInt assumps) throws TimeoutException {
237 throw new UnsupportedOperationException();
238 }
239
240 public int nConstraints() {
241 return nbclauses;
242 }
243
244 public int nVars() {
245 return nbvars;
246 }
247
248 @Override
249 public String toString() {
250
251
252
253
254
255 out.insert(firstCharPos, nbclauses);
256 return out.toString();
257 }
258
259 public void expireTimeout() {
260
261
262 }
263
264 public boolean isSatisfiable(IVecInt assumps, boolean global)
265 throws TimeoutException {
266 throw new TimeoutException("There is no real solver behind!");
267 }
268
269 public boolean isSatisfiable(boolean global) throws TimeoutException {
270 throw new TimeoutException("There is no real solver behind!");
271 }
272
273 public void printInfos(PrintWriter output, String prefix) {
274 }
275
276 public void setTimeoutOnConflicts(int count) {
277
278 }
279
280 public boolean isDBSimplificationAllowed() {
281 return false;
282 }
283
284 public void setDBSimplificationAllowed(boolean status) {
285
286 }
287
288 }