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.reader;
29
30 import java.io.BufferedInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.PrintWriter;
34 import java.io.Serializable;
35
36 import org.sat4j.core.VecInt;
37 import org.sat4j.specs.ContradictionException;
38 import org.sat4j.specs.IProblem;
39 import org.sat4j.specs.ISolver;
40 import org.sat4j.specs.IVecInt;
41
42
43
44
45
46
47
48
49
50
51
52
53 public class LecteurDimacs extends Reader implements Serializable {
54
55 private static final long serialVersionUID = 1L;
56
57
58 private final static int TAILLE_BUF = 16384;
59
60 private final ISolver s;
61
62 private transient BufferedInputStream in;
63
64
65 private int nbVars = -1;
66
67 private int nbClauses = -1;
68
69 private static final char EOF = (char) -1;
70
71
72
73
74 public LecteurDimacs(ISolver s) {
75 this.s = s;
76 }
77
78 @Override
79 public final IProblem parseInstance(final InputStream input)
80 throws ParseFormatException, ContradictionException, IOException {
81
82 this.in = new BufferedInputStream(input, LecteurDimacs.TAILLE_BUF);
83 s.reset();
84 passerCommentaire();
85 if (nbVars < 0)
86 throw new ParseFormatException(
87 "DIMACS error: wrong max number of variables");
88 s.newVar(nbVars);
89 s.setExpectedNumberOfClauses(nbClauses);
90 char car = passerEspaces();
91 if (nbClauses > 0) {
92 if (car == EOF)
93 throw new ParseFormatException(
94 "DIMACS error: the clauses are missing");
95 ajouterClauses(car);
96 }
97 input.close();
98 return s;
99 }
100
101 @Override
102 public IProblem parseInstance(java.io.Reader input) throws IOException,
103 ContradictionException {
104 throw new UnsupportedOperationException();
105 }
106
107
108 private char passerCommentaire() throws IOException {
109 char car;
110 for (;;) {
111 car = passerEspaces();
112 if (car == 'p') {
113 car = lectureNombreLiteraux();
114 }
115 if (car != 'c' && car != 'p')
116 break;
117 car = nextLine();
118 if (car == EOF)
119 break;
120 }
121 return car;
122 }
123
124
125 private char lectureNombreLiteraux() throws IOException {
126 char car = nextChiffre();
127 if (car != EOF) {
128 nbVars = car - '0';
129 for (;;) {
130 car = (char) in.read();
131 if (car < '0' || car > '9')
132 break;
133 nbVars = 10 * nbVars + (car - '0');
134 }
135 car = nextChiffre();
136 nbClauses = car - '0';
137 for (;;) {
138 car = (char) in.read();
139 if (car < '0' || car > '9')
140 break;
141 nbClauses = 10 * nbClauses + (car - '0');
142 }
143 if (car != '\n' && car != EOF)
144 nextLine();
145 }
146 return car;
147 }
148
149
150
151
152
153
154 private void ajouterClauses(char car) throws IOException,
155 ContradictionException, ParseFormatException {
156 final IVecInt lit = new VecInt();
157 int val = 0;
158 boolean neg = false;
159 for (;;) {
160
161 if (car == '-') {
162 neg = true;
163 car = (char) in.read();
164 } else if (car == '+')
165 car = (char) in.read();
166 else
167 if (car >= '0' && car <= '9') {
168 val = car - '0';
169 car = (char) in.read();
170 } else
171 throw new ParseFormatException("Unknown character " + car);
172
173 while (car >= '0' && car <= '9') {
174 val = (val * 10) + car - '0';
175 car = (char) in.read();
176 }
177 if (val == 0) {
178 s.addClause(lit);
179 lit.clear();
180 } else {
181
182
183 lit.push(neg ? -val : val);
184 neg = false;
185 val = 0;
186 }
187 if (car != EOF)
188 car = passerEspaces();
189 if (car == EOF) {
190 if (!lit.isEmpty()) {
191 s.addClause(lit);
192 }
193 break;
194 }
195 }
196 }
197
198
199 private char passerEspaces() throws IOException {
200 char car;
201
202 do {
203 car = (char) in.read();
204 } while (car == ' ' || car == '\n');
205
206 return car;
207 }
208
209
210 private char nextLine() throws IOException {
211 char car;
212 do {
213 car = (char) in.read();
214 } while ((car != '\n') && (car != EOF));
215 return car;
216 }
217
218
219 private char nextChiffre() throws IOException {
220 char car;
221 do {
222 car = (char) in.read();
223 } while ((car < '0') || (car > '9') && (car != EOF));
224 return car;
225 }
226
227 @Override
228 public String decode(int[] model) {
229 StringBuffer stb = new StringBuffer();
230 for (int i = 0; i < model.length; i++) {
231 stb.append(model[i]);
232 stb.append(" ");
233 }
234 stb.append("0");
235 return stb.toString();
236 }
237
238 @Override
239 public void decode(int[] model, PrintWriter out) {
240 for (int i = 0; i < model.length; i++) {
241 out.print(model[i]);
242 out.print(" ");
243 }
244 out.print("0");
245 }
246 }