View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j.reader;
27  
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  import java.io.PrintWriter;
31  import java.net.URL;
32  import java.util.Locale;
33  
34  import org.sat4j.specs.ContradictionException;
35  import org.sat4j.specs.IProblem;
36  import org.sat4j.specs.ISolver;
37  
38  /**
39   * An reader having the responsability to choose the right reader according to
40   * the input.
41   * 
42   * @author leberre
43   */
44  public class InstanceReader extends Reader {
45  
46      private AAGReader aag;
47  
48      private AIGReader aig;
49  
50      private DimacsReader ezdimacs;
51  
52      private LecteurDimacs dimacs;
53  
54      private GoodOPBReader opb;
55  
56      private ExtendedDimacsReader edimacs;
57  
58      private CSPReader csp;
59  
60      private CSPReader csp2;
61  
62      private CSPReader csp3;
63  
64      private XMLCSPReader xmlcsp;
65  
66      private Reader reader = null;
67  
68      private final ISolver solver;
69  
70      public InstanceReader(ISolver solver) {
71          // dimacs = new DimacsReader(solver);
72          this.solver = solver;
73      }
74  
75      private Reader getDefaultSATReader() {
76          if (dimacs == null) {
77              dimacs = new LecteurDimacs(solver);// new LecteurDimacs(solver);
78          }
79          return dimacs;
80      }
81  
82      private Reader getEZSATReader() {
83          if (ezdimacs == null) {
84              ezdimacs = new DimacsReader(solver);// new LecteurDimacs(solver);
85          }
86          return ezdimacs;
87      }
88  
89      private Reader getDefaultOPBReader() {
90          if (opb == null) {
91              opb = new GoodOPBReader(solver);
92          }
93          return opb;
94      }
95  
96      private Reader getDefaultExtendedDimacsReader() {
97          if (edimacs == null) {
98              edimacs = new ExtendedDimacsReader(solver);
99          }
100         return edimacs;
101     }
102 
103     private Reader getCSPReader1() {
104         if (csp == null) {
105             csp = new CSPReader(solver);
106         }
107         return csp;
108     }
109 
110     private Reader getCSPReader2() {
111         if (csp2 == null) {
112             csp2 = new CSPSupportReader(solver);
113         }
114         return csp2;
115     }
116 
117     private Reader getCSPReader3() {
118         if (csp3 == null) {
119             csp3 = new CSPExtSupportReader(solver);
120         }
121         return csp3;
122     }
123 
124     private Reader getXMLCSPReader() {
125         if (xmlcsp == null) {
126             xmlcsp = new XMLCSPReader(solver);
127         }
128         return xmlcsp;
129     }
130 
131     private Reader getAIGReader() {
132         if (aig == null) {
133             aig = new AIGReader(solver);
134         }
135         return aig;
136     }
137 
138     private Reader getAAGReader() {
139         if (aag == null) {
140             aag = new AAGReader(solver);
141         }
142         return aag;
143     }
144 
145     @Override
146     public IProblem parseInstance(String filename)
147             throws FileNotFoundException, ParseFormatException, IOException,
148             ContradictionException {
149         String fname;
150         boolean isHttp = false;
151         String tempFileName = "";
152         String prefix = "";
153 
154         if (filename.startsWith("http://")) {
155             isHttp = true;
156             tempFileName = filename;
157             filename = filename.substring(filename.lastIndexOf('/'), filename
158                     .length() - 1);
159         }
160 
161         if (filename.indexOf(':') != -1) {
162 
163             String[] parts = filename.split(":");
164             filename = parts[1];
165             prefix = parts[0].toUpperCase(Locale.getDefault());
166 
167         }
168 
169         if (filename.endsWith(".gz")) {
170             fname = filename.substring(0, filename.lastIndexOf('.'));
171         } else {
172             fname = filename;
173         }
174         if ("EZCNF".equals(prefix)) {
175             reader = getEZSATReader();
176         } else if ("CSP".equals(prefix)) {
177             reader = getCSPReader1();
178         } else if ("CSP3".equals(prefix)) {
179             reader = getCSPReader3();
180         } else if (fname.endsWith(".txt") || "CSP2".equals(prefix)) {
181             reader = getCSPReader2();
182         } else if (fname.endsWith(".opb") || "PB".equals(prefix)) {
183             reader = getDefaultOPBReader();
184         } else if (fname.endsWith(".edimacs") || fname.endsWith(".ncnf")
185                 || "EDIMACS".equals(prefix)) {
186             reader = getDefaultExtendedDimacsReader();
187         } else if (fname.endsWith(".xml")) {
188             reader = getXMLCSPReader();
189         } else if (fname.endsWith(".aag")) {
190             reader = getAAGReader();
191         } else if (fname.endsWith(".aig")) {
192             reader = getAIGReader();
193 
194         } else {
195             reader = getDefaultSATReader();
196         }
197 
198         if (isHttp) {
199             return reader.parseInstance((new URL(tempFileName)).openStream());
200         }
201         return reader.parseInstance(filename);
202     }
203 
204     @Override
205     public String decode(int[] model) {
206         return reader.decode(model);
207     }
208 
209     @Override
210     public void decode(int[] model, PrintWriter out) {
211         reader.decode(model, out);
212     }
213 
214     @Override
215     public IProblem parseInstance(java.io.Reader in)
216             throws ParseFormatException, ContradictionException, IOException {
217         throw new UnsupportedOperationException();
218     }
219 }