| 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 |
|
package org.sat4j.reader; |
| 27 |
|
|
| 28 |
|
import java.io.IOException; |
| 29 |
|
import java.io.LineNumberReader; |
| 30 |
|
import java.io.PrintWriter; |
| 31 |
|
import java.io.Serializable; |
| 32 |
|
import java.util.Scanner; |
| 33 |
|
import java.util.StringTokenizer; |
| 34 |
|
|
| 35 |
|
import org.sat4j.core.VecInt; |
| 36 |
|
import org.sat4j.opt.WeightedMaxSatDecorator; |
| 37 |
|
import org.sat4j.specs.ContradictionException; |
| 38 |
|
import org.sat4j.specs.IProblem; |
| 39 |
|
import org.sat4j.specs.ISolver; |
| 40 |
|
import org.sat4j.specs.IVecInt; |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@version |
| 60 |
|
@author |
| 61 |
|
@author |
| 62 |
|
|
|
|
|
| 0% |
Uncovered Elements: 132 (132) |
Complexity: 26 |
Complexity Density: 0,45 |
|
| 63 |
|
public class DimacsReader extends Reader implements Serializable { |
| 64 |
|
|
| 65 |
|
private static final long serialVersionUID = 1L; |
| 66 |
|
|
| 67 |
|
protected int expectedNbOfConstr; |
| 68 |
|
|
| 69 |
|
protected final ISolver solver; |
| 70 |
|
|
| 71 |
|
private boolean checkConstrNb = true; |
| 72 |
|
|
| 73 |
|
private final String formatString; |
| 74 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 75 |
0
|
public DimacsReader(ISolver solver) {... |
| 76 |
0
|
this(solver, "cnf"); |
| 77 |
|
} |
| 78 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 79 |
0
|
public DimacsReader(ISolver solver, String format) {... |
| 80 |
0
|
this.solver = solver; |
| 81 |
0
|
formatString = format; |
| 82 |
|
} |
| 83 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 84 |
0
|
public void disableNumberOfConstraintCheck() {... |
| 85 |
0
|
checkConstrNb = false; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
@param |
| 92 |
|
|
| 93 |
|
@throws |
| 94 |
|
|
| 95 |
|
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
| 96 |
0
|
protected void skipComments(final LineNumberReader in) throws IOException {... |
| 97 |
0
|
int c; |
| 98 |
|
|
| 99 |
0
|
do { |
| 100 |
0
|
in.mark(4); |
| 101 |
0
|
c = in.read(); |
| 102 |
0
|
if (c == 'c') { |
| 103 |
0
|
in.readLine(); |
| 104 |
|
} else { |
| 105 |
0
|
in.reset(); |
| 106 |
|
} |
| 107 |
0
|
} while (c == 'c'); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
@param |
| 112 |
|
|
| 113 |
|
@throws |
| 114 |
|
|
| 115 |
|
@throws |
| 116 |
|
|
| 117 |
|
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0,47 |
|
| 118 |
0
|
protected void readProblemLine(LineNumberReader in) throws IOException,... |
| 119 |
|
ParseFormatException { |
| 120 |
|
|
| 121 |
0
|
String line = in.readLine(); |
| 122 |
|
|
| 123 |
0
|
if (line == null) { |
| 124 |
0
|
throw new ParseFormatException( |
| 125 |
|
"premature end of file: <p cnf ...> expected on line " |
| 126 |
|
+ in.getLineNumber()); |
| 127 |
|
} |
| 128 |
0
|
StringTokenizer stk = new StringTokenizer(line); |
| 129 |
|
|
| 130 |
0
|
if (!(stk.hasMoreTokens() && stk.nextToken().equals("p") |
| 131 |
|
&& stk.hasMoreTokens() && stk.nextToken().equals(formatString))) { |
| 132 |
0
|
throw new ParseFormatException( |
| 133 |
|
"problem line expected (p cnf ...) on line " |
| 134 |
|
+ in.getLineNumber()); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
0
|
int vars; |
| 138 |
|
|
| 139 |
|
|
| 140 |
0
|
vars = Integer.parseInt(stk.nextToken()); |
| 141 |
0
|
assert vars > 0; |
| 142 |
0
|
solver.newVar(vars); |
| 143 |
|
|
| 144 |
0
|
expectedNbOfConstr = Integer.parseInt(stk.nextToken()); |
| 145 |
0
|
assert expectedNbOfConstr > 0; |
| 146 |
0
|
solver.setExpectedNumberOfClauses(expectedNbOfConstr); |
| 147 |
0
|
if ("wcnf".equals(formatString)) { |
| 148 |
|
|
| 149 |
0
|
int top; |
| 150 |
0
|
if (stk.hasMoreTokens()) { |
| 151 |
0
|
top = Integer.parseInt(stk.nextToken()); |
| 152 |
|
} else { |
| 153 |
0
|
top = Integer.MAX_VALUE; |
| 154 |
|
} |
| 155 |
0
|
((WeightedMaxSatDecorator)solver).setTopWeight(top); |
| 156 |
|
} |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param |
| 161 |
|
|
| 162 |
|
@throws |
| 163 |
|
|
| 164 |
|
@throws |
| 165 |
|
|
| 166 |
|
@throws |
| 167 |
|
|
| 168 |
|
|
|
|
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 10 |
Complexity Density: 0,48 |
|
| 169 |
0
|
protected void readConstrs(LineNumberReader in) throws IOException,... |
| 170 |
|
ParseFormatException, ContradictionException { |
| 171 |
0
|
String line; |
| 172 |
|
|
| 173 |
0
|
int realNbOfConstr = 0; |
| 174 |
|
|
| 175 |
0
|
IVecInt literals = new VecInt(); |
| 176 |
|
|
| 177 |
0
|
while (true) { |
| 178 |
0
|
line = in.readLine(); |
| 179 |
|
|
| 180 |
0
|
if (line == null) { |
| 181 |
|
|
| 182 |
0
|
if (literals.size() > 0) { |
| 183 |
|
|
| 184 |
0
|
solver.addClause(literals); |
| 185 |
0
|
realNbOfConstr++; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
0
|
break; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
0
|
if (line.startsWith("c ")) { |
| 192 |
|
|
| 193 |
0
|
System.out.println("Found commmented line : " + line); |
| 194 |
0
|
continue; |
| 195 |
|
} |
| 196 |
0
|
if (line.startsWith("%") && expectedNbOfConstr == realNbOfConstr) { |
| 197 |
0
|
System.out |
| 198 |
|
.println("Ignoring the rest of the file (SATLIB format"); |
| 199 |
0
|
break; |
| 200 |
|
} |
| 201 |
0
|
boolean added = handleConstr(line, literals); |
| 202 |
0
|
if (added) { |
| 203 |
0
|
realNbOfConstr++; |
| 204 |
|
} |
| 205 |
|
} |
| 206 |
0
|
if (checkConstrNb && expectedNbOfConstr != realNbOfConstr) { |
| 207 |
0
|
throw new ParseFormatException("wrong nbclauses parameter. Found " |
| 208 |
|
+ realNbOfConstr + ", " + expectedNbOfConstr + " expected"); |
| 209 |
|
} |
| 210 |
|
} |
| 211 |
|
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0,31 |
|
| 212 |
0
|
protected boolean handleConstr(String line, IVecInt literals)... |
| 213 |
|
throws ContradictionException { |
| 214 |
0
|
int lit; |
| 215 |
0
|
boolean added = false; |
| 216 |
0
|
Scanner scan; |
| 217 |
0
|
scan = new Scanner(line); |
| 218 |
0
|
while (scan.hasNext()) { |
| 219 |
0
|
lit = scan.nextInt(); |
| 220 |
|
|
| 221 |
0
|
if (lit == 0) { |
| 222 |
0
|
if (literals.size() > 0) { |
| 223 |
0
|
solver.addClause(literals); |
| 224 |
0
|
literals.clear(); |
| 225 |
0
|
added = true; |
| 226 |
|
} |
| 227 |
|
} else { |
| 228 |
0
|
literals.push(lit); |
| 229 |
|
} |
| 230 |
|
} |
| 231 |
0
|
return added; |
| 232 |
|
} |
| 233 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 234 |
0
|
@Override... |
| 235 |
|
public final IProblem parseInstance(final java.io.Reader in) |
| 236 |
|
throws ParseFormatException, ContradictionException, IOException { |
| 237 |
0
|
return parseInstance(new LineNumberReader(in)); |
| 238 |
|
|
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
|
@param |
| 243 |
|
|
| 244 |
|
@throws |
| 245 |
|
|
| 246 |
|
@throws |
| 247 |
|
|
| 248 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0,38 |
|
| 249 |
0
|
private IProblem parseInstance(LineNumberReader in)... |
| 250 |
|
throws ParseFormatException, ContradictionException { |
| 251 |
0
|
solver.reset(); |
| 252 |
0
|
try { |
| 253 |
0
|
skipComments(in); |
| 254 |
0
|
readProblemLine(in); |
| 255 |
0
|
readConstrs(in); |
| 256 |
0
|
return solver; |
| 257 |
|
} catch (IOException e) { |
| 258 |
0
|
throw new ParseFormatException(e); |
| 259 |
|
} catch (NumberFormatException e) { |
| 260 |
0
|
throw new ParseFormatException("integer value expected on line " |
| 261 |
|
+ in.getLineNumber(), e); |
| 262 |
|
} |
| 263 |
|
} |
| 264 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
| 265 |
0
|
@Override... |
| 266 |
|
public String decode(int[] model) { |
| 267 |
0
|
StringBuffer stb = new StringBuffer(); |
| 268 |
0
|
for (int i = 0; i < model.length; i++) { |
| 269 |
0
|
stb.append(model[i]); |
| 270 |
0
|
stb.append(" "); |
| 271 |
|
} |
| 272 |
0
|
stb.append("0"); |
| 273 |
0
|
return stb.toString(); |
| 274 |
|
} |
| 275 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 276 |
0
|
@Override... |
| 277 |
|
public void decode(int[] model, PrintWriter out) { |
| 278 |
0
|
for (int i = 0; i < model.length; i++) { |
| 279 |
0
|
out.print(model[i]); |
| 280 |
0
|
out.print(" "); |
| 281 |
|
} |
| 282 |
0
|
out.print("0"); |
| 283 |
|
} |
| 284 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 285 |
0
|
protected ISolver getSolver() {... |
| 286 |
0
|
return solver; |
| 287 |
|
} |
| 288 |
|
} |