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.tools; |
26 |
|
|
27 |
|
import org.sat4j.core.VecInt; |
28 |
|
import org.sat4j.specs.ContradictionException; |
29 |
|
import org.sat4j.specs.ISolver; |
30 |
|
import org.sat4j.specs.IVecInt; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@author |
39 |
|
@author |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 162 (162) |
Complexity: 18 |
Complexity Density: 0,25 |
|
41 |
|
public class ExtendedDimacsArrayReader extends DimacsArrayReader { |
42 |
|
|
43 |
|
public static final int FALSE = 1; |
44 |
|
|
45 |
|
public static final int TRUE = 2; |
46 |
|
|
47 |
|
public static final int NOT = 3; |
48 |
|
|
49 |
|
public static final int AND = 4; |
50 |
|
|
51 |
|
public static final int NAND = 5; |
52 |
|
|
53 |
|
public static final int OR = 6; |
54 |
|
|
55 |
|
public static final int NOR = 7; |
56 |
|
|
57 |
|
public static final int XOR = 8; |
58 |
|
|
59 |
|
public static final int XNOR = 9; |
60 |
|
|
61 |
|
public static final int IMPLIES = 10; |
62 |
|
|
63 |
|
public static final int IFF = 11; |
64 |
|
|
65 |
|
public static final int IFTHENELSE = 12; |
66 |
|
|
67 |
|
public static final int ATLEAST = 13; |
68 |
|
|
69 |
|
public static final int ATMOST = 14; |
70 |
|
|
71 |
|
public static final int COUNT = 15; |
72 |
|
|
73 |
|
private static final long serialVersionUID = 1L; |
74 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
75 |
0
|
public ExtendedDimacsArrayReader(ISolver solver) {... |
76 |
0
|
super(solver); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
|
84 |
|
@param |
85 |
|
|
86 |
|
@param |
87 |
|
|
88 |
|
@return |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 9 |
Complexity Density: 0,31 |
|
90 |
0
|
@Override... |
91 |
|
protected boolean handleConstr(int gateType, int output, int[] inputs) |
92 |
|
throws ContradictionException { |
93 |
0
|
IVecInt literals = new VecInt(inputs); |
94 |
0
|
switch (gateType) { |
95 |
0
|
case FALSE: |
96 |
0
|
gateFalse(output, literals); |
97 |
0
|
break; |
98 |
0
|
case TRUE: |
99 |
0
|
gateTrue(output, literals); |
100 |
0
|
break; |
101 |
0
|
case OR: |
102 |
0
|
or(output, literals); |
103 |
0
|
break; |
104 |
0
|
case NOT: |
105 |
0
|
not(output, literals); |
106 |
0
|
break; |
107 |
0
|
case AND: |
108 |
0
|
and(output, literals); |
109 |
0
|
break; |
110 |
0
|
case XOR: |
111 |
0
|
xor(output, literals); |
112 |
0
|
break; |
113 |
0
|
case IFF: |
114 |
0
|
iff(output, literals); |
115 |
0
|
break; |
116 |
0
|
case IFTHENELSE: |
117 |
0
|
ite(output, literals); |
118 |
0
|
break; |
119 |
0
|
default: |
120 |
0
|
throw new UnsupportedOperationException("Gate type " + gateType |
121 |
|
+ " not handled yet"); |
122 |
|
} |
123 |
0
|
return true; |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0,33 |
|
126 |
0
|
private void gateFalse(int y, IVecInt literals)... |
127 |
|
throws ContradictionException { |
128 |
0
|
assert literals.size() == 0; |
129 |
0
|
IVecInt clause = new VecInt(1); |
130 |
0
|
clause.push(-y); |
131 |
0
|
processClause(clause); |
132 |
|
} |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0,33 |
|
134 |
0
|
private void gateTrue(int y, IVecInt literals)... |
135 |
|
throws ContradictionException { |
136 |
0
|
assert literals.size() == 0; |
137 |
0
|
IVecInt clause = new VecInt(1); |
138 |
0
|
clause.push(y); |
139 |
0
|
processClause(clause); |
140 |
|
} |
141 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 1 |
Complexity Density: 0,07 |
|
142 |
0
|
private void ite(int y, IVecInt literals) throws ContradictionException {... |
143 |
0
|
assert literals.size() == 3; |
144 |
0
|
IVecInt clause = new VecInt(2); |
145 |
|
|
146 |
|
|
147 |
0
|
clause.push(-y).push(-literals.get(0)).push(literals.get(1)); |
148 |
0
|
processClause(clause); |
149 |
0
|
clause.clear(); |
150 |
0
|
clause.push(-y).push(literals.get(0)).push(literals.get(2)); |
151 |
0
|
processClause(clause); |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
0
|
clause.clear(); |
160 |
0
|
clause.push(-literals.get(0)).push(-literals.get(1)).push(y); |
161 |
0
|
processClause(clause); |
162 |
0
|
clause.clear(); |
163 |
0
|
clause.push(literals.get(0)).push(-literals.get(2)).push(y); |
164 |
0
|
processClause(clause); |
165 |
0
|
clause.clear(); |
166 |
0
|
clause.push(-literals.get(1)).push(-literals.get(2)).push(y); |
167 |
0
|
processClause(clause); |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0,27 |
|
170 |
0
|
private void and(int y, IVecInt literals) throws ContradictionException {... |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
0
|
IVecInt clause = new VecInt(literals.size() + 1); |
175 |
0
|
clause.push(y); |
176 |
0
|
for (int i = 0; i < literals.size(); i++) { |
177 |
0
|
clause.push(-literals.get(i)); |
178 |
|
} |
179 |
0
|
processClause(clause); |
180 |
0
|
clause.clear(); |
181 |
0
|
for (int i = 0; i < literals.size(); i++) { |
182 |
|
|
183 |
0
|
clause.clear(); |
184 |
0
|
clause.push(-y); |
185 |
0
|
clause.push(literals.get(i)); |
186 |
0
|
processClause(clause); |
187 |
|
} |
188 |
|
} |
189 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0,2 |
|
190 |
0
|
private void or(int y, IVecInt literals) throws ContradictionException {... |
191 |
|
|
192 |
|
|
193 |
0
|
IVecInt clause = new VecInt(literals.size() + 1); |
194 |
0
|
literals.copyTo(clause); |
195 |
0
|
clause.push(-y); |
196 |
0
|
processClause(clause); |
197 |
0
|
clause.clear(); |
198 |
0
|
for (int i = 0; i < literals.size(); i++) { |
199 |
|
|
200 |
0
|
clause.clear(); |
201 |
0
|
clause.push(y); |
202 |
0
|
clause.push(-literals.get(i)); |
203 |
0
|
processClause(clause); |
204 |
|
} |
205 |
|
} |
206 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
0
|
private void processClause(IVecInt clause) throws ContradictionException {... |
208 |
0
|
solver.addClause(clause); |
209 |
|
} |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 1 |
Complexity Density: 0,17 |
|
211 |
0
|
private void not(int y, IVecInt literals) throws ContradictionException {... |
212 |
0
|
assert literals.size() == 1; |
213 |
0
|
IVecInt clause = new VecInt(2); |
214 |
|
|
215 |
|
|
216 |
0
|
clause.push(-y).push(-literals.get(0)); |
217 |
0
|
processClause(clause); |
218 |
|
|
219 |
0
|
clause.clear(); |
220 |
0
|
clause.push(y).push(literals.get(0)); |
221 |
0
|
processClause(clause); |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
224 |
0
|
void xor(int y, IVecInt literals) throws ContradictionException {... |
225 |
0
|
literals.push(-y); |
226 |
0
|
int[] f = new int[literals.size()]; |
227 |
0
|
literals.copyTo(f); |
228 |
0
|
xor2Clause(f, 0, false); |
229 |
|
} |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
231 |
0
|
void iff(int y, IVecInt literals) throws ContradictionException {... |
232 |
0
|
literals.push(y); |
233 |
0
|
int[] f = new int[literals.size()]; |
234 |
0
|
literals.copyTo(f); |
235 |
0
|
iff2Clause(f, 0, false); |
236 |
|
} |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0,25 |
|
238 |
0
|
void xor2Clause(int[] f, int prefix, boolean negation)... |
239 |
|
throws ContradictionException { |
240 |
0
|
if (prefix == f.length - 1) { |
241 |
0
|
IVecInt clause = new VecInt(f.length); |
242 |
0
|
for (int i = 0; i < f.length - 1; ++i) { |
243 |
0
|
clause.push(f[i]); |
244 |
|
} |
245 |
0
|
clause.push(f[f.length - 1] * (negation ? -1 : 1)); |
246 |
0
|
processClause(clause); |
247 |
0
|
return; |
248 |
|
} |
249 |
|
|
250 |
0
|
if (negation) { |
251 |
0
|
f[prefix] = -f[prefix]; |
252 |
0
|
xor2Clause(f, prefix + 1, false); |
253 |
0
|
f[prefix] = -f[prefix]; |
254 |
|
|
255 |
0
|
xor2Clause(f, prefix + 1, true); |
256 |
|
} else { |
257 |
0
|
xor2Clause(f, prefix + 1, false); |
258 |
|
|
259 |
0
|
f[prefix] = -f[prefix]; |
260 |
0
|
xor2Clause(f, prefix + 1, true); |
261 |
0
|
f[prefix] = -f[prefix]; |
262 |
|
} |
263 |
|
} |
264 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0,25 |
|
265 |
0
|
void iff2Clause(int[] f, int prefix, boolean negation)... |
266 |
|
throws ContradictionException { |
267 |
0
|
if (prefix == f.length - 1) { |
268 |
0
|
IVecInt clause = new VecInt(f.length); |
269 |
0
|
for (int i = 0; i < f.length - 1; ++i) { |
270 |
0
|
clause.push(f[i]); |
271 |
|
} |
272 |
0
|
clause.push(f[f.length - 1] * (negation ? -1 : 1)); |
273 |
0
|
processClause(clause); |
274 |
0
|
return; |
275 |
|
} |
276 |
|
|
277 |
0
|
if (negation) { |
278 |
0
|
iff2Clause(f, prefix + 1, false); |
279 |
0
|
f[prefix] = -f[prefix]; |
280 |
0
|
iff2Clause(f, prefix + 1, true); |
281 |
0
|
f[prefix] = -f[prefix]; |
282 |
|
} else { |
283 |
0
|
f[prefix] = -f[prefix]; |
284 |
0
|
iff2Clause(f, prefix + 1, false); |
285 |
0
|
f[prefix] = -f[prefix]; |
286 |
0
|
iff2Clause(f, prefix + 1, true); |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
|
} |