|
1 |
| package org.sat4j.reader; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.LineNumberReader; |
|
5 |
| import java.util.StringTokenizer; |
|
6 |
| |
|
7 |
| import org.sat4j.core.VecInt; |
|
8 |
| import org.sat4j.specs.ContradictionException; |
|
9 |
| import org.sat4j.specs.ISolver; |
|
10 |
| import org.sat4j.specs.IVecInt; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| @Deprecated |
|
18 |
| public class CardDimacsReader extends DimacsReader { |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| private static final long serialVersionUID = 3258130241376368435L; |
|
24 |
| |
|
25 |
0
| public CardDimacsReader(ISolver solver) {
|
|
26 |
0
| super(solver);
|
|
27 |
| } |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
0
| @Override
|
|
40 |
| protected void readConstrs(LineNumberReader in) throws IOException, |
|
41 |
| ParseFormatException, ContradictionException { |
|
42 |
0
| int lit;
|
|
43 |
0
| String line;
|
|
44 |
0
| StringTokenizer stk;
|
|
45 |
| |
|
46 |
0
| int realNbOfClauses = 0;
|
|
47 |
| |
|
48 |
0
| IVecInt literals = new VecInt();
|
|
49 |
| |
|
50 |
0
| while (true) {
|
|
51 |
0
| line = in.readLine();
|
|
52 |
| |
|
53 |
0
| if (line == null) {
|
|
54 |
| |
|
55 |
0
| if (literals.size() > 0) {
|
|
56 |
| |
|
57 |
0
| solver.addClause(literals);
|
|
58 |
0
| realNbOfClauses++;
|
|
59 |
| } |
|
60 |
| |
|
61 |
0
| break;
|
|
62 |
| } |
|
63 |
| |
|
64 |
0
| if (line.startsWith("c ")) {
|
|
65 |
| |
|
66 |
0
| continue;
|
|
67 |
| } |
|
68 |
0
| if (line.startsWith("%") && expectedNbOfConstr == realNbOfClauses) {
|
|
69 |
0
| System.out
|
|
70 |
| .println("Ignoring the rest of the file (SATLIB format"); |
|
71 |
0
| break;
|
|
72 |
| } |
|
73 |
0
| stk = new StringTokenizer(line);
|
|
74 |
0
| String token;
|
|
75 |
| |
|
76 |
0
| while (stk.hasMoreTokens()) {
|
|
77 |
| |
|
78 |
0
| token = stk.nextToken();
|
|
79 |
| |
|
80 |
0
| if ("<=".equals(token) || ">=".equals(token)) {
|
|
81 |
| |
|
82 |
0
| readCardinalityConstr(token, stk, literals);
|
|
83 |
0
| literals.clear();
|
|
84 |
0
| realNbOfClauses++;
|
|
85 |
| } else { |
|
86 |
0
| lit = Integer.parseInt(token);
|
|
87 |
0
| if (lit == 0) {
|
|
88 |
0
| if (literals.size() > 0) {
|
|
89 |
0
| solver.addClause(literals);
|
|
90 |
0
| literals.clear();
|
|
91 |
0
| realNbOfClauses++;
|
|
92 |
| } |
|
93 |
| } else { |
|
94 |
0
| literals.push(lit);
|
|
95 |
| } |
|
96 |
| } |
|
97 |
| } |
|
98 |
| } |
|
99 |
0
| if (expectedNbOfConstr != realNbOfClauses) {
|
|
100 |
0
| throw new ParseFormatException("wrong nbclauses parameter. Found "
|
|
101 |
| + realNbOfClauses + ", " + expectedNbOfConstr + " expected"); |
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
0
| private void readCardinalityConstr(String token, StringTokenizer stk,
|
|
106 |
| IVecInt literals) throws ContradictionException, |
|
107 |
| ParseFormatException { |
|
108 |
0
| int card = Integer.parseInt(stk.nextToken());
|
|
109 |
0
| int lit = Integer.parseInt(stk.nextToken());
|
|
110 |
0
| if (lit == 0) {
|
|
111 |
0
| if ("<=".equals(token)) {
|
|
112 |
0
| solver.addAtMost(literals, card);
|
|
113 |
0
| } else if (">=".equals(token)) {
|
|
114 |
0
| solver.addAtLeast(literals, card);
|
|
115 |
| } |
|
116 |
| } else |
|
117 |
0
| throw new ParseFormatException();
|
|
118 |
| } |
|
119 |
| |
|
120 |
| } |