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