1 /*
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3 *
4 * Based on the original minisat specification from:
5 *
6 * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7 * Sixth International Conference on Theory and Applications of Satisfiability
8 * Testing, LNCS 2919, pp 502-518, 2003.
9 *
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option)
13 * any later version.
14 *
15 * This library is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 package org.sat4j.tools;
27
28 import org.sat4j.core.VecInt;
29 import org.sat4j.specs.ContradictionException;
30 import org.sat4j.specs.ISolver;
31 import org.sat4j.specs.IVecInt;
32 import org.sat4j.specs.TimeoutException;
33
34 /**
35 * That class allows to iterate through all the models (implicants) of a
36 * formula.
37 *
38 * <pre>
39 * ISolver solver = new ModelIterator(SolverFactory.OneSolver());
40 * boolean unsat = true;
41 * while (solver.isSatisfiable()) {
42 * unsat = false;
43 * int[] model = solver.model();
44 * // do something with model
45 * }
46 * if (unsat) {
47 * // UNSAT case
48 * }
49 * </pre>
50 *
51 * @author leberre
52 */
53 public class ModelIterator extends SolverDecorator {
54
55 private static final long serialVersionUID = 1L;
56
57 private boolean trivialfalsity = false;
58
59 /**
60 * @param solver
61 */
62 public ModelIterator(ISolver solver) {
63 super(solver);
64 }
65
66 /*
67 * (non-Javadoc)
68 *
69 * @see org.sat4j.ISolver#model()
70 */
71 @Override
72 public int[] model() {
73 int[] last = super.model();
74 IVecInt clause = new VecInt(last.length);
75 for (int q : last) {
76 clause.push(-q);
77 }
78 try {
79 // System.out.println("adding " + clause);
80 addClause(clause);
81 } catch (ContradictionException e) {
82 trivialfalsity = true;
83 }
84 return last;
85 }
86
87 /*
88 * (non-Javadoc)
89 *
90 * @see org.sat4j.ISolver#isSatisfiable()
91 */
92 @Override
93 public boolean isSatisfiable() throws TimeoutException {
94 if (trivialfalsity) {
95 return false;
96 }
97 trivialfalsity = false;
98 return super.isSatisfiable();
99 }
100
101 /*
102 * (non-Javadoc)
103 *
104 * @see org.sat4j.ISolver#isSatisfiable(org.sat4j.datatype.VecInt)
105 */
106 @Override
107 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
108 if (trivialfalsity) {
109 return false;
110 }
111 trivialfalsity = false;
112 return super.isSatisfiable(assumps);
113 }
114
115 /*
116 * (non-Javadoc)
117 *
118 * @see org.sat4j.ISolver#reset()
119 */
120 @Override
121 public void reset() {
122 trivialfalsity = false;
123 super.reset();
124 }
125 }