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 GoodOPBReader 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  
63      private Reader getDefaultOPBReader() {
64          if (opb == null) {
65              opb = new GoodOPBReader(solver);
66          }
67          return opb;
68      }
69  
70  
71      @Override
72      public IProblem parseInstance(String filename)
73              throws FileNotFoundException, ParseFormatException, IOException,
74              ContradictionException {
75          String fname;
76          boolean isHttp = false;
77          String tempFileName = "";
78          String prefix = "";
79  
80          if (filename.startsWith("http://")) {
81              isHttp = true;
82              tempFileName = filename;
83              filename = filename.substring(filename.lastIndexOf('/'), filename
84                      .length() - 1);
85          }
86  
87          if (filename.indexOf(':') != -1) {
88  
89              String[] parts = filename.split(":");
90              filename = parts[1];
91              prefix = parts[0].toUpperCase(Locale.getDefault());
92  
93          }
94  
95          if (filename.endsWith(".gz")) {
96              fname = filename.substring(0, filename.lastIndexOf('.'));
97          } else {
98              fname = filename;
99          }
100         if (fname.endsWith(".opb") || "PB".equals(prefix)) {
101             reader = getDefaultOPBReader();
102         } else {
103             return super.parseInstance(filename);
104         }
105 
106         if (isHttp) {
107             return reader.parseInstance((new URL(tempFileName)).openStream());
108         }
109         return reader.parseInstance(filename);
110     }
111 
112     @Override
113     @Deprecated
114     public String decode(int[] model) {
115         return reader.decode(model);
116     }
117 
118     @Override
119     public void decode(int[] model, PrintWriter out) {
120         reader.decode(model, out);
121     }
122 
123     @Override
124     public IProblem parseInstance(java.io.Reader in)
125             throws ParseFormatException, ContradictionException, IOException {
126         throw new UnsupportedOperationException();
127     }
128 }