1 package org.sat4j.pb; 2 3 import static org.mockito.Mockito.inOrder; 4 import static org.mockito.Mockito.mock; 5 import static org.mockito.Mockito.verify; 6 7 import java.io.IOException; 8 import java.math.BigInteger; 9 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.mockito.InOrder; 13 import org.sat4j.core.Vec; 14 import org.sat4j.core.VecInt; 15 import org.sat4j.pb.reader.JSONPBReader; 16 import org.sat4j.reader.ParseFormatException; 17 import org.sat4j.specs.ContradictionException; 18 import org.sat4j.specs.IVec; 19 import org.sat4j.specs.IVecInt; 20 21 public class JsonPBReaderTest { 22 23 private IPBSolver solver; 24 private JSONPBReader reader; 25 26 @Before 27 public void setUp() throws Exception { 28 solver = mock(IPBSolver.class); 29 reader = new JSONPBReader(solver); 30 } 31 32 @Test 33 public void testReadingSimplePseudoAtLeast() throws ParseFormatException, 34 ContradictionException, IOException { 35 String json = "[[[[1,1],[23,-2],[-64,3]],'>=',24]]"; 36 reader.parseString(json); 37 IVecInt clause = new VecInt().push(1).push(-2).push(3); 38 IVecInt coefs = new VecInt().push(1).push(23).push(-64); 39 verify(solver).addAtLeast(clause, coefs, 24); 40 41 } 42 43 @Test 44 public void testReadingSimplePseudoAtMost() throws ParseFormatException, 45 ContradictionException, IOException { 46 String json = "[[[[1,1],[23,-2],[-64,3]],'<=',24]]"; 47 reader.parseString(json); 48 IVecInt clause = new VecInt().push(1).push(-2).push(3); 49 IVecInt coefs = new VecInt().push(1).push(23).push(-64); 50 verify(solver).addAtMost(clause, coefs, 24); 51 52 } 53 54 @Test 55 public void testReadingSimplePseudoExactly() throws ParseFormatException, 56 ContradictionException, IOException { 57 String json = "[[[[1,1],[23,-2],[-64,3]],'=',24]]"; 58 reader.parseString(json); 59 IVecInt clause = new VecInt().push(1).push(-2).push(3); 60 IVecInt coefs = new VecInt().push(1).push(23).push(-64); 61 verify(solver).addExactly(clause, coefs, 24); 62 63 } 64 65 @Test 66 public void testReadingSimplePseudoAtLeastStrictly() 67 throws ParseFormatException, ContradictionException, IOException { 68 String json = "[[[[1,1],[23,-2],[-64,3]],'>',24]]"; 69 reader.parseString(json); 70 IVecInt clause = new VecInt().push(1).push(-2).push(3); 71 IVecInt coefs = new VecInt().push(1).push(23).push(-64); 72 verify(solver).addAtLeast(clause, coefs, 25); 73 74 } 75 76 @Test 77 public void testReadingSimplePseudoAtMostStrictly() 78 throws ParseFormatException, ContradictionException, IOException { 79 String json = "[[[[1,1],[23,-2],[-64,3]],'<',24]]"; 80 reader.parseString(json); 81 IVecInt clause = new VecInt().push(1).push(-2).push(3); 82 IVecInt coefs = new VecInt().push(1).push(23).push(-64); 83 verify(solver).addAtMost(clause, coefs, 23); 84 85 } 86 87 @Test 88 public void testOrderofMixedConstraints() throws ParseFormatException, 89 ContradictionException { 90 String json = "[[-1,-2,-3],[[1,-2,3],'>',2],[4,-3,6],[[[1,1],[2,2],[4,3],[8,4]],'<=',6]]"; 91 reader.parseString(json); 92 IVecInt clause1 = new VecInt().push(-1).push(-2).push(-3); 93 IVecInt card = new VecInt().push(1).push(-2).push(3); 94 IVecInt clause2 = new VecInt().push(4).push(-3).push(6); 95 IVecInt lits = new VecInt().push(1).push(2).push(3).push(4); 96 IVecInt coefs = new VecInt().push(1).push(2).push(4).push(8); 97 InOrder inOrder = inOrder(solver); 98 inOrder.verify(solver).addClause(clause1); 99 inOrder.verify(solver).addAtLeast(card, 3); 100 inOrder.verify(solver).addClause(clause2); 101 inOrder.verify(solver).addAtMost(lits, coefs, 6); 102 } 103 104 @Test 105 public void testObjectiveFunctionMin() throws ParseFormatException, 106 ContradictionException { 107 String json = "[['min',[[1,1],[20,2],[80,3]]],[-1,-2,-3],[[1,-2,3],'>',2],[4,-3,6],[[[1,1],[2,2],[4,3],[8,4]],'<=',6]]"; 108 reader.parseString(json); 109 IVecInt objvars = new VecInt().push(1).push(2).push(3); 110 IVec<BigInteger> objcoefs = new Vec<BigInteger>() 111 .push(BigInteger.valueOf(1)).push(BigInteger.valueOf(20)) 112 .push(BigInteger.valueOf(80)); 113 ObjectiveFunction obj = new ObjectiveFunction(objvars, objcoefs); 114 IVecInt clause1 = new VecInt().push(-1).push(-2).push(-3); 115 IVecInt card = new VecInt().push(1).push(-2).push(3); 116 IVecInt clause2 = new VecInt().push(4).push(-3).push(6); 117 IVecInt lits = new VecInt().push(1).push(2).push(3).push(4); 118 IVecInt coefs = new VecInt().push(1).push(2).push(4).push(8); 119 InOrder inOrder = inOrder(solver); 120 inOrder.verify(solver).setObjectiveFunction(obj); 121 inOrder.verify(solver).addClause(clause1); 122 inOrder.verify(solver).addAtLeast(card, 3); 123 inOrder.verify(solver).addClause(clause2); 124 inOrder.verify(solver).addAtMost(lits, coefs, 6); 125 } 126 127 @Test 128 public void testObjectiveFunctionMax() throws ParseFormatException, 129 ContradictionException { 130 String json = "[['max',[[1,1],[20,2],[80,3]]],[-1,-2,-3],[[1,-2,3],'>',2],[4,-3,6],[[[1,1],[2,2],[4,3],[8,4]],'<=',6]]"; 131 reader.parseString(json); 132 IVecInt objvars = new VecInt().push(1).push(2).push(3); 133 IVec<BigInteger> objcoefs = new Vec<BigInteger>() 134 .push(new BigInteger("-1")).push(new BigInteger("-20")) 135 .push(new BigInteger("-80")); 136 ObjectiveFunction obj = new ObjectiveFunction(objvars, objcoefs); 137 IVecInt clause1 = new VecInt().push(-1).push(-2).push(-3); 138 IVecInt card = new VecInt().push(1).push(-2).push(3); 139 IVecInt clause2 = new VecInt().push(4).push(-3).push(6); 140 IVecInt lits = new VecInt().push(1).push(2).push(3).push(4); 141 IVecInt coefs = new VecInt().push(1).push(2).push(4).push(8); 142 InOrder inOrder = inOrder(solver); 143 inOrder.verify(solver).setObjectiveFunction(obj); 144 inOrder.verify(solver).addClause(clause1); 145 inOrder.verify(solver).addAtLeast(card, 3); 146 inOrder.verify(solver).addClause(clause2); 147 inOrder.verify(solver).addAtMost(lits, coefs, 6); 148 } 149 150 }