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.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
32
33
34
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 }