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(" " + 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 		// TODO Auto-generated method stub
174 
175 	}
176 
177 	public void setTimeoutMs(long t) {
178 		// TODO Auto-generated method stub
179 	}
180 
181 	public int getTimeout() {
182 		return 0;
183 	}
184 
185 	/**
186 	 * @since 2.1
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 		// TODO Auto-generated method stub
201 	}
202 
203 	public void printStat(PrintWriter output, String prefix) {
204 		// TODO Auto-generated method stub
205 
206 	}
207 
208 	public Map<String, Number> getStat() {
209 		// TODO Auto-generated method stub
210 		return null;
211 	}
212 
213 	public String toString(String prefix) {
214 		return "Dimacs output solver";
215 	}
216 
217 	public void clearLearntClauses() {
218 		// TODO Auto-generated method stub
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 		// String numClauses = Integer.toString(nbclauses);
257 		// int numClausesLength = numClauses.length();
258 		// for (int i = 0; i < numClausesLength; ++i) {
259 		// out.setCharAt(firstCharPos + i, numClauses.charAt(i));
260 		// }
261 		out.insert(firstCharPos, "p cnf " + maxvarid + " " + nbclauses);
262 		return out.toString();
263 	}
264 
265 	public void expireTimeout() {
266 		// TODO Auto-generated method stub
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 	 * @since 2.1
296 	 */
297 	public void setSearchListener(SearchListener sl) {
298 	}
299 
300 	/**
301 	 * @since 2.1
302 	 */
303 	public int nextFreeVarId(boolean reserve) {
304 		if (reserve) {
305 			maxvarid++;
306 			return maxvarid;
307 		}
308 		return maxvarid;
309 	}
310 
311 	/**
312 	 * @since 2.1
313 	 */
314 	public boolean removeSubsumedConstr(IConstr c) {
315 		return false;
316 	}
317 
318 	/**
319 	 * @since 2.1
320 	 */
321 	public IConstr addBlockingClause(IVecInt literals)
322 			throws ContradictionException {
323 		throw new UnsupportedOperationException();
324 	}
325 }