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