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