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 OPBReader2007 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 private Reader getDefaultOPBReader() {
63 if (opb == null) {
64 opb = new OPBReader2007(solver);
65 }
66 return opb;
67 }
68
69 @Override
70 public IProblem parseInstance(String filename)
71 throws FileNotFoundException, ParseFormatException, IOException,
72 ContradictionException {
73 String fname;
74 boolean isHttp = false;
75 String tempFileName = "";
76 String prefix = "";
77
78 if (filename.startsWith("http://")) {
79 isHttp = true;
80 tempFileName = filename;
81 filename = filename.substring(filename.lastIndexOf('/'), filename
82 .length() - 1);
83 }
84
85 if (filename.indexOf(':') != -1) {
86
87 String[] parts = filename.split(":");
88 filename = parts[1];
89 prefix = parts[0].toUpperCase(Locale.getDefault());
90
91 }
92
93 if (filename.endsWith(".gz")) {
94 fname = filename.substring(0, filename.lastIndexOf('.'));
95 } else {
96 fname = filename;
97 }
98 if (fname.endsWith(".opb") || "PB".equals(prefix)) {
99 reader = getDefaultOPBReader();
100 } else {
101 return super.parseInstance(filename);
102 }
103
104 if (isHttp) {
105 return reader.parseInstance((new URL(tempFileName)).openStream());
106 }
107 return reader.parseInstance(filename);
108 }
109
110 @Override
111 @Deprecated
112 public String decode(int[] model) {
113 return reader.decode(model);
114 }
115
116 @Override
117 public void decode(int[] model, PrintWriter out) {
118 reader.decode(model, out);
119 }
120
121 @Override
122 public IProblem parseInstance(java.io.Reader in)
123 throws ParseFormatException, ContradictionException, IOException {
124 throw new UnsupportedOperationException();
125 }
126 }