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 package org.sat4j.reader;
26
27 import java.io.IOException;
28 import java.io.LineNumberReader;
29 import java.util.StringTokenizer;
30
31 import org.sat4j.core.VecInt;
32 import org.sat4j.specs.ContradictionException;
33 import org.sat4j.specs.ISolver;
34 import org.sat4j.specs.IVecInt;
35
36
37
38
39
40
41 @Deprecated
42 public class CardDimacsReader extends DimacsReader {
43
44
45
46
47 private static final long serialVersionUID = 3258130241376368435L;
48
49 public CardDimacsReader(ISolver solver) {
50 super(solver);
51 }
52
53
54
55
56
57
58
59
60
61
62
63 @Override
64 protected void readConstrs(LineNumberReader in) throws IOException,
65 ParseFormatException, ContradictionException {
66 int lit;
67 String line;
68 StringTokenizer stk;
69
70 int realNbOfClauses = 0;
71
72 IVecInt literals = new VecInt();
73
74 while (true) {
75 line = in.readLine();
76
77 if (line == null) {
78
79 if (literals.size() > 0) {
80
81 solver.addClause(literals);
82 realNbOfClauses++;
83 }
84
85 break;
86 }
87
88 if (line.startsWith("c ")) {
89
90 continue;
91 }
92 if (line.startsWith("%") && expectedNbOfConstr == realNbOfClauses) {
93 System.out
94 .println("Ignoring the rest of the file (SATLIB format");
95 break;
96 }
97 stk = new StringTokenizer(line);
98 String token;
99
100 while (stk.hasMoreTokens()) {
101
102 token = stk.nextToken();
103
104 if ("<=".equals(token) || ">=".equals(token)) {
105
106 readCardinalityConstr(token, stk, literals);
107 literals.clear();
108 realNbOfClauses++;
109 } else {
110 lit = Integer.parseInt(token);
111 if (lit == 0) {
112 if (literals.size() > 0) {
113 solver.addClause(literals);
114 literals.clear();
115 realNbOfClauses++;
116 }
117 } else {
118 literals.push(lit);
119 }
120 }
121 }
122 }
123 if (expectedNbOfConstr != realNbOfClauses) {
124 throw new ParseFormatException("wrong nbclauses parameter. Found "
125 + realNbOfClauses + ", " + expectedNbOfConstr + " expected");
126 }
127 }
128
129 private void readCardinalityConstr(String token, StringTokenizer stk,
130 IVecInt literals) throws ContradictionException,
131 ParseFormatException {
132 int card = Integer.parseInt(stk.nextToken());
133 int lit = Integer.parseInt(stk.nextToken());
134 if (lit == 0) {
135 if ("<=".equals(token)) {
136 solver.addAtMost(literals, card);
137 } else if (">=".equals(token)) {
138 solver.addAtLeast(literals, card);
139 }
140 } else
141 throw new ParseFormatException();
142 }
143
144 }