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.minisat.core;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  import org.junit.Before;
36  import org.junit.Test;
37  import org.sat4j.core.VecInt;
38  import org.sat4j.minisat.SolverFactory;
39  import org.sat4j.minisat.orders.PositiveLiteralSelectionStrategy;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.IVecInt;
42  import org.sat4j.specs.TimeoutException;
43  
44  public class TestPrimeComputation {
45  
46      private Solver<DataStructureFactory> solver;
47  
48      @Before
49      public void setUp() {
50          this.solver = SolverFactory.newBestWL();
51          this.solver.getOrder().setPhaseSelectionStrategy(
52                  new PositiveLiteralSelectionStrategy());
53      }
54  
55      @Test
56      public void testBasicImplicant() throws ContradictionException,
57              TimeoutException {
58          IVecInt clause = new VecInt();
59          clause.push(1).push(-2).push(3);
60          this.solver.addClause(clause);
61          clause.clear();
62          clause.push(-1).push(2).push(3);
63          this.solver.addClause(clause);
64          clause.clear();
65          clause.push(1).push(2).push(-3);
66          this.solver.addClause(clause);
67          clause.clear();
68          assertTrue(this.solver.isSatisfiable());
69          int[] model = this.solver.model();
70          assertEquals(3, model.length);
71          int[] implicant = this.solver.primeImplicant();
72          assertEquals(2, implicant.length);
73      }
74  
75      @Test
76      public void testImplicantPascal() throws ContradictionException,
77              TimeoutException {
78          IVecInt clause = new VecInt();
79          clause.push(1).push(-2).push(3).push(-4);
80          this.solver.addClause(clause);
81          assertTrue(this.solver.isSatisfiable());
82          int[] model = this.solver.model();
83          assertEquals(4, model.length);
84          int[] implicant = this.solver.primeImplicant();
85          assertEquals(1, implicant.length);
86      }
87  
88      @Test
89      public void testOtherImplicant() throws ContradictionException,
90              TimeoutException {
91          IVecInt clause = new VecInt();
92          clause.push(1).push(-2).push(3);
93          this.solver.addClause(clause);
94          clause.clear();
95          clause.push(1).push(2).push(3);
96          this.solver.addClause(clause);
97          clause.clear();
98          clause.push(1).push(2).push(-3);
99          this.solver.addClause(clause);
100         clause.clear();
101         assertTrue(this.solver.isSatisfiable());
102         int[] model = this.solver.model();
103         assertEquals(3, model.length);
104         int[] implicant = this.solver.primeImplicant();
105         assertEquals(2, implicant.length);
106         clause.push(1).push(-2).push(-3);
107         this.solver.addBlockingClause(clause);
108         assertTrue(this.solver.isSatisfiable());
109         clause.clear();
110         model = this.solver.model();
111         assertEquals(3, model.length);
112         implicant = this.solver.primeImplicant();
113         assertEquals(1, implicant.length);
114     }
115 
116     @Test
117     public void testFolletExample() throws ContradictionException,
118             TimeoutException {
119         IVecInt clause = new VecInt();
120         clause.push(1);
121         this.solver.addClause(clause);
122         clause.clear();
123         clause.push(2).push(3);
124         this.solver.addClause(clause);
125         clause.clear();
126         clause.push(3);
127         this.solver.addClause(clause);
128         clause.clear();
129         assertTrue(this.solver.isSatisfiable());
130         int[] model = this.solver.model();
131         assertEquals(3, model.length);
132         int[] implicant = this.solver.primeImplicant();
133         assertEquals(2, implicant.length);
134     }
135 }