View Javadoc

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.pb.reader;
31  
32  import java.io.FileNotFoundException;
33  import java.io.IOException;
34  import java.io.PrintWriter;
35  import java.net.URL;
36  import java.util.Locale;
37  
38  import org.sat4j.pb.IPBSolver;
39  import org.sat4j.reader.InstanceReader;
40  import org.sat4j.reader.ParseFormatException;
41  import org.sat4j.reader.Reader;
42  import org.sat4j.specs.ContradictionException;
43  import org.sat4j.specs.IProblem;
44  
45  /**
46   * An reader having the responsibility to choose the right reader according to
47   * the input.
48   * 
49   * @author leberre
50   */
51  public class PBInstanceReader extends InstanceReader {
52  
53      private OPBReader2007 opb;
54  
55      private Reader reader = null;
56  
57      private final IPBSolver solver;
58  
59      public PBInstanceReader(IPBSolver solver) {
60          super(solver);
61          this.solver = solver;
62      }
63  
64      private Reader getDefaultOPBReader() {
65          if (this.opb == null) {
66              this.opb = new OPBReader2007(this.solver);
67          }
68          return this.opb;
69      }
70  
71      public boolean hasObjectiveFunction() {
72          return this.opb.hasObjFunc;
73      }
74  
75      @Override
76      public IProblem parseInstance(String filename)
77              throws FileNotFoundException, ParseFormatException, IOException,
78              ContradictionException {
79          String fname;
80          boolean isHttp = false;
81          String tempFileName = "";
82          String prefix = "";
83  
84          if (filename.startsWith("http://")) {
85              isHttp = true;
86              tempFileName = filename;
87              filename = filename.substring(filename.lastIndexOf('/'),
88                      filename.length() - 1);
89          }
90  
91          if (filename.indexOf(':') != -1) {
92  
93              String[] parts = filename.split(":");
94              filename = parts[1];
95              prefix = parts[0].toUpperCase(Locale.getDefault());
96  
97          }
98  
99          if (filename.endsWith(".gz")) {
100             fname = filename.substring(0, filename.lastIndexOf('.'));
101         } else {
102             fname = filename;
103         }
104         if (fname.endsWith(".opb") || "PB".equals(prefix)) {
105             this.reader = getDefaultOPBReader();
106         } else {
107             return super.parseInstance(filename);
108         }
109 
110         if (isHttp) {
111             return this.reader
112                     .parseInstance(new URL(tempFileName).openStream());
113         }
114         return this.reader.parseInstance(filename);
115     }
116 
117     @Override
118     @Deprecated
119     public String decode(int[] model) {
120         return this.reader.decode(model);
121     }
122 
123     @Override
124     public void decode(int[] model, PrintWriter out) {
125         this.reader.decode(model, out);
126     }
127 }