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