View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
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   *******************************************************************************/
28  package org.sat4j.reader;
29  
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.PrintWriter;
35  import java.net.URL;
36  import java.util.zip.GZIPInputStream;
37  
38  import org.sat4j.specs.ContradictionException;
39  import org.sat4j.specs.IProblem;
40  
41  /**
42   * A reader is responsible to feed an ISolver from a text file and to convert
43   * the model found by the solver to a textual representation.
44   * 
45   * @author leberre
46   */
47  public abstract class Reader {
48  
49  	/**
50  	 * This is the usual method to feed a solver with a benchmark.
51  	 * 
52  	 * @param filename
53  	 *            the fully qualified name of the benchmark. The filename
54  	 *            extension may by used to detect which type of benchmarks it is
55  	 *            (SAT, OPB, MAXSAT, etc).
56  	 * @return the problem to solve (an ISolver in fact).
57  	 * @throws FileNotFoundException
58  	 *             if the file cannot be found.
59  	 * @throws ParseFormatException
60  	 *             if an error occurs during parsing.
61  	 * @throws IOException
62  	 *             if an I/O error occurs.
63  	 * @throws ContradictionException
64  	 *             if the problem is found trivially inconsistent.
65  	 */
66  	public IProblem parseInstance(final String filename)
67  			throws FileNotFoundException, ParseFormatException, IOException,
68  			ContradictionException {
69  		InputStream in = null;
70  		try {
71  			if (filename.startsWith("http://")) {
72  				in = (new URL(filename)).openStream();
73  			} else {
74  				in = new FileInputStream(filename);
75  			}
76  			if (filename.endsWith(".gz")) {
77  				in = new GZIPInputStream(in);
78  			} else if (filename.endsWith(".bz2")) {
79  				in = Runtime.getRuntime().exec("bunzip2 -c " + filename)
80  						.getInputStream();
81  			}
82  			IProblem problem;
83  			problem = parseInstance(in);
84  			return problem;
85  		} catch (FileNotFoundException e) {
86  			throw e;
87  		} catch (ParseFormatException e) {
88  			throw e;
89  		} catch (IOException e) {
90  			throw e;
91  		} catch (ContradictionException e) {
92  			throw e;
93  		} finally {
94  			if (in != null) {
95  				in.close();
96  			}
97  		}
98  	}
99  
100 	/**
101 	 * Read a file from a stream.
102 	 * 
103 	 * It is important to note that benchmarks are usually encoded in ASCII, not
104 	 * UTF8. As such, the only reasonable way to feed a solver from a stream is
105 	 * to use a stream.
106 	 * 
107 	 * @param in
108 	 *            a stream containing the benchmark.
109 	 * @return the problem to solve (an ISolver in fact).
110 	 * @throws ParseFormatException
111 	 *             if an error occurs during parsing.
112 	 * @throws IOException
113 	 *             if an I/O error occurs.
114 	 * @throws ContradictionException
115 	 *             if the problem is found trivially inconsistent.
116 	 */
117 	public abstract IProblem parseInstance(final InputStream in)
118 			throws ParseFormatException, ContradictionException, IOException;
119 
120 	/**
121 	 * Read a file from a reader.
122 	 * 
123 	 * Do not use that method, it is no longer supported.
124 	 * 
125 	 * @param in
126 	 *            a stream containing the benchmark.
127 	 * @return the problem to solve (an ISolver in fact).
128 	 * @throws ParseFormatException
129 	 *             if an error occurs during parsing.
130 	 * @throws IOException
131 	 *             if an I/O error occurs.
132 	 * @throws ContradictionException
133 	 *             if the problem is found trivially inconsistent.
134 	 * @see #parseInstance(InputStream)
135 	 */
136 	@Deprecated
137 	public IProblem parseInstance(java.io.Reader in)
138 			throws ParseFormatException, ContradictionException, IOException {
139 		throw new UnsupportedOperationException(
140 				"Use #parseInstance(InputStream) instead");
141 	}
142 
143 	/**
144 	 * Produce a model using the reader format.
145 	 * 
146 	 * @param model
147 	 *            a model using the Dimacs format.
148 	 * @return a human readable view of the model.
149 	 */
150 	@Deprecated
151 	public abstract String decode(int[] model);
152 
153 	/**
154 	 * Produce a model using the reader format on a provided printwriter.
155 	 * 
156 	 * @param model
157 	 *            a model using the Dimacs format.
158 	 * @param out
159 	 *            the place where to display the model
160 	 */
161 	public abstract void decode(int[] model, PrintWriter out);
162 
163 	public boolean isVerbose() {
164 		return verbose;
165 	}
166 
167 	public void setVerbosity(boolean b) {
168 		verbose = b;
169 	}
170 
171 	private boolean verbose = false;
172 }