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.io.Serializable;
33  import java.util.Map;
34  
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.IConstr;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVec;
39  import org.sat4j.specs.IVecInt;
40  import org.sat4j.specs.SearchListener;
41  import org.sat4j.specs.TimeoutException;
42  
43  /**
44   * The aim of that class is to allow adding dynamic responsabilities to SAT
45   * solvers using the Decorator design pattern. The class is abstract because it
46   * does not makes sense to use it "as is".
47   * 
48   * @author leberre
49   */
50  public abstract class SolverDecorator<T extends ISolver> implements ISolver,
51  		Serializable {
52  
53  	/**
54  	 * 
55  	 */
56  	private static final long serialVersionUID = 1L;
57  
58  	public boolean isDBSimplificationAllowed() {
59  		return solver.isDBSimplificationAllowed();
60  	}
61  
62  	public void setDBSimplificationAllowed(boolean status) {
63  		solver.setDBSimplificationAllowed(status);
64  	}
65  
66  	/*
67  	 * (non-Javadoc)
68  	 * 
69  	 * @see org.sat4j.specs.ISolver#setTimeoutOnConflicts(int)
70  	 */
71  	public void setTimeoutOnConflicts(int count) {
72  		solver.setTimeoutOnConflicts(count);
73  	}
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * 
78  	 * @see org.sat4j.specs.IProblem#printInfos(java.io.PrintWriter,
79  	 * java.lang.String)
80  	 */
81  	public void printInfos(PrintWriter out, String prefix) {
82  		solver.printInfos(out, prefix);
83  	}
84  
85  	/*
86  	 * (non-Javadoc)
87  	 * 
88  	 * @see org.sat4j.specs.IProblem#isSatisfiable(boolean)
89  	 */
90  	public boolean isSatisfiable(boolean global) throws TimeoutException {
91  		return solver.isSatisfiable(global);
92  	}
93  
94  	/*
95  	 * (non-Javadoc)
96  	 * 
97  	 * @see org.sat4j.specs.IProblem#isSatisfiable(org.sat4j.specs.IVecInt,
98  	 * boolean)
99  	 */
100 	public boolean isSatisfiable(IVecInt assumps, boolean global)
101 			throws TimeoutException {
102 		return solver.isSatisfiable(assumps, global);
103 	}
104 
105 	/*
106 	 * (non-Javadoc)
107 	 * 
108 	 * @see org.sat4j.specs.ISolver#clearLearntClauses()
109 	 */
110 	public void clearLearntClauses() {
111 		solver.clearLearntClauses();
112 	}
113 
114 	/*
115 	 * (non-Javadoc)
116 	 * 
117 	 * @see org.sat4j.specs.IProblem#findModel()
118 	 */
119 	public int[] findModel() throws TimeoutException {
120 		return solver.findModel();
121 	}
122 
123 	/*
124 	 * (non-Javadoc)
125 	 * 
126 	 * @see org.sat4j.specs.IProblem#findModel(org.sat4j.specs.IVecInt)
127 	 */
128 	public int[] findModel(IVecInt assumps) throws TimeoutException {
129 		return solver.findModel(assumps);
130 	}
131 
132 	/*
133 	 * (non-Javadoc)
134 	 * 
135 	 * @see org.sat4j.specs.IProblem#model(int)
136 	 */
137 	public boolean model(int var) {
138 		return solver.model(var);
139 	}
140 
141 	public void setExpectedNumberOfClauses(int nb) {
142 		solver.setExpectedNumberOfClauses(nb);
143 	}
144 
145 	/*
146 	 * (non-Javadoc)
147 	 * 
148 	 * @see org.sat4j.specs.ISolver#getTimeout()
149 	 */
150 	public int getTimeout() {
151 		return solver.getTimeout();
152 	}
153 
154 	/**
155 	 * @since 2.1
156 	 */
157 	public long getTimeoutMs() {
158 		return solver.getTimeoutMs();
159 	}
160 
161 	/*
162 	 * (non-Javadoc)
163 	 * 
164 	 * @see org.sat4j.specs.ISolver#toString(java.lang.String)
165 	 */
166 	public String toString(String prefix) {
167 		return solver.toString(prefix);
168 	}
169 
170 	/*
171 	 * (non-Javadoc)
172 	 * 
173 	 * @see org.sat4j.specs.ISolver#printStat(java.io.PrintStream,
174 	 * java.lang.String)
175 	 */
176 	@Deprecated
177 	public void printStat(PrintStream out, String prefix) {
178 		solver.printStat(out, prefix);
179 	}
180 
181 	public void printStat(PrintWriter out, String prefix) {
182 		solver.printStat(out, prefix);
183 	}
184 
185 	private T solver;
186 
187 	/**
188      * 
189      */
190 	public SolverDecorator(T solver) {
191 		this.solver = solver;
192 	}
193 
194 	@Deprecated
195 	public int newVar() {
196 		return solver.newVar();
197 	}
198 
199 	/*
200 	 * (non-Javadoc)
201 	 * 
202 	 * @see org.sat4j.ISolver#newVar(int)
203 	 */
204 	public int newVar(int howmany) {
205 		return solver.newVar(howmany);
206 	}
207 
208 	/*
209 	 * (non-Javadoc)
210 	 * 
211 	 * @see org.sat4j.ISolver#addClause(org.sat4j.datatype.VecInt)
212 	 */
213 	public IConstr addClause(IVecInt literals) throws ContradictionException {
214 		return solver.addClause(literals);
215 	}
216 
217 	public void addAllClauses(IVec<IVecInt> clauses)
218 			throws ContradictionException {
219 		solver.addAllClauses(clauses);
220 	}
221 
222 	/**
223 	 * @since 2.1
224 	 */
225 	public IConstr addBlockingClause(IVecInt literals)
226 			throws ContradictionException {
227 		return solver.addBlockingClause(literals);
228 	}
229 
230 	/*
231 	 * (non-Javadoc)
232 	 * 
233 	 * @see org.sat4j.ISolver#addAtMost(org.sat4j.datatype.VecInt, int)
234 	 */
235 	public IConstr addAtMost(IVecInt literals, int degree)
236 			throws ContradictionException {
237 		return solver.addAtMost(literals, degree);
238 	}
239 
240 	/*
241 	 * (non-Javadoc)
242 	 * 
243 	 * @see org.sat4j.ISolver#addAtLeast(org.sat4j.datatype.VecInt, int)
244 	 */
245 	public IConstr addAtLeast(IVecInt literals, int degree)
246 			throws ContradictionException {
247 		return solver.addAtLeast(literals, degree);
248 	}
249 
250 	/*
251 	 * (non-Javadoc)
252 	 * 
253 	 * @see org.sat4j.ISolver#model()
254 	 */
255 	public int[] model() {
256 		return solver.model();
257 	}
258 
259 	/*
260 	 * (non-Javadoc)
261 	 * 
262 	 * @see org.sat4j.ISolver#isSatisfiable()
263 	 */
264 	public boolean isSatisfiable() throws TimeoutException {
265 		return solver.isSatisfiable();
266 	}
267 
268 	/*
269 	 * (non-Javadoc)
270 	 * 
271 	 * @see org.sat4j.ISolver#isSatisfiable(org.sat4j.datatype.VecInt)
272 	 */
273 	public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
274 		return solver.isSatisfiable(assumps);
275 	}
276 
277 	/*
278 	 * (non-Javadoc)
279 	 * 
280 	 * @see org.sat4j.ISolver#setTimeout(int)
281 	 */
282 	public void setTimeout(int t) {
283 		solver.setTimeout(t);
284 	}
285 
286 	/*
287 	 * (non-Javadoc)
288 	 * 
289 	 * @see org.sat4j.ISolver#setTimeoutMs(int)
290 	 */
291 	public void setTimeoutMs(long t) {
292 		solver.setTimeoutMs(t);
293 	}
294 
295 	/*
296 	 * (non-Javadoc)
297 	 * 
298 	 * @see org.sat4j.ISolver#expireTimeout()
299 	 */
300 	public void expireTimeout() {
301 		solver.expireTimeout();
302 	}
303 
304 	/*
305 	 * (non-Javadoc)
306 	 * 
307 	 * @see org.sat4j.ISolver#nConstraints()
308 	 */
309 	public int nConstraints() {
310 		return solver.nConstraints();
311 	}
312 
313 	/*
314 	 * (non-Javadoc)
315 	 * 
316 	 * @see org.sat4j.ISolver#nVars()
317 	 */
318 	public int nVars() {
319 		return solver.nVars();
320 	}
321 
322 	/*
323 	 * (non-Javadoc)
324 	 * 
325 	 * @see org.sat4j.ISolver#reset()
326 	 */
327 	public void reset() {
328 		solver.reset();
329 	}
330 
331 	public T decorated() {
332 		return solver;
333 	}
334 
335 	/**
336 	 * Method to be called to clear the decorator from its decorated solver.
337 	 * This is especially useful to avoid memory leak in a program.
338 	 * 
339 	 * @return the decorated solver.
340 	 */
341 	public T clearDecorated() {
342 		T decorated = solver;
343 		solver = null;
344 		return decorated;
345 	}
346 
347 	/*
348 	 * (non-Javadoc)
349 	 * 
350 	 * @see org.sat4j.specs.ISolver#removeConstr(org.sat4j.minisat.core.Constr)
351 	 */
352 	public boolean removeConstr(IConstr c) {
353 		return solver.removeConstr(c);
354 	}
355 
356 	/*
357 	 * (non-Javadoc)
358 	 * 
359 	 * @see org.sat4j.specs.ISolver#getStat()
360 	 */
361 	public Map<String, Number> getStat() {
362 		return solver.getStat();
363 	}
364 
365 	/**
366 	 * @since 2.1
367 	 */
368 	public void setSearchListener(SearchListener sl) {
369 		solver.setSearchListener(sl);
370 	}
371 
372 	/**
373 	 * @since 2.1
374 	 */
375 	public int nextFreeVarId(boolean reserve) {
376 		return solver.nextFreeVarId(reserve);
377 	}
378 
379 	/**
380 	 * @since 2.1
381 	 */
382 	public boolean removeSubsumedConstr(IConstr c) {
383 		return solver.removeSubsumedConstr(c);
384 	}
385 
386 }