View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le
3    * Berre
4    * 
5    * Based on the original minisat specification from:
6    * 
7    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
8    * Sixth International Conference on Theory and Applications of Satisfiability
9    * Testing, LNCS 2919, pp 502-518, 2003.
10   * 
11   * This library is free software; you can redistribute it and/or modify it under
12   * the terms of the GNU Lesser General Public License as published by the Free
13   * Software Foundation; either version 2.1 of the License, or (at your option)
14   * any later version.
15   * 
16   * This library is distributed in the hope that it will be useful, but WITHOUT
17   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19   * details.
20   * 
21   * You should have received a copy of the GNU Lesser General Public License
22   * along with this library; if not, write to the Free Software Foundation, Inc.,
23   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24   * 
25   */
26  
27  package org.sat4j.tools;
28  
29  import java.io.ObjectInputStream;
30  import java.io.PrintStream;
31  import java.io.PrintWriter;
32  import java.math.BigInteger;
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   * Solver used to display in a writer the CNF instance in Dimacs format.
44   * 
45   * That solver is useful to produce CNF files to be used by third party solvers.
46   * 
47   * @author leberre
48   * 
49   */
50  public class DimacsOutputSolver implements ISolver {
51  
52      /**
53       * 
54       */
55      private static final long serialVersionUID = 1L;
56  
57      private transient PrintWriter out;
58  
59      private int nbvars;
60  
61      private int nbclauses;
62  
63      private boolean fixedNbClauses = false;
64  
65      private boolean firstConstr = true;
66      
67      public DimacsOutputSolver() {
68          this(new PrintWriter(System.out, true));
69      }
70  
71      public DimacsOutputSolver(PrintWriter pw) {
72          out = pw;
73      }
74  
75      private void readObject(ObjectInputStream stream) {
76          out = new PrintWriter(System.out, true);
77      }
78  
79      public int newVar() {
80          return 0;
81      }
82  
83      public int newVar(int howmany) {
84          out.print("p cnf " + howmany);
85          nbvars = howmany;
86          return 0;
87      }
88  
89      public void setExpectedNumberOfClauses(int nb) {
90          out.println(" " + nb);
91          nbclauses = nb;
92          fixedNbClauses = true;
93      }
94  
95      public IConstr addClause(IVecInt literals) throws ContradictionException {
96          if (firstConstr) {
97              if (!fixedNbClauses) {
98                  out.println(" XXXXXX");
99              }
100             firstConstr = false;
101         }
102         for (int i : literals)
103             out.print(i + " ");
104         out.println("0");
105         return null;        
106     }
107 
108     public boolean removeConstr(IConstr c) {
109         throw new UnsupportedOperationException();
110     }
111 
112     public void addAllClauses(IVec<IVecInt> clauses)
113             throws ContradictionException {
114         throw new UnsupportedOperationException();
115     }
116 
117     public IConstr addAtMost(IVecInt literals, int degree)
118             throws ContradictionException {
119         if (degree > 1) {
120             throw new UnsupportedOperationException(
121                     "Not a clausal problem! degree " + degree);
122         }
123         assert degree == 1;
124         if (firstConstr) {
125             if (!fixedNbClauses) {
126                 out.println("XXXXXX");
127             }
128             firstConstr = false;
129         }
130         for (int i = 0; i <= literals.size(); i++) {
131             for (int j = i + 1; j < literals.size(); j++) {
132                 out.println("" + (-literals.get(i)) + " " + (-literals.get(j))
133                         + " 0");
134             }
135         }
136         return null;
137     }
138 
139     public IConstr addAtLeast(IVecInt literals, int degree)
140             throws ContradictionException {
141         if (degree > 1) {
142             throw new UnsupportedOperationException(
143                     "Not a clausal problem! degree " + degree);
144         }
145         assert degree == 1;
146         return addClause(literals);
147     }
148 
149     public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
150             boolean moreThan, BigInteger d) throws ContradictionException {
151         throw new UnsupportedOperationException();
152     }
153 
154     public void setTimeout(int t) {
155         // TODO Auto-generated method stub
156 
157     }
158 
159     public void setTimeoutMs(long t) {
160         // TODO Auto-generated method stub
161     }
162 
163     public int getTimeout() {
164         // TODO Auto-generated method stub
165         return 0;
166     }
167 
168     public void reset() {
169         fixedNbClauses = false;
170         firstConstr  = true;
171 
172     }
173 
174     public void printStat(PrintStream out, String prefix) {
175         // TODO Auto-generated method stub
176 
177     }
178 
179     public void printStat(PrintWriter out, String prefix) {
180         // TODO Auto-generated method stub
181 
182     }
183 
184     public Map<String, Number> getStat() {
185         // TODO Auto-generated method stub
186         return null;
187     }
188 
189     public String toString(String prefix) {
190         return "Dimacs output solver";
191     }
192 
193     public void clearLearntClauses() {
194         // TODO Auto-generated method stub
195 
196     }
197 
198     public int[] model() {
199         throw new UnsupportedOperationException();
200     }
201 
202     public boolean model(int var) {
203         throw new UnsupportedOperationException();
204     }
205 
206     public boolean isSatisfiable() throws TimeoutException {
207         throw new TimeoutException("There is no real solver behind!");
208     }
209 
210     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
211         throw new TimeoutException("There is no real solver behind!");
212     }
213 
214     public int[] findModel() throws TimeoutException {
215         throw new UnsupportedOperationException();
216     }
217 
218     public int[] findModel(IVecInt assumps) throws TimeoutException {
219         throw new UnsupportedOperationException();
220     }
221 
222     public int nConstraints() {
223         return nbclauses;
224     }
225 
226     public int nVars() {
227         // TODO Auto-generated method stub
228         return nbvars;
229     }
230 
231 }