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