1 package org.sat4j.tools; 2 3 import static org.junit.Assert.assertEquals; 4 5 import java.util.List; 6 7 import org.junit.Before; 8 import org.junit.Test; 9 import org.sat4j.core.VecInt; 10 import org.sat4j.minisat.SolverFactory; 11 import org.sat4j.specs.ContradictionException; 12 import org.sat4j.specs.IGroupSolver; 13 import org.sat4j.specs.IVecInt; 14 15 public class TestAllMUSesGroupTest { 16 private IGroupSolver solver; 17 private AllMUSes allMUSes; 18 19 @Before 20 public void setUp() throws Exception { 21 this.allMUSes = new AllMUSes(true, SolverFactory.instance()); 22 this.solver = allMUSes.getSolverInstance(); 23 } 24 25 @Test 26 public void testSimpleCase() { 27 IVecInt c1 = new VecInt(); 28 IVecInt c2 = new VecInt(); 29 IVecInt c3 = new VecInt(); 30 IVecInt c4 = new VecInt(); 31 IVecInt c5 = new VecInt(); 32 33 c1.push(1); 34 c2.push(2); 35 c3.push(-1).push(-2); 36 c4.push(3); 37 c5.push(-3); 38 39 this.solver.newVar(3); 40 System.out.println("mus should be = [1,2,3 / 4,5]"); 41 42 try { 43 this.solver.addClause(c1, 1); 44 this.solver.addClause(c2, 2); 45 this.solver.addClause(c3, 3); 46 this.solver.addClause(c4, 4); 47 this.solver.addClause(c5, 5); 48 49 List<IVecInt> muses = allMUSes.computeAllMUSes(); 50 51 assertEquals(muses.size(), 2); 52 53 } catch (ContradictionException e) { 54 e.printStackTrace(); 55 } 56 57 } 58 59 @Test 60 public void testSimpleCaseWithGroups() { 61 IVecInt c1 = new VecInt(); 62 IVecInt c2 = new VecInt(); 63 IVecInt c3 = new VecInt(); 64 IVecInt c4 = new VecInt(); 65 IVecInt c5 = new VecInt(); 66 67 c1.push(1); 68 c2.push(2); 69 c3.push(-1).push(-2); 70 c4.push(3); 71 c5.push(-3); 72 73 this.solver.newVar(3); 74 75 System.out.println("mus should be = [1 / 2]"); 76 77 try { 78 this.solver.addClause(c1, 1); 79 this.solver.addClause(c2, 1); 80 this.solver.addClause(c3, 1); 81 this.solver.addClause(c4, 2); 82 this.solver.addClause(c5, 2); 83 84 List<IVecInt> muses = allMUSes.computeAllMUSes(); 85 86 assertEquals(muses.size(), 2); 87 88 } catch (ContradictionException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 @Test 94 public void testSimpleCaseWithGroups2() { 95 IVecInt c1 = new VecInt(); 96 IVecInt c2 = new VecInt(); 97 IVecInt c3 = new VecInt(); 98 IVecInt c4 = new VecInt(); 99 IVecInt c5 = new VecInt(); 100 101 c1.push(1); 102 c2.push(2); 103 c3.push(-1).push(-2); 104 c4.push(3); 105 c5.push(-3); 106 107 this.solver.newVar(3); 108 109 System.out.println("mus should be = [1,2]"); 110 111 try { 112 this.solver.addClause(c1, 1); 113 this.solver.addClause(c2, 2); 114 this.solver.addClause(c3, 1); 115 this.solver.addClause(c4, 2); 116 this.solver.addClause(c5, 1); 117 118 List<IVecInt> muses = allMUSes.computeAllMUSes(); 119 120 assertEquals(muses.size(), 1); 121 122 } catch (ContradictionException e) { 123 e.printStackTrace(); 124 } 125 } 126 127 @Test 128 public void testSimpleCaseWithGroups3() { 129 IVecInt c1 = new VecInt(); 130 IVecInt c2 = new VecInt(); 131 IVecInt c3 = new VecInt(); 132 IVecInt c4 = new VecInt(); 133 IVecInt c5 = new VecInt(); 134 135 c1.push(1); 136 c2.push(2); 137 c3.push(-1).push(-2); 138 c4.push(3); 139 c5.push(-3); 140 141 this.solver.newVar(3); 142 143 System.out.println("mus should be = [1,2]"); 144 145 try { 146 this.solver.addClause(c1, 1); 147 this.solver.addClause(c2, 2); 148 this.solver.addClause(c3, 3); 149 this.solver.addClause(c4, 2); 150 this.solver.addClause(c5, 1); 151 152 List<IVecInt> muses = allMUSes.computeAllMUSes(); 153 154 assertEquals(muses.size(), 1); 155 156 } catch (ContradictionException e) { 157 e.printStackTrace(); 158 } 159 } 160 161 @Test 162 public void testSimpleCaseWithGroups4() { 163 IVecInt c1 = new VecInt(); 164 IVecInt c2 = new VecInt(); 165 IVecInt c3 = new VecInt(); 166 IVecInt c4 = new VecInt(); 167 IVecInt c5 = new VecInt(); 168 169 c1.push(1); 170 c2.push(2); 171 c3.push(-1).push(-2); 172 c4.push(3); 173 c5.push(-3); 174 175 this.solver.newVar(3); 176 177 System.out.println("mus should be = [1,4 / 1,2,3]"); 178 179 try { 180 this.solver.addClause(c1, 1); 181 this.solver.addClause(c2, 2); 182 this.solver.addClause(c3, 3); 183 this.solver.addClause(c4, 4); 184 this.solver.addClause(c5, 1); 185 186 List<IVecInt> muses = allMUSes.computeAllMUSes(); 187 188 assertEquals(muses.size(), 2); 189 190 } catch (ContradictionException e) { 191 e.printStackTrace(); 192 } 193 } 194 195 @Test 196 public void testVerySimpleCase() { 197 IVecInt c1 = new VecInt(); 198 IVecInt c2 = new VecInt(); 199 200 c1.push(1); 201 c2.push(-1); 202 203 this.solver.newVar(1); 204 System.out.println("mus should be = [1,2]"); 205 206 try { 207 this.solver.addClause(c1, 1); 208 this.solver.addClause(c2, 2); 209 210 List<IVecInt> muses = allMUSes.computeAllMUSes(); 211 212 assertEquals(muses.size(), 1); 213 214 } catch (ContradictionException e) { 215 e.printStackTrace(); 216 } 217 } 218 }