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.ObjectInputStream;
33  import java.io.PrintWriter;
34  
35  import org.sat4j.specs.ContradictionException;
36  import org.sat4j.specs.IConstr;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.IteratorInt;
39  
40  /**
41   * Solver used to display in a writer the CNF instance in Dimacs format.
42   * 
43   * That solver is useful to produce CNF files to be used by third party solvers.
44   * 
45   * @author leberre
46   * 
47   */
48  public class DimacsOutputSolver extends AbstractOutputSolver {
49  
50      /**
51       * 
52       */
53      private static final long serialVersionUID = 1L;
54  
55      private transient PrintWriter out;
56  
57      public DimacsOutputSolver() {
58          this(new PrintWriter(System.out, true));
59      }
60  
61      public DimacsOutputSolver(PrintWriter pw) {
62          this.out = pw;
63      }
64  
65      private void readObject(ObjectInputStream stream) {
66          this.out = new PrintWriter(System.out, true);
67      }
68  
69      public int newVar() {
70          return 0;
71      }
72  
73      @Override
74      public int newVar(int howmany) {
75          this.out.print("p cnf " + howmany);
76          this.nbvars = howmany;
77          return 0;
78      }
79  
80      public void setExpectedNumberOfClauses(int nb) {
81          this.out.println(" " + nb);
82          this.nbclauses = nb;
83          this.fixedNbClauses = true;
84      }
85  
86      public IConstr addClause(IVecInt literals) throws ContradictionException {
87          if (this.firstConstr) {
88              if (!this.fixedNbClauses) {
89                  this.out.println(" XXXXXX");
90              }
91              this.firstConstr = false;
92          }
93          for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
94              this.out.print(iterator.next() + " ");
95          }
96          this.out.println("0");
97          return null;
98      }
99  
100     public IConstr addAtMost(IVecInt literals, int degree)
101             throws ContradictionException {
102         if (degree > 1) {
103             throw new UnsupportedOperationException(
104                     "Not a clausal problem! degree " + degree);
105         }
106         assert degree == 1;
107         if (this.firstConstr) {
108             if (!this.fixedNbClauses) {
109                 this.out.println("XXXXXX");
110             }
111             this.firstConstr = false;
112         }
113         for (int i = 0; i <= literals.size(); i++) {
114             for (int j = i + 1; j < literals.size(); j++) {
115                 this.out.println("" + -literals.get(i) + " " + -literals.get(j)
116                         + " 0");
117             }
118         }
119         return null;
120     }
121 
122     public IConstr addAtLeast(IVecInt literals, int degree)
123             throws ContradictionException {
124         if (degree > 1) {
125             throw new UnsupportedOperationException(
126                     "Not a clausal problem! degree " + degree);
127         }
128         assert degree == 1;
129         return addClause(literals);
130     }
131 
132     public IConstr addExactly(IVecInt literals, int n)
133             throws ContradictionException {
134         if (n > 1) {
135             throw new UnsupportedOperationException(
136                     "Not a clausal problem! degree " + n);
137         }
138         assert n == 1;
139         addAtMost(literals, n);
140         addAtLeast(literals, n);
141         return null;
142     }
143 
144     public void reset() {
145         this.fixedNbClauses = false;
146         this.firstConstr = true;
147 
148     }
149 
150     public String toString(String prefix) {
151         return "Dimacs output solver";
152     }
153 
154     @Override
155     public int nConstraints() {
156         return this.nbclauses;
157     }
158 
159     @Override
160     public int nVars() {
161         return this.nbvars;
162     }
163 
164     /**
165      * @since 2.1
166      */
167     public int nextFreeVarId(boolean reserve) {
168         if (reserve) {
169             return ++this.nbvars;
170         }
171         return this.nbvars + 1;
172     }
173 
174     /**
175      * @since 2.3.1
176      */
177     public int[] modelWithInternalVariables() {
178         throw new UnsupportedOperationException();
179     }
180 
181     /**
182      * @since 2.3.1
183      */
184     public int realNumberOfVariables() {
185         return this.nbvars;
186     }
187 
188     /**
189      * @since 2.3.1
190      */
191     public void registerLiteral(int p) {
192         throw new UnsupportedOperationException();
193     }
194 
195     /**
196      * @since 2.3.2
197      */
198     public boolean primeImplicant(int p) {
199         throw new UnsupportedOperationException();
200     }
201 }