Clover Coverage Report
Coverage timestamp: mer. juin 27 2007 07:27:16 CEST
19   125   5   3,8
4   47   0,47   5
5     1,8  
1    
 
  ModelIterator       Line # 53 19 5 53,6% 0.53571427
 
  (1)
 
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  1 toggle public ModelIterator(ISolver solver) {
63  1 super(solver);
64    }
65   
66    /*
67    * (non-Javadoc)
68    *
69    * @see org.sat4j.ISolver#model()
70    */
 
71  6 toggle @Override
72    public int[] model() {
73  6 int[] last = super.model();
74  6 IVecInt clause = new VecInt(last.length);
75  6 for (int q : last) {
76  18 clause.push(-q);
77    }
78  6 try {
79    // System.out.println("adding " + clause);
80  6 addClause(clause);
81    } catch (ContradictionException e) {
82  0 trivialfalsity = true;
83    }
84  6 return last;
85    }
86   
87    /*
88    * (non-Javadoc)
89    *
90    * @see org.sat4j.ISolver#isSatisfiable()
91    */
 
92  7 toggle @Override
93    public boolean isSatisfiable() throws TimeoutException {
94  7 if (trivialfalsity) {
95  0 return false;
96    }
97  7 trivialfalsity = false;
98  7 return super.isSatisfiable();
99    }
100   
101    /*
102    * (non-Javadoc)
103    *
104    * @see org.sat4j.ISolver#isSatisfiable(org.sat4j.datatype.VecInt)
105    */
 
106  0 toggle @Override
107    public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
108  0 if (trivialfalsity) {
109  0 return false;
110    }
111  0 trivialfalsity = false;
112  0 return super.isSatisfiable(assumps);
113    }
114   
115    /*
116    * (non-Javadoc)
117    *
118    * @see org.sat4j.ISolver#reset()
119    */
 
120  0 toggle @Override
121    public void reset() {
122  0 trivialfalsity = false;
123  0 super.reset();
124    }
125    }