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.tools;
31  
32  import java.util.Collection;
33  
34  import org.sat4j.core.VecInt;
35  import org.sat4j.specs.ISolver;
36  import org.sat4j.specs.IVecInt;
37  import org.sat4j.specs.IteratorInt;
38  import org.sat4j.specs.TimeoutException;
39  
40  public abstract class AbstractClauseSelectorSolver<T extends ISolver> extends
41          SolverDecorator<T> {
42  
43      private static final long serialVersionUID = 1L;
44      private int lastCreatedVar;
45      private boolean pooledVarId = false;
46  
47      private interface SelectorState {
48  
49          boolean isSatisfiable(boolean global) throws TimeoutException;
50  
51          boolean isSatisfiable() throws TimeoutException;
52  
53          boolean isSatisfiable(IVecInt assumps) throws TimeoutException;
54  
55          boolean isSatisfiable(IVecInt assumps, boolean global)
56                  throws TimeoutException;
57  
58      }
59  
60      private final SelectorState external = new SelectorState() {
61  
62          private IVecInt getNegatedSelectors() {
63              IVecInt assumps = new VecInt();
64              for (int var : getAddedVars()) {
65                  assumps.push(-var);
66              }
67              return assumps;
68          }
69  
70          public boolean isSatisfiable(boolean global) throws TimeoutException {
71              return decorated().isSatisfiable(getNegatedSelectors(), global);
72          }
73  
74          public boolean isSatisfiable(IVecInt assumps, boolean global)
75                  throws TimeoutException {
76              IVecInt all = getNegatedSelectors();
77              assumps.copyTo(all);
78              return decorated().isSatisfiable(all, global);
79          }
80  
81          public boolean isSatisfiable() throws TimeoutException {
82              return decorated().isSatisfiable(getNegatedSelectors());
83          }
84  
85          public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
86              IVecInt all = getNegatedSelectors();
87              assumps.copyTo(all);
88              return decorated().isSatisfiable(all);
89          }
90  
91      };
92  
93      private final SelectorState internal = new SelectorState() {
94  
95          public boolean isSatisfiable(boolean global) throws TimeoutException {
96              return decorated().isSatisfiable(global);
97          }
98  
99          public boolean isSatisfiable() throws TimeoutException {
100             return decorated().isSatisfiable();
101         }
102 
103         public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
104             return decorated().isSatisfiable(assumps);
105         }
106 
107         public boolean isSatisfiable(IVecInt assumps, boolean global)
108                 throws TimeoutException {
109             return decorated().isSatisfiable(assumps, global);
110         }
111     };
112 
113     private SelectorState selectedState = external;
114 
115     public AbstractClauseSelectorSolver(T solver) {
116         super(solver);
117     }
118 
119     public abstract Collection<Integer> getAddedVars();
120 
121     /**
122      * 
123      * @param literals
124      * @return
125      * @since 2.1
126      */
127     protected int createNewVar(IVecInt literals) {
128         for (IteratorInt it = literals.iterator(); it.hasNext();) {
129             if (Math.abs(it.next()) > nextFreeVarId(false)) {
130                 throw new IllegalStateException(
131                         "Please call newVar(int) before adding constraints!!!");
132             }
133         }
134         if (this.pooledVarId) {
135             this.pooledVarId = false;
136             return this.lastCreatedVar;
137         }
138         this.lastCreatedVar = nextFreeVarId(true);
139         return this.lastCreatedVar;
140     }
141 
142     protected void discardLastestVar() {
143         this.pooledVarId = true;
144     }
145 
146     @Override
147     public boolean isSatisfiable(boolean global) throws TimeoutException {
148         return selectedState.isSatisfiable(global);
149     }
150 
151     @Override
152     public boolean isSatisfiable(IVecInt assumps, boolean global)
153             throws TimeoutException {
154         return selectedState.isSatisfiable(assumps, global);
155     }
156 
157     @Override
158     public boolean isSatisfiable() throws TimeoutException {
159         return selectedState.isSatisfiable();
160     }
161 
162     @Override
163     public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
164         return selectedState.isSatisfiable(assumps);
165     }
166 
167     /**
168      * In the internal state, the solver will allow the selector variables to be
169      * satisfied. As such, the solver will answer "true" to isSatisfiable() on
170      * an UNSAT problem. it is the responsibility of the user to take into
171      * account the meaning of the selector variables.
172      */
173     public void internalState() {
174         this.selectedState = internal;
175     }
176 
177     /**
178      * In external state, the solver will prevent the selector variables to be
179      * satisfied. As a consequence, from an external point of view, an UNSAT
180      * problem will answer "false" to isSatisfiable().
181      */
182 
183     public void externalState() {
184         this.selectedState = external;
185     }
186 }