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.tools;
31
32 import java.io.PrintWriter;
33
34 import org.sat4j.specs.ContradictionException;
35 import org.sat4j.specs.IConstr;
36 import org.sat4j.specs.IVecInt;
37 import org.sat4j.specs.IteratorInt;
38
39
40
41
42
43
44
45
46
47
48 public class DimacsStringSolver extends AbstractOutputSolver {
49
50
51
52
53 private static final long serialVersionUID = 1L;
54
55 private StringBuffer out;
56
57 private int firstCharPos;
58
59 private final int initBuilderSize;
60
61 private int maxvarid = 0;
62
63 public DimacsStringSolver() {
64 this(16);
65 }
66
67 public DimacsStringSolver(int initSize) {
68 this.out = new StringBuffer(initSize);
69 this.initBuilderSize = initSize;
70 }
71
72 public StringBuffer getOut() {
73 return this.out;
74 }
75
76 public int newVar() {
77 return 0;
78 }
79
80 @Override
81 public int newVar(int howmany) {
82 setNbVars(howmany);
83 return howmany;
84 }
85
86 protected void setNbVars(int howmany) {
87 this.nbvars = howmany;
88 this.maxvarid = howmany;
89 }
90
91 public void setExpectedNumberOfClauses(int nb) {
92 this.out.append(" ");
93 this.out.append(nb);
94 this.nbclauses = nb;
95 this.fixedNbClauses = true;
96 }
97
98 public IConstr addClause(IVecInt literals) throws ContradictionException {
99 if (this.firstConstr) {
100 if (!this.fixedNbClauses) {
101 this.firstCharPos = 0;
102 this.out.append(" ");
103 this.out.append("\n");
104 this.nbclauses = 0;
105 }
106 this.firstConstr = false;
107 }
108 if (!this.fixedNbClauses) {
109 this.nbclauses++;
110 }
111 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
112 this.out.append(iterator.next()).append(" ");
113 }
114 this.out.append("0\n");
115 return null;
116 }
117
118 public IConstr addAtMost(IVecInt literals, int degree)
119 throws ContradictionException {
120 if (degree > 1) {
121 throw new UnsupportedOperationException(
122 "Not a clausal problem! degree " + degree);
123 }
124 assert degree == 1;
125 if (this.firstConstr) {
126 this.firstCharPos = 0;
127 this.out.append(" ");
128 this.out.append("\n");
129 this.nbclauses = 0;
130 this.firstConstr = false;
131 }
132
133 for (int i = 0; i <= literals.size(); i++) {
134 for (int j = i + 1; j < literals.size(); j++) {
135 if (!this.fixedNbClauses) {
136 this.nbclauses++;
137 }
138 this.out.append(-literals.get(i));
139 this.out.append(" ");
140 this.out.append(-literals.get(j));
141 this.out.append(" 0\n");
142 }
143 }
144 return null;
145 }
146
147 public IConstr addExactly(IVecInt literals, int n)
148 throws ContradictionException {
149 if (n > 1) {
150 throw new UnsupportedOperationException(
151 "Not a clausal problem! degree " + n);
152 }
153 assert n == 1;
154 addAtMost(literals, n);
155 addAtLeast(literals, n);
156 return null;
157 }
158
159 public IConstr addAtLeast(IVecInt literals, int degree)
160 throws ContradictionException {
161 if (degree > 1) {
162 throw new UnsupportedOperationException(
163 "Not a clausal problem! degree " + degree);
164 }
165 assert degree == 1;
166 return addClause(literals);
167 }
168
169 public void reset() {
170 this.fixedNbClauses = false;
171 this.firstConstr = true;
172 this.out = new StringBuffer(this.initBuilderSize);
173 this.maxvarid = 0;
174 }
175
176 public String toString(String prefix) {
177 return "Dimacs output solver";
178 }
179
180 @Override
181 public int nConstraints() {
182 return this.nbclauses;
183 }
184
185 @Override
186 public int nVars() {
187 return this.maxvarid;
188 }
189
190 @Override
191 public String toString() {
192 this.out.insert(this.firstCharPos, "p cnf " + this.maxvarid + " "
193 + this.nbclauses);
194 return this.out.toString();
195 }
196
197
198
199
200 public int nextFreeVarId(boolean reserve) {
201 if (reserve) {
202 return ++this.maxvarid;
203 }
204 return this.maxvarid + 1;
205 }
206
207
208
209
210 public int[] modelWithInternalVariables() {
211 throw new UnsupportedOperationException();
212 }
213
214 public int realNumberOfVariables() {
215 return this.maxvarid;
216 }
217
218 public void registerLiteral(int p) {
219 throw new UnsupportedOperationException();
220 }
221
222
223
224
225 public boolean primeImplicant(int p) {
226 throw new UnsupportedOperationException();
227 }
228
229
230
231
232 public void printStat(PrintWriter out) {
233 throw new UnsupportedOperationException();
234
235 }
236
237
238
239
240 public void printInfos(PrintWriter out) {
241 throw new UnsupportedOperationException();
242
243 }
244 }