| 
 1 | 
  
 |  | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 |  | 
| 
 4 | 
  
 |  | 
| 
 5 | 
  
 |  | 
| 
 6 | 
  
 |  | 
| 
 7 | 
  
 | package org.sat4j.tools; | 
| 
 8 | 
  
 |  | 
| 
 9 | 
  
 | import java.io.Serializable; | 
| 
 10 | 
  
 |  | 
| 
 11 | 
  
 | import org.sat4j.core.VecInt; | 
| 
 12 | 
  
 | import org.sat4j.specs.ContradictionException; | 
| 
 13 | 
  
 | import org.sat4j.specs.ISolver; | 
| 
 14 | 
  
 | import org.sat4j.specs.IVecInt; | 
| 
 15 | 
  
 | import org.sat4j.specs.TimeoutException; | 
| 
 16 | 
  
 |  | 
| 
 17 | 
  
 |  | 
| 
 18 | 
  
 |  | 
| 
 19 | 
  
 |  | 
| 
 20 | 
  
 |  | 
| 
 21 | 
  
 |  | 
| 
 22 | 
  
 |  | 
| 
 23 | 
  
 |  | 
| 
 24 | 
  
 |  | 
| 
 25 | 
  
 |  | 
| 
 26 | 
  
 |  | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |  | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |  | 
| 
 31 | 
  
 |  | 
| 
 32 | 
  
 |  | 
| 
 33 | 
  
 |  | 
| 
 34 | 
  
 |  | 
| 
 35 | 
  
 |  | 
| 
 36 | 
  
 | public class ModelIterator extends SolverDecorator implements Serializable { | 
| 
 37 | 
  
 |  | 
| 
 38 | 
  
 |     private static final long serialVersionUID = 1L; | 
| 
 39 | 
  
 |  | 
| 
 40 | 
  
 |     private boolean trivialfalsity = false; | 
| 
 41 | 
  
 |  | 
| 
 42 | 
  
 |      | 
| 
 43 | 
  
 |  | 
| 
 44 | 
  
 |  | 
| 
 45 | 
 1
 |     public ModelIterator(ISolver solver) {
 | 
| 
 46 | 
 1
 |         super(solver);
 | 
| 
 47 | 
  
 |     } | 
| 
 48 | 
  
 |  | 
| 
 49 | 
  
 |      | 
| 
 50 | 
  
 |  | 
| 
 51 | 
  
 |  | 
| 
 52 | 
  
 |  | 
| 
 53 | 
  
 |  | 
| 
 54 | 
 6
 |     @Override
 | 
| 
 55 | 
  
 |     public int[] model() { | 
| 
 56 | 
 6
 |         int[] last = super.model();
 | 
| 
 57 | 
 6
 |         IVecInt clause = new VecInt(last.length);
 | 
| 
 58 | 
 6
 |         for (int q : last) {
 | 
| 
 59 | 
 18
 |             clause.push(-q);
 | 
| 
 60 | 
  
 |         } | 
| 
 61 | 
 6
 |         try {
 | 
| 
 62 | 
  
 |              | 
| 
 63 | 
 6
 |             addClause(clause);
 | 
| 
 64 | 
  
 |         } catch (ContradictionException e) { | 
| 
 65 | 
 0
 |             trivialfalsity = true;
 | 
| 
 66 | 
  
 |         } | 
| 
 67 | 
 6
 |         return last;
 | 
| 
 68 | 
  
 |     } | 
| 
 69 | 
  
 |  | 
| 
 70 | 
  
 |      | 
| 
 71 | 
  
 |  | 
| 
 72 | 
  
 |  | 
| 
 73 | 
  
 |  | 
| 
 74 | 
  
 |  | 
| 
 75 | 
 7
 |     @Override
 | 
| 
 76 | 
  
 |     public boolean isSatisfiable() throws TimeoutException { | 
| 
 77 | 
 7
 |         if (trivialfalsity) {
 | 
| 
 78 | 
 0
 |             return false;
 | 
| 
 79 | 
  
 |         } | 
| 
 80 | 
 7
 |         trivialfalsity = false;
 | 
| 
 81 | 
 7
 |         return super.isSatisfiable();
 | 
| 
 82 | 
  
 |     } | 
| 
 83 | 
  
 |  | 
| 
 84 | 
  
 |      | 
| 
 85 | 
  
 |  | 
| 
 86 | 
  
 |  | 
| 
 87 | 
  
 |  | 
| 
 88 | 
  
 |  | 
| 
 89 | 
 0
 |     @Override
 | 
| 
 90 | 
  
 |     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException { | 
| 
 91 | 
 0
 |         if (trivialfalsity) {
 | 
| 
 92 | 
 0
 |             return false;
 | 
| 
 93 | 
  
 |         } | 
| 
 94 | 
 0
 |         trivialfalsity = false;
 | 
| 
 95 | 
 0
 |         return super.isSatisfiable(assumps);
 | 
| 
 96 | 
  
 |     } | 
| 
 97 | 
  
 |  | 
| 
 98 | 
  
 |      | 
| 
 99 | 
  
 |  | 
| 
 100 | 
  
 |  | 
| 
 101 | 
  
 |  | 
| 
 102 | 
  
 |  | 
| 
 103 | 
 0
 |     @Override
 | 
| 
 104 | 
  
 |     public void reset() { | 
| 
 105 | 
 0
 |         trivialfalsity = false;
 | 
| 
 106 | 
 0
 |         super.reset();
 | 
| 
 107 | 
  
 |     } | 
| 
 108 | 
  
 | } |