1 /*******************************************************************************
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Alternatively, the contents of this file may be used under the terms of
10 * either the GNU Lesser General Public License Version 2.1 or later (the
11 * "LGPL"), in which case the provisions of the LGPL are applicable instead
12 * of those above. If you wish to allow use of your version of this file only
13 * under the terms of the LGPL, and not to allow others to use your version of
14 * this file under the terms of the EPL, indicate your decision by deleting
15 * the provisions above and replace them with the notice and other provisions
16 * required by the LGPL. If you do not delete the provisions above, a recipient
17 * may use your version of this file under the terms of the EPL or the LGPL.
18 *
19 * Based on the original MiniSat specification from:
20 *
21 * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22 * Sixth International Conference on Theory and Applications of Satisfiability
23 * Testing, LNCS 2919, pp 502-518, 2003.
24 *
25 * See www.minisat.se for the original solver in C++.
26 *
27 *******************************************************************************/
28 package org.sat4j.tools;
29
30 import java.io.Serializable;
31
32 import org.sat4j.core.VecInt;
33 import org.sat4j.specs.ContradictionException;
34 import org.sat4j.specs.ISolver;
35 import org.sat4j.specs.IVecInt;
36
37 /**
38 * Very simple Dimacs array reader. Allow solvers to read the constraints from
39 * arrays that effectively contain Dimacs formatted lines (without the
40 * terminating 0).
41 *
42 * Adaptation of org.sat4j.reader.DimacsReader.
43 *
44 * @author dlb
45 * @author fuhs
46 */
47 public class DimacsArrayReader implements Serializable {
48
49 private static final long serialVersionUID = 1L;
50
51 protected final ISolver solver;
52
53 public DimacsArrayReader(ISolver solver) {
54 this.solver = solver;
55 }
56
57 protected boolean handleConstr(int gateType, int output, int[] inputs)
58 throws ContradictionException {
59 IVecInt literals = new VecInt(inputs);
60 solver.addClause(literals);
61 return true;
62 }
63
64 /**
65 * @param gateType
66 * gateType[i] is the type of gate i according to the Extended
67 * Dimacs specs; ignored in DimacsArrayReader, but important for
68 * inheriting classes
69 * @param outputs
70 * outputs[i] is the number of the output; ignored in
71 * DimacsArrayReader
72 * @param inputs
73 * inputs[i] contains the clauses in DimacsArrayReader; an
74 * overriding class might have it contain the inputs of the
75 * current gate
76 * @param maxVar
77 * the maximum number of assigned ids
78 * @throws ContradictionException
79 * si le probleme est trivialement inconsitant
80 */
81 public ISolver parseInstance(int[] gateType, int[] outputs,
82 int[][] inputs, int maxVar) throws ContradictionException {
83 solver.reset();
84 solver.newVar(maxVar);
85 solver.setExpectedNumberOfClauses(outputs.length);
86 for (int i = 0; i < outputs.length; ++i) {
87 handleConstr(gateType[i], outputs[i], inputs[i]);
88 }
89 return solver;
90 }
91
92 public String decode(int[] model) {
93 StringBuffer stb = new StringBuffer(4 * model.length);
94 for (int i = 0; i < model.length; i++) {
95 stb.append(model[i]);
96 stb.append(" ");
97 }
98 stb.append("0");
99 return stb.toString();
100 }
101
102 protected ISolver getSolver() {
103 return solver;
104 }
105 }