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