1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.sat4j.maxsat.reader;
20
21 import java.io.IOException;
22 import java.io.LineNumberReader;
23 import java.util.StringTokenizer;
24
25 import org.sat4j.maxsat.WeightedMaxSatDecorator;
26 import org.sat4j.pb.IPBSolver;
27 import org.sat4j.reader.DimacsReader;
28 import org.sat4j.reader.ParseFormatException;
29
30
31
32
33
34
35
36 public class WDimacsReader extends DimacsReader {
37
38
39
40
41 private static final long serialVersionUID = 1L;
42
43 public WDimacsReader(IPBSolver solver) {
44 super(solver,"wcnf");
45 }
46
47 public WDimacsReader(IPBSolver solver, String format) {
48 super(solver, format);
49 }
50
51 @Override
52 protected void readProblemLine(LineNumberReader in) throws IOException,
53 ParseFormatException {
54 String line = in.readLine();
55
56 if (line == null) {
57 throw new ParseFormatException(
58 "premature end of file: <p cnf ...> expected on line "
59 + in.getLineNumber());
60 }
61 StringTokenizer stk = new StringTokenizer(line);
62
63 if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
64 && stk.hasMoreTokens() && stk.nextToken().equals(formatString))) {
65 throw new ParseFormatException(
66 "problem line expected (p cnf ...) on line "
67 + in.getLineNumber());
68 }
69
70 int vars;
71
72
73 vars = Integer.parseInt(stk.nextToken());
74 assert vars > 0;
75 solver.newVar(vars);
76
77 expectedNbOfConstr = Integer.parseInt(stk.nextToken());
78 assert expectedNbOfConstr > 0;
79 solver.setExpectedNumberOfClauses(expectedNbOfConstr);
80
81 if ("wcnf".equals(formatString)) {
82
83 int top;
84 if (stk.hasMoreTokens()) {
85 top = Integer.parseInt(stk.nextToken());
86 } else {
87 top = Integer.MAX_VALUE;
88 }
89 ((WeightedMaxSatDecorator)solver).setTopWeight(top);
90 }
91 }
92
93
94 }