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.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.specs.ContradictionException;
39  import org.sat4j.specs.IProblem;
40  import org.sat4j.specs.ISolver;
41  
42  /**
43   * An reader having the responsability to choose the right reader according to
44   * the input.
45   * 
46   * @author leberre
47   */
48  public class InstanceReader extends Reader {
49  
50      private AAGReader aag;
51  
52      private AIGReader aig;
53  
54      private DimacsReader ezdimacs;
55  
56      private LecteurDimacs dimacs;
57  
58      private Reader reader = null;
59  
60      private final ISolver solver;
61  
62      public InstanceReader(ISolver solver) {
63          // dimacs = new DimacsReader(solver);
64          this.solver = solver;
65      }
66  
67      private Reader getDefaultSATReader() {
68          if (this.dimacs == null) {
69              this.dimacs = new LecteurDimacs(this.solver);// new
70                                                           // LecteurDimacs(solver);
71          }
72          return this.dimacs;
73      }
74  
75      private Reader getEZSATReader() {
76          if (this.ezdimacs == null) {
77              this.ezdimacs = new DimacsReader(this.solver);// new
78                                                            // LecteurDimacs(solver);
79          }
80          return this.ezdimacs;
81      }
82  
83      private Reader getAIGReader() {
84          if (this.aig == null) {
85              this.aig = new AIGReader(this.solver);
86          }
87          return this.aig;
88      }
89  
90      private Reader getAAGReader() {
91          if (this.aag == null) {
92              this.aag = new AAGReader(this.solver);
93          }
94          return this.aag;
95      }
96  
97      @Override
98      public IProblem parseInstance(String filename)
99              throws FileNotFoundException, ParseFormatException, IOException,
100             ContradictionException {
101         String fname;
102         boolean isHttp = false;
103         String tempFileName = "";
104         String prefix = "";
105 
106         if (filename.startsWith("http://")) {
107             isHttp = true;
108             tempFileName = filename;
109             filename = filename.substring(filename.lastIndexOf('/'),
110                     filename.length() - 1);
111         }
112 
113         if (filename.indexOf(':') != -1) {
114 
115             String[] parts = filename.split(":");
116             filename = parts[1];
117             prefix = parts[0].toUpperCase(Locale.getDefault());
118 
119         }
120 
121         if (filename.endsWith(".gz")) {
122             fname = filename.substring(0, filename.lastIndexOf('.'));
123         } else {
124             fname = filename;
125         }
126         if ("EZCNF".equals(prefix)) {
127             this.reader = getEZSATReader();
128         } else if (fname.endsWith(".aag")) {
129             this.reader = getAAGReader();
130         } else if (fname.endsWith(".aig")) {
131             this.reader = getAIGReader();
132 
133         } else {
134             this.reader = getDefaultSATReader();
135         }
136 
137         if (isHttp) {
138             return this.reader
139                     .parseInstance(new URL(tempFileName).openStream());
140         }
141         return this.reader.parseInstance(filename);
142     }
143 
144     @Override
145     @Deprecated
146     public String decode(int[] model) {
147         return this.reader.decode(model);
148     }
149 
150     @Override
151     public void decode(int[] model, PrintWriter out) {
152         this.reader.decode(model, out);
153     }
154 
155     @Override
156     public IProblem parseInstance(java.io.InputStream in)
157             throws ParseFormatException, ContradictionException, IOException {
158         throw new UnsupportedOperationException(
159                 "Use a domain specific Reader (LecteurDimacs, AIGReader, etc.) for stream input ");
160     }
161 
162 }