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 |
|
package org.sat4j; |
26 |
|
|
27 |
|
import java.io.BufferedReader; |
28 |
|
import java.io.FileNotFoundException; |
29 |
|
import java.io.IOException; |
30 |
|
import java.io.InputStreamReader; |
31 |
|
import java.io.ObjectInputStream; |
32 |
|
import java.io.PrintWriter; |
33 |
|
import java.io.Serializable; |
34 |
|
import java.lang.management.ManagementFactory; |
35 |
|
import java.net.URL; |
36 |
|
import java.util.Properties; |
37 |
|
|
38 |
|
import org.sat4j.reader.ParseFormatException; |
39 |
|
import org.sat4j.reader.Reader; |
40 |
|
import org.sat4j.specs.ContradictionException; |
41 |
|
import org.sat4j.specs.IProblem; |
42 |
|
import org.sat4j.specs.ISolver; |
43 |
|
import org.sat4j.specs.TimeoutException; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@author |
50 |
|
|
51 |
|
|
|
|
| 0% |
Uncovered Elements: 120 (120) |
Complexity: 18 |
Complexity Density: 0,41 |
|
52 |
|
public abstract class AbstractLauncher implements Serializable { |
53 |
|
|
54 |
|
public static final String SOLUTION_PREFIX = "v "; |
55 |
|
|
56 |
|
public static final String ANSWER_PREFIX = "s "; |
57 |
|
|
58 |
|
public static final String COMMENT_PREFIX = "c "; |
59 |
|
|
60 |
|
private long beginTime; |
61 |
|
|
62 |
|
private ExitCode exitCode = ExitCode.UNKNOWN; |
63 |
|
|
64 |
|
protected Reader reader; |
65 |
|
|
66 |
|
private transient PrintWriter out = new PrintWriter(System.out, true); |
67 |
|
|
68 |
|
private static final boolean cputimesupported = ManagementFactory.getThreadMXBean().isCurrentThreadCpuTimeSupported(); |
69 |
|
|
70 |
|
protected transient Thread shutdownHook = new Thread() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
71 |
0
|
@Override... |
72 |
|
public void run() { |
73 |
0
|
displayResult(); |
74 |
|
} |
75 |
|
}; |
76 |
|
|
77 |
|
protected ISolver solver; |
78 |
|
|
79 |
|
private boolean silent = false; |
80 |
|
|
81 |
|
private double cputime; |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0
|
protected AbstractLauncher() {... |
84 |
0
|
Runtime.getRuntime().addShutdownHook(shutdownHook); |
85 |
|
} |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0,33 |
|
87 |
0
|
protected void displayResult() {... |
88 |
0
|
if (solver != null) { |
89 |
0
|
double wallclocktime = (System.currentTimeMillis() - beginTime) / 1000.0; |
90 |
0
|
solver.printStat(out, COMMENT_PREFIX); |
91 |
0
|
out.println(ANSWER_PREFIX + exitCode); |
92 |
0
|
if (exitCode == ExitCode.SATISFIABLE) { |
93 |
0
|
int[] model = solver.model(); |
94 |
0
|
out.print(SOLUTION_PREFIX); |
95 |
0
|
reader.decode(model, out); |
96 |
0
|
out.println(); |
97 |
|
} |
98 |
0
|
log("Total wall clock time (in seconds) : " + wallclocktime); |
99 |
0
|
if (cputimesupported) { |
100 |
0
|
log("Total CPU time (experimental, in seconds) : " + cputime) ; |
101 |
|
} |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
protected abstract void usage(); |
106 |
|
|
107 |
|
|
108 |
|
@throws |
109 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 6 |
Complexity Density: 0,25 |
|
110 |
0
|
protected final void displayHeader() {... |
111 |
0
|
log("SAT4J: a SATisfiability library for Java (c) 2004-2007 Daniel Le Berre"); |
112 |
0
|
log("This is free software under the GNU LGPL licence. See www.sat4j.org for details."); |
113 |
0
|
log("This software uses some libraries from the Jakarta project. See jakarta.apache.org for details."); |
114 |
0
|
URL url = Lanceur.class.getResource("/sat4j.version"); |
115 |
0
|
if (url == null) { |
116 |
0
|
log("no version file found!!!"); |
117 |
|
} else { |
118 |
0
|
BufferedReader in = null; |
119 |
0
|
try { |
120 |
0
|
in = new BufferedReader(new InputStreamReader(url |
121 |
|
.openStream())); |
122 |
0
|
log("version " + in.readLine()); |
123 |
|
} catch (IOException e) { |
124 |
0
|
log("c ERROR: "+e.getMessage()); |
125 |
|
} finally { |
126 |
0
|
if (in!=null) { |
127 |
0
|
try { |
128 |
0
|
in.close(); |
129 |
|
} catch (IOException e) { |
130 |
0
|
log("c ERROR: "+e.getMessage()); |
131 |
|
} |
132 |
|
} |
133 |
|
} |
134 |
|
} |
135 |
0
|
Properties prop = System.getProperties(); |
136 |
0
|
String[] infoskeys = { |
137 |
|
"sun.arch.data.model", "java.version", "os.name", "os.version", "os.arch" }; |
138 |
0
|
for (String key : infoskeys) { |
139 |
0
|
log(key + "\t" + prop.getProperty(key)); |
140 |
|
} |
141 |
0
|
Runtime runtime = Runtime.getRuntime(); |
142 |
0
|
log("Free memory " + runtime.freeMemory()); |
143 |
0
|
log("Max memory " + runtime.maxMemory()); |
144 |
0
|
log("Total memory " + runtime.totalMemory()); |
145 |
0
|
log("Number of processors " + runtime.availableProcessors()); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
|
153 |
|
@param |
154 |
|
|
155 |
|
@param |
156 |
|
|
157 |
|
@return |
158 |
|
@throws |
159 |
|
|
160 |
|
@throws |
161 |
|
|
162 |
|
@throws |
163 |
|
|
164 |
|
@throws |
165 |
|
|
166 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0,2 |
|
167 |
0
|
protected IProblem readProblem(String problemname)... |
168 |
|
throws FileNotFoundException, ParseFormatException, IOException, |
169 |
|
ContradictionException { |
170 |
0
|
log("solving " + problemname); |
171 |
0
|
log("reading problem ... "); |
172 |
0
|
reader = createReader(solver, problemname); |
173 |
0
|
IProblem problem = reader.parseInstance(problemname); |
174 |
0
|
log("... done. Wall clock time " |
175 |
|
+ (System.currentTimeMillis() - beginTime) / 1000.0 + "s."); |
176 |
0
|
if (cputimesupported) { |
177 |
0
|
log("CPU time (experimental) "+ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime() /1000000000.0+"s."); |
178 |
|
} |
179 |
0
|
log("#vars " + solver.nVars()); |
180 |
0
|
log("#constraints " + solver.nConstraints()); |
181 |
0
|
return problem; |
182 |
|
} |
183 |
|
|
184 |
|
protected abstract Reader createReader(ISolver solver, String problemname); |
185 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 8 |
Complexity Density: 0,38 |
|
186 |
0
|
public void run(String[] args) {... |
187 |
|
|
188 |
0
|
try { |
189 |
0
|
displayHeader(); |
190 |
0
|
solver = configureSolver(args); |
191 |
0
|
if (solver == null) |
192 |
0
|
return; |
193 |
0
|
String instanceName = getInstanceName(args); |
194 |
0
|
beginTime = System.currentTimeMillis(); |
195 |
0
|
IProblem problem = readProblem(instanceName); |
196 |
0
|
try { |
197 |
0
|
solve(problem); |
198 |
|
} catch (TimeoutException e) { |
199 |
0
|
log("timeout"); |
200 |
|
} |
201 |
|
} catch (FileNotFoundException e) { |
202 |
0
|
log("FATAL"); |
203 |
0
|
e.printStackTrace(); |
204 |
|
} catch (IOException e) { |
205 |
0
|
log("FATAL"); |
206 |
0
|
e.printStackTrace(); |
207 |
|
} catch (ContradictionException e) { |
208 |
0
|
exitCode = ExitCode.UNSATISFIABLE; |
209 |
0
|
log("(trivial inconsistency)"); |
210 |
|
} catch (ParseFormatException e) { |
211 |
0
|
log("FATAL"); |
212 |
0
|
e.printStackTrace(); |
213 |
|
} |
214 |
0
|
if (cputimesupported) { |
215 |
0
|
cputime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime() /1000000000.0; |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
protected abstract String getInstanceName(String[] args); |
220 |
|
|
221 |
|
protected abstract ISolver configureSolver(String[] args); |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
@param |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
228 |
0
|
protected void log(String message) {... |
229 |
0
|
if (!silent) |
230 |
0
|
out.println(COMMENT_PREFIX + message); |
231 |
|
} |
232 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 1 |
|
233 |
0
|
protected void solve(IProblem problem) throws TimeoutException {... |
234 |
0
|
exitCode = problem.isSatisfiable() ? ExitCode.SATISFIABLE |
235 |
|
: ExitCode.UNSATISFIABLE; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@param |
242 |
|
|
243 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
0
|
public final void setExitCode(ExitCode exitCode) {... |
245 |
0
|
this.exitCode = exitCode; |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
@return |
252 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
253 |
0
|
public final ExitCode getExitCode() {... |
254 |
0
|
return exitCode; |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
@return |
262 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
263 |
0
|
public final long getBeginTime() {... |
264 |
0
|
return beginTime; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
@return |
270 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
271 |
0
|
public final Reader getReader() {... |
272 |
0
|
return reader; |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
@param |
280 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
281 |
0
|
public void setLogWriter(PrintWriter out) {... |
282 |
0
|
this.out = out; |
283 |
|
} |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
285 |
0
|
public PrintWriter getLogWriter() {... |
286 |
0
|
return out; |
287 |
|
} |
288 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
289 |
0
|
protected void setSilent(boolean b) {... |
290 |
0
|
silent = b; |
291 |
|
} |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
293 |
0
|
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {... |
294 |
0
|
stream.defaultReadObject(); |
295 |
0
|
out = new PrintWriter(System.out, true); |
296 |
0
|
shutdownHook = new Thread() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
0
|
@Override... |
298 |
|
public void run() { |
299 |
0
|
displayResult(); |
300 |
|
} |
301 |
|
}; |
302 |
|
} |
303 |
|
} |