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  
31  package org.sat4j.pb.tools;
32  
33  import java.util.Iterator;
34  
35  import org.sat4j.core.Vec;
36  import org.sat4j.core.VecInt;
37  import org.sat4j.specs.ContradictionException;
38  import org.sat4j.specs.IConstr;
39  import org.sat4j.specs.IVec;
40  import org.sat4j.specs.IVecInt;
41  
42  /**
43   * That class is used to represent a conjunction of literals in the RHS of an
44   * implication.
45   * 
46   * @author daniel
47   * 
48   * @param <T>
49   * @param <C>
50   */
51  public class ImplicationAnd<T, C> {
52      private final DependencyHelper<T, C> helper;
53      private final IVecInt clause;
54      private final IVec<IConstr> toName = new Vec<IConstr>();
55  
56      public ImplicationAnd(DependencyHelper<T, C> helper, IVecInt clause) {
57          this.clause = clause;
58          this.helper = helper;
59      }
60  
61      /**
62       * Add a new positive literal to the conjunction of literals.
63       * 
64       * @param thing
65       *            a domain object
66       * @return a RHS conjunction of literals.
67       * @throws ContradictionException
68       */
69      public ImplicationAnd<T, C> and(T thing) throws ContradictionException {
70          IVecInt tmpClause = new VecInt();
71          this.clause.copyTo(tmpClause);
72          tmpClause.push(this.helper.getIntValue(thing));
73          IConstr constr = this.helper.solver.addClause(tmpClause);
74          if (constr != null) {
75              this.toName.push(constr);
76          }
77          return this;
78      }
79  
80      /**
81       * Add a new negative literal to the conjunction of literals.
82       * 
83       * @param thing
84       *            a domain object
85       * @return a RHS conjunction of literals.
86       * @throws ContradictionException
87       */
88      public ImplicationAnd<T, C> andNot(T thing) throws ContradictionException {
89          IVecInt tmpClause = new VecInt();
90          this.clause.copyTo(tmpClause);
91          tmpClause.push(-this.helper.getIntValue(thing));
92          IConstr constr = this.helper.solver.addClause(tmpClause);
93          if (constr != null) {
94              this.toName.push(constr);
95          }
96          return this;
97      }
98  
99      /**
100      * "name" the constraint for the explanation.
101      * 
102      * IT IS MANDATORY TO NAME ALL THE CONSTRAINTS!
103      * 
104      * @param name
105      *            an object to link to the constraint.
106      */
107     public void named(C name) {
108         for (Iterator<IConstr> it = this.toName.iterator(); it.hasNext();) {
109             this.helper.descs.put(it.next(), name);
110         }
111     }
112 }