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.math.BigInteger;
23
24 import org.sat4j.maxsat.WeightedMaxSatDecorator;
25 import org.sat4j.reader.DimacsReader;
26 import org.sat4j.reader.ParseFormatException;
27 import org.sat4j.specs.ContradictionException;
28
29
30
31
32
33
34
35 public class WDimacsReader extends DimacsReader {
36
37 protected BigInteger weight;
38 protected BigInteger top;
39
40 @Override
41 protected void flushConstraint() throws ContradictionException {
42 decorator.addSoftClause(weight, literals);
43 }
44
45 @Override
46 protected boolean handleLine() throws ContradictionException, IOException, ParseFormatException {
47 weight = scanner.nextBigInteger();
48 return super.handleLine();
49 }
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56 private final WeightedMaxSatDecorator decorator;
57
58 public WDimacsReader(WeightedMaxSatDecorator solver) {
59 super(solver,"wcnf");
60 decorator = solver;
61 }
62
63 public WDimacsReader(WeightedMaxSatDecorator solver, String format) {
64 super(solver, format);
65 decorator = solver;
66 }
67
68 @Override
69 protected void readProblemLine() throws IOException,
70 ParseFormatException {
71 String line = scanner.nextLine().trim();
72
73 if (line == null) {
74 throw new ParseFormatException(
75 "premature end of file: <p cnf ...> expected");
76 }
77 String[] tokens = line.split("\\s+");
78 if (tokens.length < 4 || !"p".equals(tokens[0])
79 || !formatString.equals(tokens[1])) {
80 throw new ParseFormatException("problem line expected (p cnf ...)");
81 }
82
83 int vars;
84
85
86 vars = Integer.parseInt(tokens[2]);
87 assert vars > 0;
88 solver.newVar(vars);
89
90 expectedNbOfConstr = Integer.parseInt(tokens[3]);
91 assert expectedNbOfConstr > 0;
92 solver.setExpectedNumberOfClauses(expectedNbOfConstr);
93
94 if ("wcnf".equals(formatString)) {
95
96 if (tokens.length==5) {
97 top = new BigInteger(tokens[4]);
98 } else {
99 top = WeightedMaxSatDecorator.SAT4J_MAX_BIG_INTEGER;
100 }
101 decorator.setTopWeight(top);
102 }
103 }
104
105
106 }