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