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 |
| package org.sat4j.reader; |
27 |
| |
28 |
| import java.io.IOException; |
29 |
| import java.io.LineNumberReader; |
30 |
| import java.io.Serializable; |
31 |
| import java.util.Scanner; |
32 |
| import java.util.StringTokenizer; |
33 |
| |
34 |
| import org.sat4j.core.VecInt; |
35 |
| import org.sat4j.specs.ContradictionException; |
36 |
| import org.sat4j.specs.IProblem; |
37 |
| import org.sat4j.specs.ISolver; |
38 |
| import org.sat4j.specs.IVecInt; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| public class DimacsReader extends Reader implements Serializable { |
62 |
| |
63 |
| private static final long serialVersionUID = 1L; |
64 |
| |
65 |
| protected int expectedNbOfConstr; |
66 |
| |
67 |
| protected final ISolver solver; |
68 |
| |
69 |
| private boolean checkConstrNb = true; |
70 |
| |
71 |
| private final String formatString; |
72 |
| |
73 |
0
| public DimacsReader(ISolver solver) {
|
74 |
0
| this(solver,"cnf");
|
75 |
| } |
76 |
| |
77 |
0
| public DimacsReader(ISolver solver,String format) {
|
78 |
0
| this.solver = solver;
|
79 |
0
| formatString = format;
|
80 |
| } |
81 |
| |
82 |
0
| public void disableNumberOfConstraintCheck() {
|
83 |
0
| checkConstrNb = false;
|
84 |
| } |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
0
| protected void skipComments(final LineNumberReader in) throws IOException {
|
93 |
0
| int c;
|
94 |
| |
95 |
0
| do {
|
96 |
0
| in.mark(4);
|
97 |
0
| c = in.read();
|
98 |
0
| if (c == 'c') {
|
99 |
0
| in.readLine();
|
100 |
| } else { |
101 |
0
| in.reset();
|
102 |
| } |
103 |
0
| } while (c == 'c');
|
104 |
| } |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
0
| protected void readProblemLine(LineNumberReader in) throws IOException,
|
113 |
| ParseFormatException { |
114 |
| |
115 |
0
| String line = in.readLine();
|
116 |
| |
117 |
0
| if (line == null) {
|
118 |
0
| throw new ParseFormatException(
|
119 |
| "premature end of file: <p cnf ...> expected on line " |
120 |
| + in.getLineNumber()); |
121 |
| } |
122 |
0
| StringTokenizer stk = new StringTokenizer(line);
|
123 |
| |
124 |
0
| if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
|
125 |
| && stk.hasMoreTokens() && stk.nextToken().equals(formatString))) { |
126 |
0
| throw new ParseFormatException(
|
127 |
| "problem line expected (p cnf ...) on line " |
128 |
| + in.getLineNumber()); |
129 |
| } |
130 |
| |
131 |
0
| int vars;
|
132 |
| |
133 |
| |
134 |
0
| vars = Integer.parseInt(stk.nextToken());
|
135 |
| assert vars > 0; |
136 |
0
| solver.newVar(vars);
|
137 |
| |
138 |
0
| expectedNbOfConstr = Integer.parseInt(stk.nextToken());
|
139 |
| assert expectedNbOfConstr > 0; |
140 |
0
| solver.setExpectedNumberOfClauses(expectedNbOfConstr);
|
141 |
| |
142 |
| } |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
0
| protected void readConstrs(LineNumberReader in) throws IOException,
|
153 |
| ParseFormatException, ContradictionException { |
154 |
0
| String line;
|
155 |
| |
156 |
0
| int realNbOfConstr = 0;
|
157 |
| |
158 |
0
| IVecInt literals = new VecInt();
|
159 |
| |
160 |
0
| while (true) {
|
161 |
0
| line = in.readLine();
|
162 |
| |
163 |
0
| if (line == null) {
|
164 |
| |
165 |
0
| if (literals.size() > 0) {
|
166 |
| |
167 |
0
| solver.addClause(literals);
|
168 |
0
| realNbOfConstr++;
|
169 |
| } |
170 |
| |
171 |
0
| break;
|
172 |
| } |
173 |
| |
174 |
0
| if (line.startsWith("c ")) {
|
175 |
| |
176 |
0
| System.out.println("Found commmented line : " + line);
|
177 |
0
| continue;
|
178 |
| } |
179 |
0
| if (line.startsWith("%") && expectedNbOfConstr == realNbOfConstr) {
|
180 |
0
| System.out
|
181 |
| .println("Ignoring the rest of the file (SATLIB format"); |
182 |
0
| break;
|
183 |
| } |
184 |
0
| boolean added = handleConstr(line, literals);
|
185 |
0
| if (added) {
|
186 |
0
| realNbOfConstr++;
|
187 |
| } |
188 |
| } |
189 |
0
| if (checkConstrNb && expectedNbOfConstr != realNbOfConstr) {
|
190 |
0
| throw new ParseFormatException("wrong nbclauses parameter. Found "
|
191 |
| + realNbOfConstr + ", " + expectedNbOfConstr + " expected"); |
192 |
| } |
193 |
| } |
194 |
| |
195 |
0
| protected boolean handleConstr(String line, IVecInt literals)
|
196 |
| throws ContradictionException { |
197 |
0
| int lit;
|
198 |
0
| boolean added = false;
|
199 |
0
| Scanner scan;
|
200 |
0
| scan = new Scanner(line);
|
201 |
0
| while (scan.hasNext()) {
|
202 |
0
| lit = scan.nextInt();
|
203 |
| |
204 |
0
| if (lit == 0) {
|
205 |
0
| if (literals.size() > 0) {
|
206 |
0
| solver.addClause(literals);
|
207 |
0
| literals.clear();
|
208 |
0
| added = true;
|
209 |
| } |
210 |
| } else { |
211 |
0
| literals.push(lit);
|
212 |
| } |
213 |
| } |
214 |
0
| return added;
|
215 |
| } |
216 |
| |
217 |
0
| @Override
|
218 |
| public final IProblem parseInstance(final java.io.Reader in) |
219 |
| throws ParseFormatException, ContradictionException, IOException { |
220 |
0
| return parseInstance(new LineNumberReader(in));
|
221 |
| |
222 |
| } |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
0
| private IProblem parseInstance(LineNumberReader in) throws ParseFormatException,
|
232 |
| ContradictionException { |
233 |
0
| solver.reset();
|
234 |
0
| try {
|
235 |
0
| skipComments(in);
|
236 |
0
| readProblemLine(in);
|
237 |
0
| readConstrs(in);
|
238 |
0
| return solver;
|
239 |
| } catch (IOException e) { |
240 |
0
| throw new ParseFormatException(e);
|
241 |
| } catch (NumberFormatException e) { |
242 |
0
| throw new ParseFormatException("integer value expected on line "
|
243 |
| + in.getLineNumber(), e); |
244 |
| } |
245 |
| } |
246 |
| |
247 |
0
| @Override
|
248 |
| public String decode(int[] model) { |
249 |
0
| StringBuffer stb = new StringBuffer();
|
250 |
0
| for (int i = 0; i < model.length; i++) {
|
251 |
0
| stb.append(model[i]);
|
252 |
0
| stb.append(" ");
|
253 |
| } |
254 |
0
| stb.append("0");
|
255 |
0
| return stb.toString();
|
256 |
| } |
257 |
| |
258 |
0
| protected ISolver getSolver() {
|
259 |
0
| return solver;
|
260 |
| } |
261 |
| } |