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.pb;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  
35  import java.util.HashSet;
36  import java.util.Set;
37  
38  import org.junit.Test;
39  import org.sat4j.pb.tools.DependencyHelper;
40  import org.sat4j.specs.ContradictionException;
41  import org.sat4j.specs.IVec;
42  import org.sat4j.specs.TimeoutException;
43  
44  public class BugSAT22 {
45      @Test
46      public void testSimpleResolverUnitFirst() throws ContradictionException,
47              TimeoutException {
48          IPBSolver solver = SolverFactory.newEclipseP2();
49          DependencyHelper<Named, String> helper = new DependencyHelper<Named, String>(
50                  solver, false);
51          Set<Named> slice = new HashSet<Named>();
52          Named A1 = new Named("A1");
53          slice.add(A1);
54          Named A2 = new Named("A2");
55          slice.add(A2);
56          Named B = new Named("B");
57          slice.add(B);
58          Named X = new Named("X");
59          // base
60          helper.setTrue(X, "Build");
61          // objective function
62          helper.addToObjectiveFunction(A2, 1);
63          helper.addToObjectiveFunction(A1, 2);
64          helper.addToObjectiveFunction(B, 1);
65          // depends
66          helper.or("a", X, new Named[] { A1, A2 });
67          helper.or("b", X, new Named[] { B });
68          // solve
69          assertTrue(helper.hasASolution());
70          IVec<Named> solution = helper.getSolution();
71          assertEquals(3, solution.size());
72          assertTrue(solution.contains(B));
73          assertTrue(solution.contains(X));
74          assertTrue(solution.contains(A2));
75      }
76  
77      @Test
78      public void testSimpleResolverUnitLast() throws ContradictionException,
79              TimeoutException {
80          IPBSolver solver = SolverFactory.newEclipseP2();
81          DependencyHelper<Named, String> helper = new DependencyHelper<Named, String>(
82                  solver, false);
83          Set<Named> slice = new HashSet<Named>();
84          Named A1 = new Named("A1");
85          slice.add(A1);
86          Named A2 = new Named("A2");
87          slice.add(A2);
88          Named B = new Named("B");
89          slice.add(B);
90          Named X = new Named("X");
91          // objective function
92          helper.addToObjectiveFunction(A2, 1);
93          helper.addToObjectiveFunction(A1, 2);
94          helper.addToObjectiveFunction(B, 1);
95          // depends
96          helper.or("a", X, new Named[] { A1, A2 });
97          helper.or("b", X, new Named[] { B });
98          // base
99          helper.setTrue(X, "Build");
100         // solve
101         assertTrue(helper.hasASolution());
102         IVec<Named> solution = helper.getSolution();
103         assertEquals(3, solution.size());
104         assertTrue(solution.contains(B));
105         assertTrue(solution.contains(X));
106         assertTrue(solution.contains(A2));
107     }
108 
109     public class Named {
110         public String name;
111 
112         public Named(String name) {
113             this.name = name;
114         }
115     }
116 }