1 /*******************************************************************************
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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 * Contributors:
28 * CRIL - initial API and implementation
29 *******************************************************************************/
30 package org.sat4j.tools;
31
32 import java.io.Serializable;
33
34 import org.sat4j.core.VecInt;
35 import org.sat4j.specs.ContradictionException;
36 import org.sat4j.specs.ISolver;
37 import org.sat4j.specs.IVecInt;
38
39 /**
40 * Very simple Dimacs array reader. Allow solvers to read the constraints from
41 * arrays that effectively contain Dimacs formatted lines (without the
42 * terminating 0).
43 *
44 * Adaptation of org.sat4j.reader.DimacsReader.
45 *
46 * @author dlb
47 * @author fuhs
48 */
49 public class DimacsArrayReader implements Serializable {
50
51 private static final long serialVersionUID = 1L;
52
53 protected final ISolver solver;
54
55 public DimacsArrayReader(ISolver solver) {
56 this.solver = solver;
57 }
58
59 protected boolean handleConstr(int gateType, int output, int[] inputs)
60 throws ContradictionException {
61 IVecInt literals = new VecInt(inputs);
62 this.solver.addClause(literals);
63 return true;
64 }
65
66 /**
67 * @param gateType
68 * gateType[i] is the type of gate i according to the Extended
69 * Dimacs specs; ignored in DimacsArrayReader, but important for
70 * inheriting classes
71 * @param outputs
72 * outputs[i] is the number of the output; ignored in
73 * DimacsArrayReader
74 * @param inputs
75 * inputs[i] contains the clauses in DimacsArrayReader; an
76 * overriding class might have it contain the inputs of the
77 * current gate
78 * @param maxVar
79 * the maximum number of assigned ids
80 * @throws ContradictionException
81 * si le probleme est trivialement inconsitant
82 */
83 public ISolver parseInstance(int[] gateType, int[] outputs, int[][] inputs,
84 int maxVar) throws ContradictionException {
85 this.solver.reset();
86 this.solver.newVar(maxVar);
87 this.solver.setExpectedNumberOfClauses(outputs.length);
88 for (int i = 0; i < outputs.length; ++i) {
89 handleConstr(gateType[i], outputs[i], inputs[i]);
90 }
91 return this.solver;
92 }
93
94 public String decode(int[] model) {
95 StringBuffer stb = new StringBuffer(4 * model.length);
96 for (int element : model) {
97 stb.append(element);
98 stb.append(" ");
99 }
100 stb.append("0");
101 return stb.toString();
102 }
103
104 protected ISolver getSolver() {
105 return this.solver;
106 }
107 }