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