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 public class GateTranslator extends SolverDecorator {
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45 public GateTranslator(ISolver solver) {
46 super(solver);
47 }
48
49
50
51
52
53
54 public void gateFalse(int y)
55 throws ContradictionException {
56 IVecInt clause = new VecInt(1);
57 clause.push(-y);
58 processClause(clause);
59 }
60
61
62
63
64
65
66 public void gateTrue(int y)
67 throws ContradictionException {
68 IVecInt clause = new VecInt(1);
69 clause.push(y);
70 processClause(clause);
71 }
72
73
74
75
76
77
78
79
80
81 public void ite(int y, int x1, int x2, int x3) throws ContradictionException {
82 IVecInt clause = new VecInt(2);
83
84
85 clause.push(-y).push(-x1).push(x2);
86 processClause(clause);
87 clause.clear();
88 clause.push(-y).push(x1).push(x3);
89 processClause(clause);
90
91
92
93
94
95
96
97 clause.clear();
98 clause.push(-x1).push(-x2).push(y);
99 processClause(clause);
100 clause.clear();
101 clause.push(x1).push(-x3).push(y);
102 processClause(clause);
103 clause.clear();
104 clause.push(-x2).push(-x3).push(y);
105 processClause(clause);
106
107
108
109 clause.clear();
110 clause.push(y).push(x2).push(x3);
111 processClause(clause);
112 }
113
114
115
116
117
118
119
120
121 public void and(int y, IVecInt literals) throws ContradictionException {
122
123
124
125 IVecInt clause = new VecInt(literals.size() + 1);
126 clause.push(y);
127 for (int i = 0; i < literals.size(); i++) {
128 clause.push(-literals.get(i));
129 }
130 processClause(clause);
131 clause.clear();
132 for (int i = 0; i < literals.size(); i++) {
133
134 clause.clear();
135 clause.push(-y);
136 clause.push(literals.get(i));
137 processClause(clause);
138 }
139 }
140
141
142
143
144
145
146
147
148 public void and(int y, int x1, int x2) throws ContradictionException {
149 IVecInt clause = new VecInt();
150 clause.push(-y);
151 clause.push(x1);
152 addClause(clause);
153 clause.clear();
154 clause.push(-y);
155 clause.push(x2);
156 addClause(clause);
157 clause.clear();
158 clause.push(y);
159 clause.push(-x1);
160 clause.push(-x2);
161 addClause(clause);
162 }
163
164
165
166
167
168
169
170
171 public void or(int y, IVecInt literals) throws ContradictionException {
172
173
174 IVecInt clause = new VecInt(literals.size() + 1);
175 literals.copyTo(clause);
176 clause.push(-y);
177 processClause(clause);
178 clause.clear();
179 for (int i = 0; i < literals.size(); i++) {
180
181 clause.clear();
182 clause.push(y);
183 clause.push(-literals.get(i));
184 processClause(clause);
185 }
186 }
187
188 private void processClause(IVecInt clause) throws ContradictionException {
189 addClause(clause);
190 }
191
192
193
194
195
196
197
198
199 public void not(int y, int x) throws ContradictionException {
200 IVecInt clause = new VecInt(2);
201
202
203 clause.push(-y).push(-x);
204 processClause(clause);
205
206 clause.clear();
207 clause.push(y).push(x);
208 processClause(clause);
209 }
210
211
212
213
214
215
216
217 public void xor(int y, IVecInt literals) throws ContradictionException {
218 literals.push(-y);
219 int[] f = new int[literals.size()];
220 literals.copyTo(f);
221 xor2Clause(f, 0, false);
222 }
223
224
225
226
227
228
229
230 public void iff(int y, IVecInt literals) throws ContradictionException {
231 literals.push(y);
232 int[] f = new int[literals.size()];
233 literals.copyTo(f);
234 iff2Clause(f, 0, false);
235 }
236
237 private void xor2Clause(int[] f, int prefix, boolean negation)
238 throws ContradictionException {
239 if (prefix == f.length - 1) {
240 IVecInt clause = new VecInt(f.length);
241 for (int i = 0; i < f.length - 1; ++i) {
242 clause.push(f[i]);
243 }
244 clause.push(f[f.length - 1] * (negation ? -1 : 1));
245 processClause(clause);
246 return;
247 }
248
249 if (negation) {
250 f[prefix] = -f[prefix];
251 xor2Clause(f, prefix + 1, false);
252 f[prefix] = -f[prefix];
253
254 xor2Clause(f, prefix + 1, true);
255 } else {
256 xor2Clause(f, prefix + 1, false);
257
258 f[prefix] = -f[prefix];
259 xor2Clause(f, prefix + 1, true);
260 f[prefix] = -f[prefix];
261 }
262 }
263
264 private void iff2Clause(int[] f, int prefix, boolean negation)
265 throws ContradictionException {
266 if (prefix == f.length - 1) {
267 IVecInt clause = new VecInt(f.length);
268 for (int i = 0; i < f.length - 1; ++i) {
269 clause.push(f[i]);
270 }
271 clause.push(f[f.length - 1] * (negation ? -1 : 1));
272 processClause(clause);
273 return;
274 }
275
276 if (negation) {
277 iff2Clause(f, prefix + 1, false);
278 f[prefix] = -f[prefix];
279 iff2Clause(f, prefix + 1, true);
280 f[prefix] = -f[prefix];
281 } else {
282 f[prefix] = -f[prefix];
283 iff2Clause(f, prefix + 1, false);
284 f[prefix] = -f[prefix];
285 iff2Clause(f, prefix + 1, true);
286 }
287 }
288
289 }