View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  package org.sat4j.tools;
26  
27  import java.io.Serializable;
28  
29  import org.sat4j.specs.ContradictionException;
30  
31  /**
32   * Converts Dimacs problems in array format (without the terminating 0) to
33   * Dimacs Strings.
34   * 
35   * Adaptation of org.sat4j.reader.DimacsReader.
36   * 
37   * @author dlb
38   * @author or
39   * @author fuhs
40   */
41  public class DimacsArrayToDimacsConverter implements Serializable {
42  
43      private static final long serialVersionUID = 1L;
44  
45      // counter for the number of clauses that occur in the SAT instance
46      protected int clauses;
47  
48      protected StringBuilder dimacs; // stores the dimacs string while under
49                                      // construction
50  
51      private final int bufSize; // initial capacity of dimacs
52  
53      public DimacsArrayToDimacsConverter(int bufSize) {
54          this.bufSize = bufSize;
55      }
56  
57      protected boolean handleConstr(int gateType, int output, int[] inputs)
58              throws ContradictionException {
59          for (int var : inputs) {
60              this.dimacs.append(var);
61              this.dimacs.append(" ");
62          }
63          this.dimacs.append("0\n");
64          ++this.clauses;
65          return true;
66      }
67  
68      /**
69       * @param gateType
70       *            gateType[i] is the type of gate i according to the Extended
71       *            Dimacs specs; ignored in DimacsArrayReader, but important for
72       *            inheriting classes
73       * @param outputs
74       *            outputs[i] is the number of the output; ignored in
75       *            DimacsArrayReader
76       * @param inputs
77       *            inputs[i] contains the clauses in DimacsArrayReader; an
78       *            overriding class might have it contain the inputs of the
79       *            current gate
80       * @param maxVar
81       *            the maximum number of assigned ids
82       * @throws ContradictionException
83       *             si le probleme est trivialement inconsitant
84       */
85      public String parseInstance(int[] gateType, int[] outputs, int[][] inputs,
86              int maxVar) throws ContradictionException {
87          init();
88          this.dimacs.append(maxVar);
89          this.dimacs.append(" ");
90  
91          // the first character to be replaced is saved
92          // (7 = "p cnf ".length() + " ".length())
93          int firstCharPos = 7 + Integer.toString(maxVar).length();
94  
95          this.dimacs.append("                    ");
96          // 20 blanks; if the number of clauses ever exceeds 10^21-1, this needs
97          // to be altered. But that would require BigIntegers anyway.
98  
99          this.dimacs.append("\n");
100         for (int i = 0; i < outputs.length; ++i) {
101             handleConstr(gateType[i], outputs[i], inputs[i]);
102         }
103         String numClauses = Integer.toString(this.clauses);
104         int numClausesLength = numClauses.length();
105         for (int i = 0; i < numClausesLength; ++i) {
106             this.dimacs.setCharAt(firstCharPos + i, numClauses.charAt(i));
107         }
108         String result = this.dimacs.toString();
109         this.dimacs = null; // let the garbage collector at it
110         return result;
111     }
112 
113     protected void init() {
114         this.dimacs = new StringBuilder(this.bufSize);
115         this.dimacs.append("p cnf ");
116         this.clauses = 0;
117     }
118 
119     public String decode(int[] model) {
120         StringBuilder stb = new StringBuilder();
121         for (int i = 0; i < model.length; i++) {
122             stb.append(model[i]);
123             stb.append(" ");
124         }
125         stb.append("0");
126         return stb.toString();
127     }
128 }