Clover Coverage Report
Coverage timestamp: mer. juin 27 2007 07:27:16 CEST
13   110   3   4,33
2   34   0,38   3
3     1,67  
1    
 
  SingleSolutionDetector       Line # 59 13 3 88,9% 0.8888889
 
  (2)
 
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    package org.sat4j.tools;
26   
27    import org.sat4j.core.VecInt;
28    import org.sat4j.specs.ContradictionException;
29    import org.sat4j.specs.IConstr;
30    import org.sat4j.specs.ISolver;
31    import org.sat4j.specs.IVecInt;
32    import org.sat4j.specs.TimeoutException;
33   
34    /**
35    * This solver decorator allows to detect whether or not the set of constraints
36    * available in the solver has only one solution or not.
37    *
38    * NOTE THAT THIS DECORATOR CANNOT BE USED WITH SOLVERS USING SPECIFIC DATA
39    * STRUCTURES FOR BINARY OR TERNARY CLAUSES!
40    *
41    * <code>
42    SingleSolutionDetector problem =
43    new SingleSolutionDetector(SolverFactory.newMiniSAT());
44    // feed problem/solver as usual
45   
46    if (problem.isSatisfiable()) {
47    if (problem.hasASingleSolution()) {
48    // great, the instance has a unique solution
49    int [] uniquesolution = problem.getModel();
50    } else {
51    // too bad, got more than one
52    }
53    }
54    * </code>
55    *
56    * @author leberre
57    *
58    */
 
59    public class SingleSolutionDetector extends SolverDecorator {
60   
61    /**
62    *
63    */
64    private static final long serialVersionUID = 1L;
65   
 
66  2 toggle public SingleSolutionDetector(ISolver solver) {
67  2 super(solver);
68    }
69   
70    /**
71    * Please use that method only after a positive answer from isSatisfiable()
72    * (else a runtime exception will be launched).
73    *
74    * @return true iff there is only one way to satisfy all the constraints in
75    * the solver.
76    * @throws TimeoutException
77    */
 
78  3 toggle public boolean hasASingleSolution() throws TimeoutException {
79  3 return hasASingleSolution(new VecInt());
80    }
81   
82    /**
83    * Please use that method only after a positive answer from
84    * isSatisfiable(assumptions) (else a runtime exception will be launched).
85    *
86    * @param assumptions
87    * a set of literals (dimacs numbering) that must be satisfied.
88    * @return true iff there is only one way to satisfy all the constraints in
89    * the solver using the provided set of assumptions.
90    * @throws TimeoutException
91    */
 
92  6 toggle public boolean hasASingleSolution(IVecInt assumptions)
93    throws TimeoutException {
94  6 int[] firstmodel = model();
95  4 assert firstmodel != null;
96  4 IVecInt clause = new VecInt(firstmodel.length);
97  4 for (int q : firstmodel) {
98  8 clause.push(-q);
99    }
100  4 boolean result = false;
101  4 try {
102  4 IConstr added = addClause(clause);
103  4 result = !isSatisfiable(assumptions);
104  4 removeConstr(added);
105    } catch (ContradictionException e) {
106  0 result = true;
107    }
108  4 return result;
109    }
110    }