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.ObjectInputStream;
31  import java.io.PrintStream;
32  import java.io.PrintWriter;
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.IteratorInt;
41  import org.sat4j.specs.TimeoutException;
42  
43  /**
44   * Solver used to display in a writer the CNF instance in Dimacs format.
45   * 
46   * That solver is useful to produce CNF files to be used by third party solvers.
47   * 
48   * @author leberre
49   * 
50   */
51  public class DimacsOutputSolver implements ISolver {
52  
53      /**
54       * 
55       */
56      private static final long serialVersionUID = 1L;
57  
58      private transient PrintWriter out;
59  
60      private int nbvars;
61  
62      private int nbclauses;
63  
64      private boolean fixedNbClauses = false;
65  
66      private boolean firstConstr = true;
67      
68      public DimacsOutputSolver() {
69          this(new PrintWriter(System.out, true));
70      }
71  
72      public DimacsOutputSolver(PrintWriter pw) {
73          out = pw;
74      }
75  
76      private void readObject(ObjectInputStream stream) {
77          out = new PrintWriter(System.out, true);
78      }
79  
80      public int newVar() {
81          return 0;
82      }
83  
84      public int newVar(int howmany) {
85          out.print("p cnf " + howmany);
86          nbvars = howmany;
87          return 0;
88      }
89  
90      public void setExpectedNumberOfClauses(int nb) {
91          out.println(" " + nb);
92          nbclauses = nb;
93          fixedNbClauses = true;
94      }
95  
96      public IConstr addClause(IVecInt literals) throws ContradictionException {
97          if (firstConstr) {
98              if (!fixedNbClauses) {
99                  out.println(" XXXXXX");
100             }
101             firstConstr = false;
102         }
103         for (IteratorInt iterator = literals.iterator();iterator.hasNext();)
104             out.print(iterator.next() + " ");
105         out.println("0");
106         return null;        
107     }
108 
109     public boolean removeConstr(IConstr c) {
110         throw new UnsupportedOperationException();
111     }
112 
113     public void addAllClauses(IVec<IVecInt> clauses)
114             throws ContradictionException {
115         throw new UnsupportedOperationException();
116     }
117 
118     public IConstr addAtMost(IVecInt literals, int degree)
119             throws ContradictionException {
120         if (degree > 1) {
121             throw new UnsupportedOperationException(
122                     "Not a clausal problem! degree " + degree);
123         }
124         assert degree == 1;
125         if (firstConstr) {
126             if (!fixedNbClauses) {
127                 out.println("XXXXXX");
128             }
129             firstConstr = false;
130         }
131         for (int i = 0; i <= literals.size(); i++) {
132             for (int j = i + 1; j < literals.size(); j++) {
133                 out.println("" + (-literals.get(i)) + " " + (-literals.get(j))
134                         + " 0");
135             }
136         }
137         return null;
138     }
139 
140     public IConstr addAtLeast(IVecInt literals, int degree)
141             throws ContradictionException {
142         if (degree > 1) {
143             throw new UnsupportedOperationException(
144                     "Not a clausal problem! degree " + degree);
145         }
146         assert degree == 1;
147         return addClause(literals);
148     }
149 
150     public void setTimeout(int t) {
151         // TODO Auto-generated method stub
152     }
153 
154     public void setTimeoutMs(long t) {
155         // TODO Auto-generated method stub
156     }
157 
158     public int getTimeout() {
159         return 0;
160     }
161 
162     public long getTimeoutMs() {
163         return 0L;
164     }
165     
166     public void reset() {
167         fixedNbClauses = false;
168         firstConstr  = true;
169 
170     }
171 
172     public void printStat(PrintStream output, String prefix) {
173         // TODO Auto-generated method stub
174 
175     }
176 
177     public void printStat(PrintWriter output, String prefix) {
178         // TODO Auto-generated method stub
179 
180     }
181 
182     public Map<String, Number> getStat() {
183         // TODO Auto-generated method stub
184         return null;
185     }
186 
187     public String toString(String prefix) {
188         return "Dimacs output solver";
189     }
190 
191     public void clearLearntClauses() {
192         // TODO Auto-generated method stub
193 
194     }
195 
196     public int[] model() {
197         throw new UnsupportedOperationException();
198     }
199 
200     public boolean model(int var) {
201         throw new UnsupportedOperationException();
202     }
203 
204     public boolean isSatisfiable() throws TimeoutException {
205         throw new TimeoutException("There is no real solver behind!");
206     }
207 
208     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
209         throw new TimeoutException("There is no real solver behind!");
210     }
211 
212     public int[] findModel() throws TimeoutException {
213         throw new UnsupportedOperationException();
214     }
215 
216     public int[] findModel(IVecInt assumps) throws TimeoutException {
217         throw new UnsupportedOperationException();
218     }
219 
220     public int nConstraints() {
221         return nbclauses;
222     }
223 
224     public int nVars() {
225        return nbvars;
226     }
227 
228     public void expireTimeout() {
229         // TODO Auto-generated method stub
230         
231     }
232 
233     public boolean isSatisfiable(IVecInt assumps, boolean global)
234             throws TimeoutException {
235         throw new TimeoutException("There is no real solver behind!");
236     }
237 
238     public boolean isSatisfiable(boolean global) throws TimeoutException {
239         throw new TimeoutException("There is no real solver behind!");
240     }
241 
242     public void printInfos(PrintWriter output, String prefix) {
243     }
244 
245     public void setTimeoutOnConflicts(int count) {        
246     }
247 
248 	public boolean isDBSimplificationAllowed() {
249 		return false;
250 	}
251 
252 	public void setDBSimplificationAllowed(boolean status) {
253 	}
254 
255 }