1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.sat4j.pb.reader;
29
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.PrintWriter;
33 import java.net.URL;
34 import java.util.Locale;
35
36 import org.sat4j.pb.IPBSolver;
37 import org.sat4j.reader.InstanceReader;
38 import org.sat4j.reader.ParseFormatException;
39 import org.sat4j.reader.Reader;
40 import org.sat4j.specs.ContradictionException;
41 import org.sat4j.specs.IProblem;
42
43
44
45
46
47
48
49 public class PBInstanceReader extends InstanceReader {
50
51 private GoodOPBReader opb;
52
53 private Reader reader = null;
54
55 private final IPBSolver solver;
56
57 public PBInstanceReader(IPBSolver solver) {
58 super(solver);
59 this.solver = solver;
60 }
61
62
63 private Reader getDefaultOPBReader() {
64 if (opb == null) {
65 opb = new GoodOPBReader(solver);
66 }
67 return opb;
68 }
69
70
71 @Override
72 public IProblem parseInstance(String filename)
73 throws FileNotFoundException, ParseFormatException, IOException,
74 ContradictionException {
75 String fname;
76 boolean isHttp = false;
77 String tempFileName = "";
78 String prefix = "";
79
80 if (filename.startsWith("http://")) {
81 isHttp = true;
82 tempFileName = filename;
83 filename = filename.substring(filename.lastIndexOf('/'), filename
84 .length() - 1);
85 }
86
87 if (filename.indexOf(':') != -1) {
88
89 String[] parts = filename.split(":");
90 filename = parts[1];
91 prefix = parts[0].toUpperCase(Locale.getDefault());
92
93 }
94
95 if (filename.endsWith(".gz")) {
96 fname = filename.substring(0, filename.lastIndexOf('.'));
97 } else {
98 fname = filename;
99 }
100 if (fname.endsWith(".opb") || "PB".equals(prefix)) {
101 reader = getDefaultOPBReader();
102 } else {
103 return super.parseInstance(filename);
104 }
105
106 if (isHttp) {
107 return reader.parseInstance((new URL(tempFileName)).openStream());
108 }
109 return reader.parseInstance(filename);
110 }
111
112 @Override
113 @Deprecated
114 public String decode(int[] model) {
115 return reader.decode(model);
116 }
117
118 @Override
119 public void decode(int[] model, PrintWriter out) {
120 reader.decode(model, out);
121 }
122
123 @Override
124 public IProblem parseInstance(java.io.Reader in)
125 throws ParseFormatException, ContradictionException, IOException {
126 throw new UnsupportedOperationException();
127 }
128 }