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.Scanner;
24 import java.util.StringTokenizer;
25
26 import org.sat4j.maxsat.MinCostDecorator;
27 import org.sat4j.reader.DimacsReader;
28 import org.sat4j.reader.ParseFormatException;
29
30
31
32
33
34
35
36 public class P2DimacsReader extends DimacsReader {
37
38
39
40
41 private static final long serialVersionUID = 1L;
42
43 public P2DimacsReader(MinCostDecorator solver) {
44 super(solver,"p2cnf");
45 }
46
47 @Override
48 protected void readProblemLine(LineNumberReader in) throws IOException,
49 ParseFormatException {
50 String line = in.readLine();
51
52 if (line == null) {
53 throw new ParseFormatException(
54 "premature end of file: <p cnf ...> expected on line "
55 + in.getLineNumber());
56 }
57 StringTokenizer stk = new StringTokenizer(line);
58
59 if (!(stk.hasMoreTokens() && stk.nextToken().equals("p")
60 && stk.hasMoreTokens() && stk.nextToken().equals(formatString))) {
61 throw new ParseFormatException(
62 "problem line expected (p cnf ...) on line "
63 + in.getLineNumber());
64 }
65
66 int vars;
67
68
69 vars = Integer.parseInt(stk.nextToken());
70 assert vars > 0;
71 solver.newVar(vars);
72
73 expectedNbOfConstr = Integer.parseInt(stk.nextToken());
74 assert expectedNbOfConstr > 0;
75 solver.setExpectedNumberOfClauses(expectedNbOfConstr);
76
77 if ("p2cnf".equals(formatString)) {
78
79 String optLine = in.readLine();
80 if (!optLine.startsWith("min: ")) {
81 throw new ParseFormatException("p2 file does not contain the function to minimize!");
82 }
83 String optfunction = optLine.substring(5);
84 MinCostDecorator mysolver = (MinCostDecorator)solver;
85 int var, coeff;
86 Scanner values = new Scanner(optfunction);
87 while (values.hasNext()) {
88 coeff = values.nextInt();
89 assert values.hasNext();
90 var = values.nextInt();
91 mysolver.setCost(var, coeff);
92 }
93 }
94 }
95
96
97 }