|
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.FileInputStream; |
|
32 |
| import java.io.IOException; |
|
33 |
| import java.io.Serializable; |
|
34 |
| import java.util.zip.GZIPInputStream; |
|
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 |
| public class LecteurDimacs implements Reader, Serializable { |
|
50 |
| |
|
51 |
| private static final long serialVersionUID = 1L; |
|
52 |
| |
|
53 |
| |
|
54 |
| private final static int TAILLE_BUF = 16384; |
|
55 |
| |
|
56 |
| private ISolver s; |
|
57 |
| |
|
58 |
| private transient BufferedInputStream in; |
|
59 |
| |
|
60 |
| |
|
61 |
| private int nbLit = 0; |
|
62 |
| |
|
63 |
| private static final char EOF = (char) -1; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
2026
| public LecteurDimacs(ISolver s) {
|
|
69 |
2026
| this.s = s;
|
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
1646
| public IProblem parseInstance(String nomFichier) throws IOException,
|
|
76 |
| ContradictionException { |
|
77 |
1646
| if (nomFichier.endsWith(".gz")) {
|
|
78 |
0
| in = new BufferedInputStream(new GZIPInputStream(
|
|
79 |
| new FileInputStream(nomFichier), TAILLE_BUF)); |
|
80 |
| } else { |
|
81 |
1646
| in = new BufferedInputStream(new FileInputStream(nomFichier),
|
|
82 |
| TAILLE_BUF); |
|
83 |
| } |
|
84 |
1646
| s.reset();
|
|
85 |
1646
| char car = passerCommentaire();
|
|
86 |
1646
| if (nbLit == 0)
|
|
87 |
0
| throw new IOException(
|
|
88 |
| "DIMACS non valide (nombre de Literaux non valide)"); |
|
89 |
1646
| s.newVar(nbLit);
|
|
90 |
1646
| car = passerEspaces();
|
|
91 |
1646
| if (car == EOF)
|
|
92 |
0
| throw new IOException("DIMACS non valide (o? sont les clauses ?)");
|
|
93 |
1646
| ajouterClauses(car);
|
|
94 |
1646
| in.close();
|
|
95 |
1646
| return s;
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
1646
| private char passerCommentaire() throws IOException {
|
|
100 |
1646
| char car;
|
|
101 |
1646
| for (;;) {
|
|
102 |
22406
| car = passerEspaces();
|
|
103 |
22406
| if (car == 'p') {
|
|
104 |
1646
| car = lectureNombreLiteraux();
|
|
105 |
| } |
|
106 |
22406
| if (car != 'c' && car != 'p')
|
|
107 |
1646
| break;
|
|
108 |
20760
| car = nextLine();
|
|
109 |
20760
| if (car == EOF)
|
|
110 |
0
| break;
|
|
111 |
| } |
|
112 |
1646
| return car;
|
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
1646
| private char lectureNombreLiteraux() throws IOException {
|
|
117 |
1646
| char car = nextChiffre();
|
|
118 |
1646
| if (car != EOF) {
|
|
119 |
1646
| nbLit = car - '0';
|
|
120 |
1646
| for (;;) {
|
|
121 |
4556
| car = (char) in.read();
|
|
122 |
4556
| if (car < '0' || car > '9')
|
|
123 |
1646
| break;
|
|
124 |
2910
| nbLit = 10 * nbLit + (car - '0');
|
|
125 |
| } |
|
126 |
1646
| if (car != EOF)
|
|
127 |
1646
| nextLine();
|
|
128 |
| } |
|
129 |
1646
| return car;
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
1646
| private void ajouterClauses(char car) throws IOException,
|
|
134 |
| ContradictionException { |
|
135 |
1646
| final IVecInt lit = new VecInt();
|
|
136 |
1646
| int val = 0;
|
|
137 |
1646
| boolean neg = false;
|
|
138 |
1646
| for (;;) {
|
|
139 |
| |
|
140 |
16306090
| if (car == '-') {
|
|
141 |
7207708
| neg = true;
|
|
142 |
7207708
| car = (char) in.read();
|
|
143 |
9098382
| } else if (car == '+')
|
|
144 |
0
| car = (char) in.read();
|
|
145 |
| else |
|
146 |
9098382
| if (car >= '0' && car <= '9') {
|
|
147 |
9098382
| val = car - '0';
|
|
148 |
9098382
| car = (char) in.read();
|
|
149 |
| } else |
|
150 |
0
| break;
|
|
151 |
| |
|
152 |
16306090
| while (car >= '0' && car <= '9') {
|
|
153 |
25361960
| val = (val * 10) + car - '0';
|
|
154 |
25361960
| car = (char) in.read();
|
|
155 |
| } |
|
156 |
16306090
| if (val == 0) {
|
|
157 |
4000706
| s.addClause(lit);
|
|
158 |
4000706
| lit.clear();
|
|
159 |
| } else { |
|
160 |
| |
|
161 |
| |
|
162 |
12305384
| lit.push(neg ? -val : val);
|
|
163 |
12305384
| neg = false;
|
|
164 |
12305384
| val = 0;
|
|
165 |
| } |
|
166 |
16306090
| if (car != EOF)
|
|
167 |
16305706
| car = passerEspaces();
|
|
168 |
16306090
| if (car == EOF)
|
|
169 |
1646
| break;
|
|
170 |
| } |
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
16329758
| private char passerEspaces() throws IOException {
|
|
175 |
16329758
| char car;
|
|
176 |
?
| while ((car = (char) in.read()) == ' ' || car == '\n')
|
|
177 |
| ; |
|
178 |
16329758
| return car;
|
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
22406
| private char nextLine() throws IOException {
|
|
183 |
22406
| char car;
|
|
184 |
22406
| do {
|
|
185 |
555738
| car = (char) in.read();
|
|
186 |
555738
| } while ((car != '\n') && (car != EOF));
|
|
187 |
22406
| return car;
|
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
1646
| private char nextChiffre() throws IOException {
|
|
192 |
1646
| char car;
|
|
193 |
1646
| do {
|
|
194 |
9876
| car = (char) in.read();
|
|
195 |
9876
| } while ((car < '0') || (car > '9') && (car != EOF));
|
|
196 |
1646
| return car;
|
|
197 |
| } |
|
198 |
| |
|
199 |
0
| public String decode(int[] model) {
|
|
200 |
0
| StringBuffer stb = new StringBuffer();
|
|
201 |
0
| for (int i = 0; i < model.length; i++) {
|
|
202 |
0
| stb.append(model[i]);
|
|
203 |
0
| stb.append(" ");
|
|
204 |
| } |
|
205 |
0
| stb.append("0");
|
|
206 |
0
| return stb.toString();
|
|
207 |
| } |
|
208 |
| } |