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.util.Map;
33  
34  import org.sat4j.specs.ContradictionException;
35  import org.sat4j.specs.IConstr;
36  import org.sat4j.specs.ISolver;
37  import org.sat4j.specs.IVec;
38  import org.sat4j.specs.IVecInt;
39  import org.sat4j.specs.IteratorInt;
40  import org.sat4j.specs.TimeoutException;
41  
42  /**
43   * Solver used to write down a CNF into a String.
44   * 
45   * It is especially useful compared to the DimacsOutputSolver 
46   * because the number of clauses does not need to be known in 
47   * advance.
48   *  
49   * @author leberre
50   * 
51   */
52  public class DimacsStringSolver implements ISolver {
53  
54      /**
55       * 
56       */
57      private static final long serialVersionUID = 1L;
58  
59      private StringBuffer out;
60  
61      private int nbvars;
62  
63      private int nbclauses;
64  
65      private boolean fixedNbClauses = false;
66  
67      private boolean firstConstr = true;
68  
69      private int firstCharPos;
70      
71      private final int initBuilderSize;
72      
73      public DimacsStringSolver() {
74          this(16);
75      }
76  
77      public DimacsStringSolver(int initSize) {
78          out = new StringBuffer(initSize);
79          initBuilderSize = initSize;
80      }
81  
82      public StringBuffer getOut(){
83      	return out;
84      }
85  
86      public int newVar() {
87          return 0;
88      }
89  
90      public int newVar(int howmany) {
91          out.append("p cnf " + howmany);
92          setNbVars(howmany);
93          return howmany;
94      }
95      
96      protected void setNbVars(int howmany){
97      	nbvars = howmany;
98      }
99  
100     public void setExpectedNumberOfClauses(int nb) {
101         out.append(" " + nb);
102         nbclauses = nb;
103         fixedNbClauses = true;
104     }
105 
106     public IConstr addClause(IVecInt literals) throws ContradictionException {
107         if (firstConstr) {
108             if (!fixedNbClauses) {
109                 firstCharPos = 7 + Integer.toString(nbvars).length();
110                 out.append("                    ");
111                 out.append("\n");
112                 nbclauses=0;
113             }
114             firstConstr = false;
115         }
116         if (!fixedNbClauses) nbclauses++;
117         
118         for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
119             out.append(iterator.next()).append(" ");
120         out.append("0\n");
121         return null;        
122     }
123 
124     public boolean removeConstr(IConstr c) {
125         throw new UnsupportedOperationException();
126     }
127 
128     public void addAllClauses(IVec<IVecInt> clauses)
129             throws ContradictionException {
130         throw new UnsupportedOperationException();
131     }
132 
133     public IConstr addAtMost(IVecInt literals, int degree)
134             throws ContradictionException {
135         if (degree > 1) {
136             throw new UnsupportedOperationException(
137                     "Not a clausal problem! degree " + degree);
138         }
139         assert degree == 1;
140         if (firstConstr) {
141             if (!fixedNbClauses) {
142                 firstCharPos = 7 + Integer.toString(nbvars).length();
143                 out.append("                    ");
144                 out.append("\n");
145                 nbclauses=0;
146             }
147             firstConstr = false;
148         }
149         
150         for (int i = 0; i <= literals.size(); i++) {
151             for (int j = i + 1; j < literals.size(); j++) {
152                 if (!fixedNbClauses) nbclauses++;
153                 out.append("" + (-literals.get(i)) + " " + (-literals.get(j))
154                         + " 0\n");
155             }
156         }
157         return null;
158     }
159 
160     public IConstr addAtLeast(IVecInt literals, int degree)
161             throws ContradictionException {
162         if (degree > 1) {
163             throw new UnsupportedOperationException(
164                     "Not a clausal problem! degree " + degree);
165         }
166         assert degree == 1;
167         return addClause(literals);
168     }
169 
170     public void setTimeout(int t) {
171         // TODO Auto-generated method stub
172 
173     }
174 
175     public void setTimeoutMs(long t) {
176         // TODO Auto-generated method stub
177     }
178 
179     public int getTimeout() {
180         return 0;
181     }
182 
183     public long getTimeoutMs() {
184         return 0L;
185     }
186     
187     public void reset() {
188         fixedNbClauses = false;
189         firstConstr  = true;
190         out = new StringBuffer(initBuilderSize);
191     }
192 
193     public void printStat(PrintStream output, String prefix) {
194         // TODO Auto-generated method stub
195     }
196 
197     public void printStat(PrintWriter output, String prefix) {
198         // TODO Auto-generated method stub
199 
200     }
201 
202     public Map<String, Number> getStat() {
203         // TODO Auto-generated method stub
204         return null;
205     }
206 
207     public String toString(String prefix) {
208         return "Dimacs output solver";
209     }
210 
211     public void clearLearntClauses() {
212         // TODO Auto-generated method stub
213 
214     }
215 
216     public int[] model() {
217         throw new UnsupportedOperationException();
218     }
219 
220     public boolean model(int var) {
221         throw new UnsupportedOperationException();
222     }
223 
224     public boolean isSatisfiable() throws TimeoutException {
225         throw new TimeoutException("There is no real solver behind!");
226     }
227 
228     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
229         throw new TimeoutException("There is no real solver behind!");
230     }
231 
232     public int[] findModel() throws TimeoutException {
233         throw new UnsupportedOperationException();
234     }
235 
236     public int[] findModel(IVecInt assumps) throws TimeoutException {
237         throw new UnsupportedOperationException();
238     }
239 
240     public int nConstraints() {
241         return nbclauses;
242     }
243 
244     public int nVars() {
245         return nbvars;
246     }
247     
248     @Override
249     public String toString() {
250 //        String numClauses = Integer.toString(nbclauses);
251 //        int numClausesLength = numClauses.length();
252 //        for (int i = 0; i < numClausesLength; ++i) {
253 //            out.setCharAt(firstCharPos + i, numClauses.charAt(i));
254 //        }
255     	out.insert(firstCharPos, nbclauses);
256         return out.toString();
257     }
258 
259     public void expireTimeout() {
260         // TODO Auto-generated method stub
261         
262     }
263 
264     public boolean isSatisfiable(IVecInt assumps, boolean global)
265             throws TimeoutException {
266         throw new TimeoutException("There is no real solver behind!");
267     }
268 
269     public boolean isSatisfiable(boolean global) throws TimeoutException {
270         throw new TimeoutException("There is no real solver behind!");
271     }
272 
273     public void printInfos(PrintWriter output, String prefix) {        
274     }
275 
276     public void setTimeoutOnConflicts(int count) {
277         
278     }
279 
280 	public boolean isDBSimplificationAllowed() {
281 		return false;
282 	}
283 
284 	public void setDBSimplificationAllowed(boolean status) {
285 		
286 	}
287 
288 }