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  
26  package org.sat4j.reader;
27  
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.InputStreamReader;
33  import java.io.PrintWriter;
34  import java.net.URL;
35  import java.util.zip.GZIPInputStream;
36  
37  import org.sat4j.specs.ContradictionException;
38  import org.sat4j.specs.IProblem;
39  
40  /**
41   * A reader is responsible to feed an ISolver from a text file and to convert
42   * the model found by the solver to a textual representation.
43   * 
44   * @author leberre
45   */
46  public abstract class Reader {
47  
48      public IProblem parseInstance(final String filename)
49              throws FileNotFoundException, ParseFormatException, IOException,
50              ContradictionException {
51          InputStream in;
52          if (filename.startsWith("http://")) {
53              in = (new URL(filename)).openStream();
54          } else {
55              in = new FileInputStream(filename);
56          }
57          if (filename.endsWith(".gz")) {
58              in = new GZIPInputStream(in);
59          }
60          return parseInstance(in);
61      }
62  
63      public IProblem parseInstance(final InputStream in)
64              throws ParseFormatException, ContradictionException, IOException {
65          return parseInstance(new InputStreamReader(in));
66      }
67  
68      public abstract IProblem parseInstance(final java.io.Reader in)
69              throws ParseFormatException, ContradictionException, IOException;
70  
71      /**
72       * Produce a model using the reader format.
73       * 
74       * @param model
75       *            a model using the Dimacs format.
76       * @return a human readable view of the model.
77       */
78      @Deprecated
79      public abstract String decode(int[] model);
80  
81      /**
82       * Produce a model using the reader format on a provided printwriter.
83       * 
84       * @param model
85       *            a model using the Dimacs format.
86       * @param out
87       *            the place where to display the model
88       */
89      public abstract void decode(int[] model, PrintWriter out);
90  
91      public boolean isVerbose() {
92          return verbose;
93      }
94  
95      public void setVerbosity(boolean b) {
96          verbose = b;
97      }
98  
99      private boolean verbose = false;
100 }