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