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 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 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 } catch (IllegalStateException e) { 94 if (e.getCause() instanceof ContradictionException) { 95 throw ((ContradictionException) e.getCause()); 96 } else { 97 throw e; 98 } 99 } finally { 100 if (in != null) { 101 in.close(); 102 } 103 } 104 } 105 106 /** 107 * Read a file from a stream. 108 * 109 * It is important to note that benchmarks are usually encoded in ASCII, not 110 * UTF8. As such, the only reasonable way to feed a solver from a stream is 111 * to use a stream. 112 * 113 * @param in 114 * a stream containing the benchmark. 115 * @return the problem to solve (an ISolver in fact). 116 * @throws ParseFormatException 117 * if an error occurs during parsing. 118 * @throws IOException 119 * if an I/O error occurs. 120 * @throws ContradictionException 121 * if the problem is found trivially inconsistent. 122 */ 123 public abstract IProblem parseInstance(final InputStream in) 124 throws ParseFormatException, ContradictionException, IOException; 125 126 /** 127 * Read a file from a reader. 128 * 129 * Do not use that method, it is no longer supported. 130 * 131 * @param in 132 * a stream containing the benchmark. 133 * @return the problem to solve (an ISolver in fact). 134 * @throws ParseFormatException 135 * if an error occurs during parsing. 136 * @throws IOException 137 * if an I/O error occurs. 138 * @throws ContradictionException 139 * if the problem is found trivially inconsistent. 140 * @see #parseInstance(InputStream) 141 */ 142 @Deprecated 143 public IProblem parseInstance(java.io.Reader in) 144 throws ParseFormatException, ContradictionException, IOException { 145 throw new UnsupportedOperationException( 146 "Use #parseInstance(InputStream) instead"); 147 } 148 149 /** 150 * Produce a model using the reader format. 151 * 152 * @param model 153 * a model using the Dimacs format. 154 * @return a human readable view of the model. 155 */ 156 @Deprecated 157 public abstract String decode(int[] model); 158 159 /** 160 * Produce a model using the reader format on a provided printwriter. 161 * 162 * @param model 163 * a model using the Dimacs format. 164 * @param out 165 * the place where to display the model 166 */ 167 public abstract void decode(int[] model, PrintWriter out); 168 169 public boolean isVerbose() { 170 return this.verbose; 171 } 172 173 public void setVerbosity(boolean b) { 174 this.verbose = b; 175 } 176 177 private boolean verbose = false; 178 }