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