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  package org.sat4j.reader;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.net.URL;
25  import java.util.Locale;
26  
27  import org.sat4j.specs.ContradictionException;
28  import org.sat4j.specs.IProblem;
29  import org.sat4j.specs.ISolver;
30  
31  /**
32   * An reader having the responsability to choose the right reader according to
33   * the input.
34   * 
35   * @author leberre
36   */
37  public class CSPInstanceReader extends InstanceReader {
38  
39      private CSPReader csp;
40  
41      private CSPReader csp2;
42  
43      private CSPReader csp3;
44  
45      private XMLCSPReader xmlcsp;
46  
47      private Reader reader = null;
48  
49      private final ISolver solver;
50  
51      public CSPInstanceReader(ISolver solver) {
52          super(solver);
53          this.solver = solver;
54      }
55  
56  
57      private Reader getCSPReader1() {
58          if (csp == null) {
59              csp = new CSPReader(solver);
60          }
61          return csp;
62      }
63  
64      private Reader getCSPReader2() {
65          if (csp2 == null) {
66              csp2 = new CSPSupportReader(solver);
67          }
68          return csp2;
69      }
70  
71      private Reader getCSPReader3() {
72          if (csp3 == null) {
73              csp3 = new CSPExtSupportReader(solver);
74          }
75          return csp3;
76      }
77  
78      private Reader getXMLCSPReader() {
79          if (xmlcsp == null) {
80              xmlcsp = new XMLCSPReader(solver);
81          }
82          return xmlcsp;
83      }
84  
85   
86      @Override
87      public IProblem parseInstance(String filename)
88              throws FileNotFoundException, ParseFormatException, IOException,
89              ContradictionException {
90          String fname;
91          boolean isHttp = false;
92          String tempFileName = "";
93          String prefix = "";
94  
95          if (filename.startsWith("http://")) {
96              isHttp = true;
97              tempFileName = filename;
98              filename = filename.substring(filename.lastIndexOf('/'), filename
99                      .length() - 1);
100         }
101 
102         if (filename.indexOf(':') != -1) {
103 
104             String[] parts = filename.split(":");
105             filename = parts[1];
106             prefix = parts[0].toUpperCase(Locale.getDefault());
107 
108         }
109 
110         if (filename.endsWith(".gz")) {
111             fname = filename.substring(0, filename.lastIndexOf('.'));
112         } else {
113             fname = filename;
114         }
115         if ("CSP".equals(prefix)) {
116             reader = getCSPReader1();
117         } else if ("CSP3".equals(prefix)) {
118             reader = getCSPReader3();
119         } else if (fname.endsWith(".txt") || "CSP2".equals(prefix)) {
120             reader = getCSPReader2();
121         } if (fname.endsWith(".xml")) {
122             reader = getXMLCSPReader();
123         } else {
124             return super.parseInstance(filename);
125         }
126             
127         if (isHttp) {
128             return reader.parseInstance((new URL(tempFileName)).openStream());
129         }
130         return reader.parseInstance(filename);
131     }
132 
133     @Override
134     @Deprecated
135     public String decode(int[] model) {
136         return reader.decode(model);
137     }
138 
139     @Override
140     public void decode(int[] model, PrintWriter out) {
141         reader.decode(model, out);
142     }
143 
144     @Override
145     public IProblem parseInstance(java.io.Reader in)
146             throws ParseFormatException, ContradictionException, IOException {
147         throw new UnsupportedOperationException();
148     }
149 }