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
29
30 package org.sat4j.minisat.constraints.cnf;
31
32 import static org.sat4j.core.LiteralsUtils.neg;
33
34 import java.io.Serializable;
35
36 import org.sat4j.minisat.core.Constr;
37 import org.sat4j.minisat.core.ILits;
38 import org.sat4j.minisat.core.Propagatable;
39 import org.sat4j.minisat.core.UnitPropagationListener;
40 import org.sat4j.specs.IVecInt;
41
42
43
44
45
46
47
48 public abstract class BinaryClause implements Propagatable, Constr,
49 Serializable {
50
51 private static final long serialVersionUID = 1L;
52
53 protected double activity;
54
55 private final ILits voc;
56
57 protected int head;
58
59 protected int tail;
60
61
62
63
64
65
66
67
68
69 public BinaryClause(IVecInt ps, ILits voc) {
70 assert ps.size() == 2;
71 this.head = ps.get(0);
72 this.tail = ps.get(1);
73 this.voc = voc;
74 this.activity = 0;
75 }
76
77
78
79
80
81
82 public void calcReason(int p, IVecInt outReason) {
83 if (this.voc.isFalsified(this.head)) {
84 outReason.push(neg(this.head));
85 }
86 if (this.voc.isFalsified(this.tail)) {
87 outReason.push(neg(this.tail));
88 }
89 }
90
91
92
93
94
95
96 public void remove(UnitPropagationListener upl) {
97 this.voc.watches(neg(this.head)).remove(this);
98 this.voc.watches(neg(this.tail)).remove(this);
99 }
100
101
102
103
104
105
106 public boolean simplify() {
107 if (this.voc.isSatisfied(this.head) || this.voc.isSatisfied(this.tail)) {
108 return true;
109 }
110 return false;
111 }
112
113 public boolean propagate(UnitPropagationListener s, int p) {
114 this.voc.watch(p, this);
115 if (this.head == neg(p)) {
116 return s.enqueue(this.tail, this);
117 }
118 assert this.tail == neg(p);
119 return s.enqueue(this.head, this);
120 }
121
122
123
124
125 public boolean locked() {
126 return this.voc.getReason(this.head) == this
127 || this.voc.getReason(this.tail) == this;
128 }
129
130
131
132
133 public double getActivity() {
134 return this.activity;
135 }
136
137 @Override
138 public String toString() {
139 StringBuffer stb = new StringBuffer();
140 stb.append(Lits.toString(this.head));
141 stb.append("[");
142 stb.append(this.voc.valueToString(this.head));
143 stb.append("]");
144 stb.append(" ");
145 stb.append(Lits.toString(this.tail));
146 stb.append("[");
147 stb.append(this.voc.valueToString(this.tail));
148 stb.append("]");
149 return stb.toString();
150 }
151
152
153
154
155
156
157
158
159
160 public int get(int i) {
161 if (i == 0) {
162 return this.head;
163 }
164 assert i == 1;
165 return this.tail;
166 }
167
168
169
170
171 public void rescaleBy(double d) {
172 this.activity *= d;
173 }
174
175 public int size() {
176 return 2;
177 }
178
179 public void assertConstraint(UnitPropagationListener s) {
180 assert this.voc.isUnassigned(this.head);
181 boolean ret = s.enqueue(this.head, this);
182 assert ret;
183 }
184
185 public ILits getVocabulary() {
186 return this.voc;
187 }
188
189 public int[] getLits() {
190 int[] tmp = new int[2];
191 tmp[0] = this.head;
192 tmp[1] = this.tail;
193 return tmp;
194 }
195
196 @Override
197 public boolean equals(Object obj) {
198 if (obj == null) {
199 return false;
200 }
201 try {
202 BinaryClause wcl = (BinaryClause) obj;
203 if (wcl.head != this.head || wcl.tail != this.tail) {
204 return false;
205 }
206 return true;
207 } catch (ClassCastException e) {
208 return false;
209 }
210 }
211
212 @Override
213 public int hashCode() {
214 long sum = this.head + this.tail;
215 return (int) sum / 2;
216 }
217
218 public void register() {
219 this.voc.watch(neg(this.head), this);
220 this.voc.watch(neg(this.tail), this);
221 }
222
223 public boolean canBePropagatedMultipleTimes() {
224 return false;
225 }
226
227 public Constr toConstraint() {
228 return this;
229 }
230 }