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;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  import org.junit.Test;
37  import org.sat4j.core.ConstrGroup;
38  import org.sat4j.core.VecInt;
39  import org.sat4j.minisat.SolverFactory;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.ISolver;
42  import org.sat4j.specs.IVecInt;
43  import org.sat4j.specs.TimeoutException;
44  
45  public class TestConstrGroup {
46  
47      @Test
48      public void testDeleteGroup() throws ContradictionException {
49          ISolver solver = SolverFactory.newDefault();
50          ConstrGroup g1 = new ConstrGroup();
51          IVecInt clause = new VecInt(new int[] { 1, 2, -3 });
52          solver.addClause(clause);
53          // starting group
54          clause.clear();
55          clause.push(2).push(-3).push(-5);
56          g1.add(solver.addClause(clause));
57          clause.clear();
58          clause.push(-3).push(-2).push(-4);
59          g1.add(solver.addClause(clause));
60          assertEquals(3, solver.nConstraints());
61          g1.removeFrom(solver);
62          assertEquals(1, solver.nConstraints());
63      }
64  
65      @Test
66      public void canPutAUnitClauseInAGroup() throws ContradictionException {
67          ISolver solver = SolverFactory.newDefault();
68          ConstrGroup g1 = new ConstrGroup();
69  
70          IVecInt clause = new VecInt(new int[] { 1 });
71          g1.add(solver.addClause(clause));
72          assertEquals(1, solver.nConstraints());
73      }
74  
75      @Test
76      public void checkBugReportedByThomas() throws ContradictionException {
77          ISolver solver = SolverFactory.newDefault();
78          ConstrGroup g1 = new ConstrGroup();
79  
80          IVecInt clause = new VecInt(new int[] { 1 });
81          solver.addClause(clause);
82  
83          // starting group
84          clause.clear();
85          clause.push(2).push(-3).push(-5);
86          g1.add(solver.addClause(clause));
87  
88          clause.clear();
89          clause.push(-3).push(-2).push(-4);
90          g1.add(solver.addClause(clause));
91          assertEquals(3, solver.nConstraints());
92  
93          g1.removeFrom(solver);
94          assertEquals(1, solver.nConstraints());
95      }
96  
97      @Test
98      public void checkItWorksAfterRunningTheSolver()
99              throws ContradictionException, TimeoutException {
100         ISolver solver = SolverFactory.newDefault();
101         ConstrGroup g1 = new ConstrGroup();
102 
103         IVecInt clause = new VecInt(new int[] { 1 });
104         solver.addClause(clause);
105 
106         // starting group
107         clause.clear();
108         clause.push(-1).push(-2).push(-3);
109         g1.add(solver.addClause(clause));
110 
111         clause.clear();
112         clause.push(-1).push(2).push(-3);
113         g1.add(solver.addClause(clause));
114         assertEquals(3, solver.nConstraints());
115         assertTrue(solver.isSatisfiable());
116         assertTrue(solver.model(1));
117         assertFalse(solver.model(3));
118         g1.removeFrom(solver);
119         assertEquals(1, solver.nConstraints());
120     }
121 
122     @Test
123     public void checkGroupDoesWorkWhenClausesAreReducedByUnitPropgation()
124             throws ContradictionException {
125         ISolver solver = SolverFactory.newDefault();
126         ConstrGroup g1 = new ConstrGroup();
127 
128         IVecInt clause = new VecInt(new int[] { 1 });
129         solver.addClause(clause);
130 
131         // starting group
132         clause.clear();
133         clause.push(-1).push(-2);
134         g1.add(solver.addClause(clause));
135         assertEquals(1, g1.size());
136     }
137 
138     @Test
139     public void checkTheExpectedWayToDealWithUnitClausesToRemove()
140             throws ContradictionException, TimeoutException {
141         ISolver solver = SolverFactory.newDefault();
142         ConstrGroup g1 = new ConstrGroup();
143 
144         IVecInt clause = new VecInt(new int[] { 1 });
145         solver.addClause(clause);
146 
147         // starting group
148         clause.clear();
149         clause.push(2).push(-3);
150         g1.add(solver.addClause(clause));
151 
152         clause.clear();
153         clause.push(-2).push(4);
154         g1.add(solver.addClause(clause));
155 
156         IVecInt unitClauses = new VecInt(new int[] { 3, -4 });
157 
158         assertFalse(solver.isSatisfiable(unitClauses));
159 
160         g1.removeFrom(solver);
161         assertTrue(solver.isSatisfiable(unitClauses));
162     }
163 }