| 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.InputStream; |
| 29 |
|
import java.io.PrintWriter; |
| 30 |
|
|
| 31 |
|
import org.sat4j.core.VecInt; |
| 32 |
|
import org.sat4j.specs.ContradictionException; |
| 33 |
|
import org.sat4j.specs.IProblem; |
| 34 |
|
import org.sat4j.specs.ISolver; |
| 35 |
|
import org.sat4j.specs.IVecInt; |
| 36 |
|
import org.sat4j.tools.GateTranslator; |
| 37 |
|
|
|
|
|
| 0% |
Uncovered Elements: 114 (114) |
Complexity: 21 |
Complexity Density: 0,48 |
|
| 38 |
|
public class AIGReader extends Reader { |
| 39 |
|
|
| 40 |
|
private final static int FALSE = 0; |
| 41 |
|
|
| 42 |
|
private final static int TRUE = 1; |
| 43 |
|
|
| 44 |
|
private final GateTranslator solver; |
| 45 |
|
|
| 46 |
|
private int maxvarid; |
| 47 |
|
|
| 48 |
|
private int nbinputs; |
| 49 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 50 |
0
|
AIGReader(ISolver s) {... |
| 51 |
0
|
solver = new GateTranslator(s); |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 54 |
0
|
@Override... |
| 55 |
|
public String decode(int[] model) { |
| 56 |
0
|
StringBuffer stb = new StringBuffer(); |
| 57 |
0
|
for (int i = 0; i < nbinputs; i++) { |
| 58 |
0
|
stb.append(model[i]>0?1:0); |
| 59 |
|
} |
| 60 |
0
|
return stb.toString(); |
| 61 |
|
} |
| 62 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 1 |
|
| 63 |
0
|
@Override... |
| 64 |
|
public void decode(int[] model, PrintWriter out) { |
| 65 |
0
|
for (int i = 0; i < nbinputs; i++) { |
| 66 |
0
|
out.print(model[i]>0?1:0); |
| 67 |
|
} |
| 68 |
|
} |
| 69 |
|
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 6 |
Complexity Density: 0,6 |
|
| 70 |
0
|
int parseInt(InputStream in, char expected) throws IOException,... |
| 71 |
|
ParseFormatException { |
| 72 |
0
|
int res, ch; |
| 73 |
0
|
ch = in.read(); |
| 74 |
|
|
| 75 |
0
|
if (ch < '0' || ch > '9') |
| 76 |
0
|
throw new ParseFormatException("expected digit"); |
| 77 |
0
|
res = ch - '0'; |
| 78 |
|
|
| 79 |
0
|
while ((ch = in.read()) >= '0' && ch <= '9') |
| 80 |
0
|
res = 10 * res + (ch - '0'); |
| 81 |
|
|
| 82 |
0
|
if (ch != expected) |
| 83 |
0
|
throw new ParseFormatException("unexpected character"); |
| 84 |
|
|
| 85 |
0
|
return res; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
@see |
| 92 |
|
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0,42 |
|
| 93 |
0
|
@Override... |
| 94 |
|
public IProblem parseInstance(InputStream in) throws ParseFormatException, |
| 95 |
|
ContradictionException, IOException { |
| 96 |
0
|
if (in.read() != 'a' || in.read() != 'i' || in.read() != 'g' |
| 97 |
|
|| in.read() != ' ') { |
| 98 |
0
|
throw new ParseFormatException("AIG format only!"); |
| 99 |
|
} |
| 100 |
0
|
maxvarid = parseInt(in, ' '); |
| 101 |
0
|
nbinputs = parseInt(in, ' '); |
| 102 |
0
|
int nblatches = parseInt(in, ' '); |
| 103 |
0
|
if (nblatches > 0) { |
| 104 |
0
|
throw new ParseFormatException( |
| 105 |
|
"CNF conversion cannot handle latches!"); |
| 106 |
|
} |
| 107 |
0
|
int nboutputs = parseInt(in, ' '); |
| 108 |
0
|
if (nboutputs > 1) { |
| 109 |
0
|
throw new ParseFormatException( |
| 110 |
|
"CNF conversion allowed for single output circuit only!"); |
| 111 |
|
} |
| 112 |
0
|
int nbands = parseInt(in, '\n'); |
| 113 |
0
|
solver.newVar(maxvarid + 1); |
| 114 |
0
|
solver.setExpectedNumberOfClauses(3 * nbands + 2); |
| 115 |
0
|
if (nboutputs > 0) { |
| 116 |
0
|
assert nboutputs == 1; |
| 117 |
0
|
int output0 = parseInt(in, '\n'); |
| 118 |
0
|
readAnd(nbands, output0, in, 2 * (nbinputs + 1)); |
| 119 |
|
} |
| 120 |
0
|
readInputSymbols(in); |
| 121 |
0
|
skipComments(in); |
| 122 |
0
|
return solver; |
| 123 |
|
} |
| 124 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 125 |
0
|
private void skipComments(InputStream in) {... |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
} |
| 129 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 130 |
0
|
private void readInputSymbols(InputStream in) {... |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
} |
| 134 |
|
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
| 135 |
0
|
static int safeGet(InputStream in) throws IOException, ParseFormatException {... |
| 136 |
0
|
int ch = in.read(); |
| 137 |
0
|
if (ch == -1) { |
| 138 |
0
|
throw new ParseFormatException("AIG Error, EOF met too early"); |
| 139 |
|
} |
| 140 |
0
|
return ch; |
| 141 |
|
} |
| 142 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
| 143 |
0
|
static int decode(InputStream in) throws IOException, ParseFormatException {... |
| 144 |
0
|
int x = 0, i = 0; |
| 145 |
0
|
int ch; |
| 146 |
|
|
| 147 |
0
|
while (((ch = safeGet(in)) & 0x80) > 0) { |
| 148 |
0
|
System.out.println("=>" + ch); |
| 149 |
0
|
x |= (ch & 0x7f) << (7 * i++); |
| 150 |
|
} |
| 151 |
0
|
return x | (ch << (7 * i)); |
| 152 |
|
} |
| 153 |
|
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 2 |
Complexity Density: 0,18 |
|
| 154 |
0
|
private void readAnd(int nbands, int output0, InputStream in, int startid)... |
| 155 |
|
throws ContradictionException, IOException, ParseFormatException { |
| 156 |
0
|
IVecInt clause = new VecInt(); |
| 157 |
0
|
int lhs = startid; |
| 158 |
0
|
for (int i = 0; i < nbands; i++) { |
| 159 |
0
|
int delta0 = decode(in); |
| 160 |
0
|
int delta1 = decode(in); |
| 161 |
0
|
int rhs0 = lhs - delta0; |
| 162 |
0
|
int rhs1 = rhs0 - delta1; |
| 163 |
0
|
solver.and(toDimacs(lhs), toDimacs(rhs0), toDimacs(rhs1)); |
| 164 |
0
|
lhs += 2; |
| 165 |
|
} |
| 166 |
0
|
solver.gateTrue(maxvarid + 1); |
| 167 |
0
|
solver.gateTrue(toDimacs(output0)); |
| 168 |
|
} |
| 169 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0,5 |
|
| 170 |
0
|
private int toDimacs(int v) {... |
| 171 |
0
|
if (v == FALSE) { |
| 172 |
0
|
return -(maxvarid + 1); |
| 173 |
|
} |
| 174 |
0
|
if (v == TRUE) { |
| 175 |
0
|
return maxvarid + 1; |
| 176 |
|
} |
| 177 |
0
|
int var = v >> 1; |
| 178 |
0
|
if ((v & 1) == 0) { |
| 179 |
0
|
return var; |
| 180 |
|
} |
| 181 |
0
|
return -var; |
| 182 |
|
} |
| 183 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 184 |
0
|
@Override... |
| 185 |
|
public IProblem parseInstance(java.io.Reader in) |
| 186 |
|
throws ParseFormatException, ContradictionException, IOException { |
| 187 |
0
|
throw new UnsupportedOperationException(); |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
} |