View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j.minisat.constraints;
27  
28  import java.math.BigInteger;
29  
30  import org.sat4j.core.VecInt;
31  import org.sat4j.minisat.constraints.cnf.LearntWLClause;
32  import org.sat4j.minisat.constraints.cnf.Lits;
33  import org.sat4j.minisat.constraints.cnf.WLClause;
34  import org.sat4j.minisat.constraints.pb.IInternalPBConstraintCreator;
35  import org.sat4j.minisat.constraints.pb.PBConstr;
36  import org.sat4j.minisat.core.Constr;
37  import org.sat4j.minisat.core.ILits;
38  import org.sat4j.specs.ContradictionException;
39  import org.sat4j.specs.IConstr;
40  import org.sat4j.specs.IVec;
41  import org.sat4j.specs.IVecInt;
42  
43  /**
44   * @author leberre To change the template for this generated type comment go to
45   *         Window>Preferences>Java>Code Generation>Code and Comments
46   */
47  public abstract class AbstractPBDataStructureFactory extends
48          AbstractDataStructureFactory<ILits> implements IInternalPBConstraintCreator {
49  
50      public Constr createClause(IVecInt literals) throws ContradictionException {
51          IVecInt v = WLClause.sanityCheck(literals, getVocabulary(), solver);
52          if (v == null)
53              return null;
54          IVecInt coefs = new VecInt(v.size(), 1);
55          return constraintFactory(v, coefs, true, 1);
56      }
57  
58      public Constr createUnregisteredClause(IVecInt literals) {
59          return new LearntWLClause(literals, getVocabulary());
60      }
61  
62      @Override
63      public Constr createCardinalityConstraint(IVecInt literals, int degree)
64              throws ContradictionException {
65          IVecInt coefs = new VecInt(literals.size(), 1);
66          return constraintFactory(literals, coefs, true, degree);
67      }
68  
69      @Override
70      public Constr createPseudoBooleanConstraint(IVecInt literals,
71              IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
72              throws ContradictionException {
73          return constraintFactory(literals, coefs, moreThan, degree);
74      }
75  
76      /**
77       * @param literals the literals
78       * @param coefs the coefficients
79       * @param moreThan 
80       * @param degree the degree of the constraint
81       * @return a new PB constraint
82       */
83      protected abstract PBConstr constraintFactory(IVecInt literals,
84              IVecInt coefs, boolean moreThan, int degree)
85              throws ContradictionException;
86  
87      protected abstract PBConstr constraintFactory(IVecInt literals,
88              IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
89              throws ContradictionException;
90  
91      @Override
92      public Constr createUnregisteredPseudoBooleanConstraint(IVecInt literals,
93              IVec<BigInteger> coefs, BigInteger degree) {
94          return constraintFactory(literals, coefs, degree);
95      }
96  
97      public IConstr createUnregisteredPseudoBooleanConstraint(IVecInt literals,
98              IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
99              throws ContradictionException {
100         return constraintFactory(literals, coefs, moreThan, degree);
101     }
102 
103     /**
104      * @param literals
105      * @param coefs
106      * @param degree
107      * @return a new PB constraint
108      */
109     protected abstract PBConstr constraintFactory(IVecInt literals,
110             IVecInt coefs, int degree);
111 
112     protected abstract PBConstr constraintFactory(IVecInt literals,
113             IVec<BigInteger> coefs, BigInteger degree);
114 
115     @Override
116     protected ILits createLits() {
117         return new Lits();
118     }
119 
120 }