1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
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 }