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.ObjectInputStream;
33 import java.io.PrintWriter;
34
35 import org.sat4j.specs.ContradictionException;
36 import org.sat4j.specs.IConstr;
37 import org.sat4j.specs.IVecInt;
38 import org.sat4j.specs.IteratorInt;
39
40
41
42
43
44
45
46
47
48 public class DimacsOutputSolver extends AbstractOutputSolver {
49
50
51
52
53 private static final long serialVersionUID = 1L;
54
55 private transient PrintWriter out;
56
57 public DimacsOutputSolver() {
58 this(new PrintWriter(System.out, true));
59 }
60
61 public DimacsOutputSolver(PrintWriter pw) {
62 this.out = pw;
63 }
64
65 private void readObject(ObjectInputStream stream) {
66 this.out = new PrintWriter(System.out, true);
67 }
68
69 public int newVar() {
70 return 0;
71 }
72
73 @Override
74 public int newVar(int howmany) {
75 this.out.print("p cnf " + howmany);
76 this.nbvars = howmany;
77 return 0;
78 }
79
80 public void setExpectedNumberOfClauses(int nb) {
81 this.out.println(" " + nb);
82 this.nbclauses = nb;
83 this.fixedNbClauses = true;
84 }
85
86 public IConstr addClause(IVecInt literals) throws ContradictionException {
87 if (this.firstConstr) {
88 if (!this.fixedNbClauses) {
89 this.out.println(" XXXXXX");
90 }
91 this.firstConstr = false;
92 }
93 for (IteratorInt iterator = literals.iterator(); iterator.hasNext();) {
94 this.out.print(iterator.next() + " ");
95 }
96 this.out.println("0");
97 return null;
98 }
99
100 public IConstr addAtMost(IVecInt literals, int degree)
101 throws ContradictionException {
102 if (degree > 1) {
103 throw new UnsupportedOperationException(
104 "Not a clausal problem! degree " + degree);
105 }
106 assert degree == 1;
107 if (this.firstConstr) {
108 if (!this.fixedNbClauses) {
109 this.out.println("XXXXXX");
110 }
111 this.firstConstr = false;
112 }
113 for (int i = 0; i <= literals.size(); i++) {
114 for (int j = i + 1; j < literals.size(); j++) {
115 this.out.println("" + -literals.get(i) + " " + -literals.get(j)
116 + " 0");
117 }
118 }
119 return null;
120 }
121
122 public IConstr addAtLeast(IVecInt literals, int degree)
123 throws ContradictionException {
124 if (degree > 1) {
125 throw new UnsupportedOperationException(
126 "Not a clausal problem! degree " + degree);
127 }
128 assert degree == 1;
129 return addClause(literals);
130 }
131
132 public IConstr addExactly(IVecInt literals, int n)
133 throws ContradictionException {
134 if (n > 1) {
135 throw new UnsupportedOperationException(
136 "Not a clausal problem! degree " + n);
137 }
138 assert n == 1;
139 addAtMost(literals, n);
140 addAtLeast(literals, n);
141 return null;
142 }
143
144 public void reset() {
145 this.fixedNbClauses = false;
146 this.firstConstr = true;
147
148 }
149
150 public String toString(String prefix) {
151 return "Dimacs output solver";
152 }
153
154 @Override
155 public int nConstraints() {
156 return this.nbclauses;
157 }
158
159 @Override
160 public int nVars() {
161 return this.nbvars;
162 }
163
164
165
166
167 public int nextFreeVarId(boolean reserve) {
168 if (reserve) {
169 return ++this.nbvars;
170 }
171 return this.nbvars + 1;
172 }
173
174
175
176
177 public int[] modelWithInternalVariables() {
178 throw new UnsupportedOperationException();
179 }
180
181
182
183
184 public int realNumberOfVariables() {
185 return this.nbvars;
186 }
187
188
189
190
191 public void registerLiteral(int p) {
192 throw new UnsupportedOperationException();
193 }
194
195
196
197
198 public boolean primeImplicant(int p) {
199 throw new UnsupportedOperationException();
200 }
201 }