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.PrintWriter;
33  
34  import org.sat4j.specs.ContradictionException;
35  import org.sat4j.specs.IConstr;
36  import org.sat4j.specs.IVecInt;
37  import org.sat4j.specs.IteratorInt;
38  
39  /**
40   * Solver used to write down a CNF into a String.
41   * 
42   * It is especially useful compared to the DimacsOutputSolver because the number
43   * of clauses does not need to be known in advance.
44   * 
45   * @author leberre
46   * 
47   */
48  public class DimacsStringSolver extends AbstractOutputSolver {
49  
50      /**
51       * 
52       */
53      private static final long serialVersionUID = 1L;
54  
55      private StringBuffer out;
56  
57      private int firstCharPos;
58  
59      private final int initBuilderSize;
60  
61      private int maxvarid = 0;
62  
63      public DimacsStringSolver() {
64          this(16);
65      }
66  
67      public DimacsStringSolver(int initSize) {
68          this.out = new StringBuffer(initSize);
69          this.initBuilderSize = initSize;
70      }
71  
72      public StringBuffer getOut() {
73          return this.out;
74      }
75  
76      public int newVar() {
77          return 0;
78      }
79  
80      @Override
81      public int newVar(int howmany) {
82          setNbVars(howmany);
83          return howmany;
84      }
85  
86      protected void setNbVars(int howmany) {
87          this.nbvars = howmany;
88          this.maxvarid = howmany;
89      }
90  
91      public void setExpectedNumberOfClauses(int nb) {
92          this.out.append(" ");
93          this.out.append(nb);
94          this.nbclauses = nb;
95          this.fixedNbClauses = true;
96      }
97  
98      public IConstr addClause(IVecInt literals) throws ContradictionException {
99          if (this.firstConstr) {
100             if (!this.fixedNbClauses) {
101                 this.firstCharPos = 0;
102                 this.out.append("                    ");
103                 this.out.append("\n");
104                 this.nbclauses = 0;
105             }
106             this.firstConstr = false;
107         }
108         if (!this.fixedNbClauses) {
109             this.nbclauses++;
110         }
111         for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
112             this.out.append(iterator.next()).append(" ");
113         }
114         this.out.append("0\n");
115         return null;
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 (this.firstConstr) {
126             this.firstCharPos = 0;
127             this.out.append("                    ");
128             this.out.append("\n");
129             this.nbclauses = 0;
130             this.firstConstr = false;
131         }
132 
133         for (int i = 0; i <= literals.size(); i++) {
134             for (int j = i + 1; j < literals.size(); j++) {
135                 if (!this.fixedNbClauses) {
136                     this.nbclauses++;
137                 }
138                 this.out.append(-literals.get(i));
139                 this.out.append(" ");
140                 this.out.append(-literals.get(j));
141                 this.out.append(" 0\n");
142             }
143         }
144         return null;
145     }
146 
147     public IConstr addExactly(IVecInt literals, int n)
148             throws ContradictionException {
149         if (n > 1) {
150             throw new UnsupportedOperationException(
151                     "Not a clausal problem! degree " + n);
152         }
153         assert n == 1;
154         addAtMost(literals, n);
155         addAtLeast(literals, n);
156         return null;
157     }
158 
159     public IConstr addAtLeast(IVecInt literals, int degree)
160             throws ContradictionException {
161         if (degree > 1) {
162             throw new UnsupportedOperationException(
163                     "Not a clausal problem! degree " + degree);
164         }
165         assert degree == 1;
166         return addClause(literals);
167     }
168 
169     public void reset() {
170         this.fixedNbClauses = false;
171         this.firstConstr = true;
172         this.out = new StringBuffer(this.initBuilderSize);
173         this.maxvarid = 0;
174     }
175 
176     public String toString(String prefix) {
177         return "Dimacs output solver";
178     }
179 
180     @Override
181     public int nConstraints() {
182         return this.nbclauses;
183     }
184 
185     @Override
186     public int nVars() {
187         return this.maxvarid;
188     }
189 
190     @Override
191     public String toString() {
192         this.out.insert(this.firstCharPos, "p cnf " + this.maxvarid + " "
193                 + this.nbclauses);
194         return this.out.toString();
195     }
196 
197     /**
198      * @since 2.1
199      */
200     public int nextFreeVarId(boolean reserve) {
201         if (reserve) {
202             return ++this.maxvarid;
203         }
204         return this.maxvarid + 1;
205     }
206 
207     /**
208      * @since 2.3.1
209      */
210     public int[] modelWithInternalVariables() {
211         throw new UnsupportedOperationException();
212     }
213 
214     public int realNumberOfVariables() {
215         return this.maxvarid;
216     }
217 
218     public void registerLiteral(int p) {
219         throw new UnsupportedOperationException();
220     }
221 
222     /**
223      * @since 2.3.2
224      */
225     public boolean primeImplicant(int p) {
226         throw new UnsupportedOperationException();
227     }
228 
229     /**
230      * @since 2.3.3
231      */
232     public void printStat(PrintWriter out) {
233         throw new UnsupportedOperationException();
234 
235     }
236 
237     /**
238      * @since 2.3.3
239      */
240     public void printInfos(PrintWriter out) {
241         throw new UnsupportedOperationException();
242 
243     }
244 }