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.PrintWriter; |
29 |
|
import java.util.Scanner; |
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: 88 (88) |
Complexity: 12 |
Complexity Density: 0,43 |
|
38 |
|
public class AAGReader 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
|
AAGReader(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: 27 (27) |
Complexity: 4 |
Complexity Density: 0,19 |
|
70 |
0
|
@Override... |
71 |
|
public IProblem parseInstance(java.io.Reader in) |
72 |
|
throws ParseFormatException, ContradictionException, IOException { |
73 |
0
|
Scanner scanner = new Scanner(in); |
74 |
0
|
String prefix = scanner.next(); |
75 |
0
|
if (!"aag".equals(prefix)) { |
76 |
0
|
throw new ParseFormatException("AAG format only!"); |
77 |
|
} |
78 |
0
|
maxvarid = scanner.nextInt(); |
79 |
0
|
nbinputs = scanner.nextInt(); |
80 |
0
|
int nblatches = scanner.nextInt(); |
81 |
0
|
int nboutputs = scanner.nextInt(); |
82 |
0
|
if (nboutputs > 1) { |
83 |
0
|
throw new ParseFormatException( |
84 |
|
"CNF conversion allowed for single output circuit only!"); |
85 |
|
} |
86 |
0
|
int nbands = scanner.nextInt(); |
87 |
0
|
solver.newVar(maxvarid + 1); |
88 |
0
|
solver.setExpectedNumberOfClauses(3 * nbands + 2); |
89 |
0
|
readInput(nbinputs, scanner); |
90 |
0
|
readLatches(nblatches, scanner); |
91 |
0
|
if (nboutputs > 0) { |
92 |
0
|
int output0 = readOutput(nboutputs, scanner); |
93 |
0
|
readAnd(nbands, output0, scanner); |
94 |
|
} |
95 |
0
|
readInputSymbols(scanner); |
96 |
0
|
skipComments(scanner); |
97 |
0
|
return solver; |
98 |
|
} |
99 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
100 |
0
|
private void skipComments(Scanner scanner) {... |
101 |
|
|
102 |
|
|
103 |
|
} |
104 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
105 |
0
|
private void readInputSymbols(Scanner scanner) {... |
106 |
|
|
107 |
|
|
108 |
|
} |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
110 |
0
|
private void readAnd(int nbands, int output0, Scanner scanner)... |
111 |
|
throws ContradictionException { |
112 |
|
|
113 |
0
|
for (int i = 0; i < nbands; i++) { |
114 |
0
|
int lhs = scanner.nextInt(); |
115 |
0
|
int rhs0 = scanner.nextInt(); |
116 |
0
|
int rhs1 = scanner.nextInt(); |
117 |
0
|
solver.and(toDimacs(lhs), toDimacs(rhs0), toDimacs(rhs1)); |
118 |
|
} |
119 |
0
|
solver.gateTrue(maxvarid + 1); |
120 |
0
|
solver.gateTrue(toDimacs(output0)); |
121 |
|
} |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0,5 |
|
123 |
0
|
private int toDimacs(int v) {... |
124 |
0
|
if (v == FALSE) { |
125 |
0
|
return -(maxvarid + 1); |
126 |
|
} |
127 |
0
|
if (v == TRUE) { |
128 |
0
|
return maxvarid + 1; |
129 |
|
} |
130 |
0
|
int var = v >> 1; |
131 |
0
|
if ((v & 1) == 0) { |
132 |
0
|
return var; |
133 |
|
} |
134 |
0
|
return -var; |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
137 |
0
|
private int readOutput(int nboutputs, Scanner scanner) {... |
138 |
0
|
IVecInt outputs = new VecInt(nboutputs); |
139 |
0
|
for (int i = 0; i < nboutputs; i++) { |
140 |
0
|
outputs.push(scanner.nextInt()); |
141 |
|
} |
142 |
0
|
return outputs.get(0); |
143 |
|
} |
144 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
145 |
0
|
private void readLatches(int nblatches, Scanner scanner) {... |
146 |
|
|
147 |
|
|
148 |
|
} |
149 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
150 |
0
|
private IVecInt readInput(int nbinputs, Scanner scanner) {... |
151 |
0
|
IVecInt inputs = new VecInt(nbinputs); |
152 |
0
|
for (int i = 0; i < nbinputs; i++) { |
153 |
0
|
inputs.push(scanner.nextInt()); |
154 |
|
} |
155 |
0
|
return inputs; |
156 |
|
} |
157 |
|
|
158 |
|
} |