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