View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   * 
19   * Based on the original MiniSat specification from:
20   * 
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
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   * Solver used to write down a CNF into a String.
45   * 
46   * It is especially useful compared to the DimacsOutputSolver because the number
47   * of clauses does not need to be known in advance.
48   * 
49   * @author leberre
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 		// TODO Auto-generated method stub
179 
180 	}
181 
182 	public void setTimeoutMs(long t) {
183 		// TODO Auto-generated method stub
184 	}
185 
186 	public int getTimeout() {
187 		return 0;
188 	}
189 
190 	/**
191 	 * @since 2.1
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 		// TODO Auto-generated method stub
206 	}
207 
208 	public void printStat(PrintWriter output, String prefix) {
209 		// TODO Auto-generated method stub
210 
211 	}
212 
213 	public Map<String, Number> getStat() {
214 		// TODO Auto-generated method stub
215 		return null;
216 	}
217 
218 	public String toString(String prefix) {
219 		return "Dimacs output solver";
220 	}
221 
222 	public void clearLearntClauses() {
223 		// TODO Auto-generated method stub
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 		// String numClauses = Integer.toString(nbclauses);
262 		// int numClausesLength = numClauses.length();
263 		// for (int i = 0; i < numClausesLength; ++i) {
264 		// out.setCharAt(firstCharPos + i, numClauses.charAt(i));
265 		// }
266 		out.insert(firstCharPos, "p cnf " + maxvarid + " " + nbclauses);
267 		return out.toString();
268 	}
269 
270 	public void expireTimeout() {
271 		// TODO Auto-generated method stub
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 	 * @since 2.1
301 	 */
302 	public void setSearchListener(SearchListener sl) {
303 	}
304 
305 	/**
306 	 * @since 2.1
307 	 */
308 	public int nextFreeVarId(boolean reserve) {
309 		if (reserve) {
310 			maxvarid++;
311 			return maxvarid;
312 		}
313 		return maxvarid;
314 	}
315 
316 	/**
317 	 * @since 2.1
318 	 */
319 	public boolean removeSubsumedConstr(IConstr c) {
320 		return false;
321 	}
322 
323 	/**
324 	 * @since 2.1
325 	 */
326 	public IConstr addBlockingClause(IVecInt literals)
327 			throws ContradictionException {
328 		throw new UnsupportedOperationException();
329 	}
330 
331 	/**
332 	 * @since 2.2
333 	 */
334 	public SearchListener getSearchListener() {
335 		throw new UnsupportedOperationException();
336 	}
337 
338 	/**
339 	 * @since 2.2
340 	 */
341 	public boolean isVerbose() {
342 		return true;
343 	}
344 
345 	/**
346 	 * @since 2.2
347 	 */
348 	public void setVerbose(boolean value) {
349 		// do nothing
350 	}
351 
352 	/**
353 	 * @since 2.2
354 	 */
355 	public void setLogPrefix(String prefix) {
356 		// do nothing
357 
358 	}
359 
360 	/**
361 	 * @since 2.2
362 	 */
363 	public String getLogPrefix() {
364 		return "";
365 	}
366 
367 	/**
368 	 * @since 2.2
369 	 */
370 	public IVecInt unsatExplanation() {
371 		throw new UnsupportedOperationException();
372 	}
373 }