View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.tools;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  import org.junit.Test;
36  import org.sat4j.core.VecInt;
37  import org.sat4j.minisat.SolverFactory;
38  import org.sat4j.specs.ContradictionException;
39  import org.sat4j.specs.ISolver;
40  import org.sat4j.specs.IVecInt;
41  import org.sat4j.specs.TimeoutException;
42  
43  /**
44   * Testcase to check that the problem with unit clauses does no longer occurs.
45   * 
46   * Bug report from Sebastian Henneberg
47   * 
48   * 
49   */
50  public class RemiUtilsTest {
51  
52      @Test
53      public void testBugUnitClauses() throws ContradictionException,
54              TimeoutException {
55          ISolver solver1 = SolverFactory.newDefault();
56          ISolver solver2 = SolverFactory.newDefault();
57          ISolver solver3 = SolverFactory.newDefault();
58  
59          int[][] cnf1 = new int[][] { new int[] { 1 }, new int[] { 1, -2 },
60                  new int[] { 1, -3 }, new int[] { -1, 2 } };
61          // A & (A v -B) & (A v -C) & (-A v B)
62          // (-A v B) & (A v -B) & (A v -C) & A | using a different order
63          int[][] cnf2 = new int[][] { new int[] { -1, 2 }, new int[] { 1, -2 },
64                  new int[] { 1, -3 }, new int[] { 1 } };
65          // (-A v B) & (A v -B) & (A v -C) & A
66          // (-A v C) & (A v -C) & (A v -B) & A | swap B and C (2 <-> 3)
67          // (A v -B) & (-A v C) & (A v -C) & A | shift the first 3 clauses to the
68          // right
69          int[][] cnf3 = new int[][] { new int[] { 1, -2 }, new int[] { -1, 3 },
70                  new int[] { 1, -3 }, new int[] { 1 } };
71  
72          for (int[] is : cnf1) {
73              solver1.addClause(new VecInt(is));
74          }
75          for (int[] is : cnf2) {
76              solver2.addClause(new VecInt(is));
77          }
78          for (int[] is : cnf3) {
79              solver3.addClause(new VecInt(is));
80          }
81  
82          IVecInt vecInt1 = RemiUtils.backbone(solver1);
83          assertEquals(vecInt1.size(), 2);
84          assertTrue(vecInt1.contains(1));
85          assertTrue(vecInt1.contains(2));
86  
87          IVecInt vecInt2 = RemiUtils.backbone(solver2);
88          assertEquals(vecInt2.size(), 2);
89          assertTrue(vecInt2.contains(1));
90          assertTrue(vecInt2.contains(2));
91  
92          IVecInt vecInt3 = RemiUtils.backbone(solver3);
93          assertEquals(vecInt3.size(), 2);
94          assertTrue(vecInt3.contains(1));
95          assertTrue(vecInt3.contains(3));
96      }
97  }