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