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 pseudo boolean algorithms described in:
20   * A fast pseudo-Boolean constraint solver Chai, D.; Kuehlmann, A.
21   * Computer-Aided Design of Integrated Circuits and Systems, IEEE Transactions on
22   * Volume 24, Issue 3, March 2005 Page(s): 305 - 317
23   * 
24   * and 
25   * Heidi E. Dixon, 2004. Automating Pseudo-Boolean Inference within a DPLL 
26   * Framework. Ph.D. Dissertation, University of Oregon.
27   *******************************************************************************/
28  package org.sat4j.pb.reader;
29  
30  import java.io.FileNotFoundException;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.net.URL;
34  import java.util.Locale;
35  
36  import org.sat4j.pb.IPBSolver;
37  import org.sat4j.reader.InstanceReader;
38  import org.sat4j.reader.ParseFormatException;
39  import org.sat4j.reader.Reader;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.IProblem;
42  
43  /**
44   * An reader having the responsibility to choose the right reader according to
45   * the input.
46   * 
47   * @author leberre
48   */
49  public class PBInstanceReader extends InstanceReader {
50  
51  	private OPBReader2007 opb;
52  
53  	private Reader reader = null;
54  
55  	private final IPBSolver solver;
56  
57  	public PBInstanceReader(IPBSolver solver) {
58  		super(solver);
59  		this.solver = solver;
60  	}
61  
62  	private Reader getDefaultOPBReader() {
63  		if (opb == null) {
64  			opb = new OPBReader2007(solver);
65  		}
66  		return opb;
67  	}
68  
69  	@Override
70  	public IProblem parseInstance(String filename)
71  			throws FileNotFoundException, ParseFormatException, IOException,
72  			ContradictionException {
73  		String fname;
74  		boolean isHttp = false;
75  		String tempFileName = "";
76  		String prefix = "";
77  
78  		if (filename.startsWith("http://")) {
79  			isHttp = true;
80  			tempFileName = filename;
81  			filename = filename.substring(filename.lastIndexOf('/'), filename
82  					.length() - 1);
83  		}
84  
85  		if (filename.indexOf(':') != -1) {
86  
87  			String[] parts = filename.split(":");
88  			filename = parts[1];
89  			prefix = parts[0].toUpperCase(Locale.getDefault());
90  
91  		}
92  
93  		if (filename.endsWith(".gz")) {
94  			fname = filename.substring(0, filename.lastIndexOf('.'));
95  		} else {
96  			fname = filename;
97  		}
98  		if (fname.endsWith(".opb") || "PB".equals(prefix)) {
99  			reader = getDefaultOPBReader();
100 		} else {
101 			return super.parseInstance(filename);
102 		}
103 
104 		if (isHttp) {
105 			return reader.parseInstance((new URL(tempFileName)).openStream());
106 		}
107 		return reader.parseInstance(filename);
108 	}
109 
110 	@Override
111 	@Deprecated
112 	public String decode(int[] model) {
113 		return reader.decode(model);
114 	}
115 
116 	@Override
117 	public void decode(int[] model, PrintWriter out) {
118 		reader.decode(model, out);
119 	}
120 
121 	@Override
122 	public IProblem parseInstance(java.io.Reader in)
123 			throws ParseFormatException, ContradictionException, IOException {
124 		throw new UnsupportedOperationException();
125 	}
126 }