View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.tools;
31  
32  import java.io.PrintStream;
33  import java.io.PrintWriter;
34  import java.util.Map;
35  
36  import org.sat4j.specs.ContradictionException;
37  import org.sat4j.specs.IConstr;
38  import org.sat4j.specs.ISolver;
39  import org.sat4j.specs.ISolverService;
40  import org.sat4j.specs.IVec;
41  import org.sat4j.specs.IVecInt;
42  import org.sat4j.specs.SearchListener;
43  import org.sat4j.specs.TimeoutException;
44  import org.sat4j.specs.UnitClauseProvider;
45  
46  /**
47   * The aim of that class is to allow adding dynamic responsibilities to SAT
48   * solvers using the Decorator design pattern. The class is abstract because it
49   * does not makes sense to use it "as is".
50   * 
51   * @author leberre
52   */
53  public abstract class SolverDecorator<T extends ISolver> implements ISolver {
54  
55      /**
56  	 * 
57  	 */
58      private static final long serialVersionUID = 1L;
59  
60      public boolean isDBSimplificationAllowed() {
61          return this.solver.isDBSimplificationAllowed();
62      }
63  
64      public void setDBSimplificationAllowed(boolean status) {
65          this.solver.setDBSimplificationAllowed(status);
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see org.sat4j.specs.ISolver#setTimeoutOnConflicts(int)
72       */
73      public void setTimeoutOnConflicts(int count) {
74          this.solver.setTimeoutOnConflicts(count);
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see org.sat4j.specs.IProblem#printInfos(java.io.PrintWriter,
81       * java.lang.String)
82       */
83      public void printInfos(PrintWriter out, String prefix) {
84          this.solver.printInfos(out, prefix);
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see org.sat4j.specs.IProblem#printInfos(java.io.PrintWriter,
91       * java.lang.String)
92       */
93      public void printInfos(PrintWriter out) {
94          this.solver.printInfos(out);
95      }
96  
97      /*
98       * (non-Javadoc)
99       * 
100      * @see org.sat4j.specs.IProblem#isSatisfiable(boolean)
101      */
102     public boolean isSatisfiable(boolean global) throws TimeoutException {
103         return this.solver.isSatisfiable(global);
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see org.sat4j.specs.IProblem#isSatisfiable(org.sat4j.specs.IVecInt,
110      * boolean)
111      */
112     public boolean isSatisfiable(IVecInt assumps, boolean global)
113             throws TimeoutException {
114         return this.solver.isSatisfiable(assumps, global);
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see org.sat4j.specs.ISolver#clearLearntClauses()
121      */
122     public void clearLearntClauses() {
123         this.solver.clearLearntClauses();
124     }
125 
126     /*
127      * (non-Javadoc)
128      * 
129      * @see org.sat4j.specs.IProblem#findModel()
130      */
131     public int[] findModel() throws TimeoutException {
132         return this.solver.findModel();
133     }
134 
135     /*
136      * (non-Javadoc)
137      * 
138      * @see org.sat4j.specs.IProblem#findModel(org.sat4j.specs.IVecInt)
139      */
140     public int[] findModel(IVecInt assumps) throws TimeoutException {
141         return this.solver.findModel(assumps);
142     }
143 
144     /*
145      * (non-Javadoc)
146      * 
147      * @see org.sat4j.specs.IProblem#model(int)
148      */
149     public boolean model(int var) {
150         return this.solver.model(var);
151     }
152 
153     public void setExpectedNumberOfClauses(int nb) {
154         this.solver.setExpectedNumberOfClauses(nb);
155     }
156 
157     /*
158      * (non-Javadoc)
159      * 
160      * @see org.sat4j.specs.ISolver#getTimeout()
161      */
162     public int getTimeout() {
163         return this.solver.getTimeout();
164     }
165 
166     /**
167      * @since 2.1
168      */
169     public long getTimeoutMs() {
170         return this.solver.getTimeoutMs();
171     }
172 
173     /*
174      * (non-Javadoc)
175      * 
176      * @see org.sat4j.specs.ISolver#toString(java.lang.String)
177      */
178     public String toString(String prefix) {
179         return this.solver.toString(prefix);
180     }
181 
182     @Override
183     public String toString() {
184         return toString("");
185     }
186 
187     /*
188      * (non-Javadoc)
189      * 
190      * @see org.sat4j.specs.ISolver#printStat(java.io.PrintStream,
191      * java.lang.String)
192      */
193     @Deprecated
194     public void printStat(PrintStream out, String prefix) {
195         this.solver.printStat(out, prefix);
196     }
197 
198     public void printStat(PrintWriter out, String prefix) {
199         this.solver.printStat(out, prefix);
200     }
201 
202     public void printStat(PrintWriter out) {
203         this.solver.printStat(out);
204     }
205 
206     private T solver;
207 
208     /**
209      * 
210      */
211     public SolverDecorator(T solver) {
212         this.solver = solver;
213     }
214 
215     @Deprecated
216     public int newVar() {
217         return this.solver.newVar();
218     }
219 
220     /*
221      * (non-Javadoc)
222      * 
223      * @see org.sat4j.ISolver#newVar(int)
224      */
225     public int newVar(int howmany) {
226         return this.solver.newVar(howmany);
227     }
228 
229     /*
230      * (non-Javadoc)
231      * 
232      * @see org.sat4j.ISolver#addClause(org.sat4j.datatype.VecInt)
233      */
234     public IConstr addClause(IVecInt literals) throws ContradictionException {
235         return this.solver.addClause(literals);
236     }
237 
238     public void addAllClauses(IVec<IVecInt> clauses)
239             throws ContradictionException {
240         this.solver.addAllClauses(clauses);
241     }
242 
243     /**
244      * @since 2.1
245      */
246     public IConstr addBlockingClause(IVecInt literals)
247             throws ContradictionException {
248         return this.solver.addBlockingClause(literals);
249     }
250 
251     /*
252      * (non-Javadoc)
253      * 
254      * @see org.sat4j.ISolver#addAtMost(org.sat4j.datatype.VecInt, int)
255      */
256     public IConstr addAtMost(IVecInt literals, int degree)
257             throws ContradictionException {
258         return this.solver.addAtMost(literals, degree);
259     }
260 
261     /*
262      * (non-Javadoc)
263      * 
264      * @see org.sat4j.ISolver#addAtLeast(org.sat4j.datatype.VecInt, int)
265      */
266     public IConstr addAtLeast(IVecInt literals, int degree)
267             throws ContradictionException {
268         return this.solver.addAtLeast(literals, degree);
269     }
270 
271     /*
272      * (non-Javadoc)
273      * 
274      * @see org.sat4j.ISolver#model()
275      */
276     public int[] model() {
277         return this.solver.model();
278     }
279 
280     /*
281      * (non-Javadoc)
282      * 
283      * @see org.sat4j.ISolver#isSatisfiable()
284      */
285     public boolean isSatisfiable() throws TimeoutException {
286         return this.solver.isSatisfiable();
287     }
288 
289     /*
290      * (non-Javadoc)
291      * 
292      * @see org.sat4j.ISolver#isSatisfiable(org.sat4j.datatype.VecInt)
293      */
294     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
295         return this.solver.isSatisfiable(assumps);
296     }
297 
298     /*
299      * (non-Javadoc)
300      * 
301      * @see org.sat4j.ISolver#setTimeout(int)
302      */
303     public void setTimeout(int t) {
304         this.solver.setTimeout(t);
305     }
306 
307     /*
308      * (non-Javadoc)
309      * 
310      * @see org.sat4j.ISolver#setTimeoutMs(int)
311      */
312     public void setTimeoutMs(long t) {
313         this.solver.setTimeoutMs(t);
314     }
315 
316     /*
317      * (non-Javadoc)
318      * 
319      * @see org.sat4j.ISolver#expireTimeout()
320      */
321     public void expireTimeout() {
322         this.solver.expireTimeout();
323     }
324 
325     /*
326      * (non-Javadoc)
327      * 
328      * @see org.sat4j.ISolver#nConstraints()
329      */
330     public int nConstraints() {
331         return this.solver.nConstraints();
332     }
333 
334     /*
335      * (non-Javadoc)
336      * 
337      * @see org.sat4j.ISolver#nVars()
338      */
339     public int nVars() {
340         return this.solver.nVars();
341     }
342 
343     /*
344      * (non-Javadoc)
345      * 
346      * @see org.sat4j.ISolver#reset()
347      */
348     public void reset() {
349         this.solver.reset();
350     }
351 
352     public T decorated() {
353         return this.solver;
354     }
355 
356     /**
357      * Method to be called to clear the decorator from its decorated solver.
358      * This is especially useful to avoid memory leak in a program.
359      * 
360      * @return the decorated solver.
361      */
362     public T clearDecorated() {
363         T decorated = this.solver;
364         this.solver = null;
365         return decorated;
366     }
367 
368     /*
369      * (non-Javadoc)
370      * 
371      * @see org.sat4j.specs.ISolver#removeConstr(org.sat4j.minisat.core.Constr)
372      */
373     public boolean removeConstr(IConstr c) {
374         return this.solver.removeConstr(c);
375     }
376 
377     /*
378      * (non-Javadoc)
379      * 
380      * @see org.sat4j.specs.ISolver#getStat()
381      */
382     public Map<String, Number> getStat() {
383         return this.solver.getStat();
384     }
385 
386     /**
387      * @since 2.1
388      */
389     public <S extends ISolverService> void setSearchListener(
390             SearchListener<S> sl) {
391         this.solver.setSearchListener(sl);
392     }
393 
394     /**
395      * @since 2.1
396      */
397     public int nextFreeVarId(boolean reserve) {
398         return this.solver.nextFreeVarId(reserve);
399     }
400 
401     /**
402      * @since 2.1
403      */
404     public boolean removeSubsumedConstr(IConstr c) {
405         return this.solver.removeSubsumedConstr(c);
406     }
407 
408     /**
409      * @since 2.2
410      */
411     public <S extends ISolverService> SearchListener<S> getSearchListener() {
412         return this.solver.getSearchListener();
413     }
414 
415     /**
416      * @since 2.2
417      */
418     public boolean isVerbose() {
419         return this.solver.isVerbose();
420     }
421 
422     /**
423      * @since 2.2
424      */
425     public void setVerbose(boolean value) {
426         this.solver.setVerbose(value);
427     }
428 
429     /**
430      * @since 2.2
431      */
432     public void setLogPrefix(String prefix) {
433         this.solver.setLogPrefix(prefix);
434     }
435 
436     /**
437      * @since 2.2
438      */
439     public String getLogPrefix() {
440         return this.solver.getLogPrefix();
441     }
442 
443     /**
444      * @since 2.2
445      */
446     public IVecInt unsatExplanation() {
447         return this.solver.unsatExplanation();
448     }
449 
450     /**
451      * @since 2.3
452      */
453     public int[] primeImplicant() {
454         return this.solver.primeImplicant();
455     }
456 
457     /**
458      * @since 2.3.1
459      */
460     public IConstr addExactly(IVecInt literals, int n)
461             throws ContradictionException {
462         return this.solver.addExactly(literals, n);
463     }
464 
465     /**
466      * @since 2.3.1
467      */
468     public int[] modelWithInternalVariables() {
469         return this.solver.modelWithInternalVariables();
470     }
471 
472     /**
473      * @since 2.3.1
474      */
475     public int realNumberOfVariables() {
476         return this.solver.realNumberOfVariables();
477     }
478 
479     /**
480      * @since 2.3.1
481      */
482     public void registerLiteral(int p) {
483         this.solver.registerLiteral(p);
484     }
485 
486     /**
487      * @since 2.3.2
488      */
489     public boolean isSolverKeptHot() {
490         return this.solver.isSolverKeptHot();
491     }
492 
493     /**
494      * @since 2.3.2
495      */
496     public void setKeepSolverHot(boolean value) {
497         this.solver.setKeepSolverHot(value);
498     }
499 
500     /**
501      * @since 2.3.2
502      */
503     public boolean primeImplicant(int p) {
504         return this.solver.primeImplicant(p);
505     }
506 
507     /**
508      * @since 2.3.3
509      */
510     public ISolver getSolvingEngine() {
511         return this.solver.getSolvingEngine();
512     }
513 
514     /**
515      * @since 2.3.4
516      */
517     public void setUnitClauseProvider(UnitClauseProvider ucp) {
518         this.solver.setUnitClauseProvider(ucp);
519     }
520 
521 }