1 /*******************************************************************************
2 * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
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 *******************************************************************************/
28 package org.sat4j.minisat.constraints.cnf;
29
30 import org.sat4j.minisat.core.ILits;
31 import org.sat4j.minisat.core.UnitPropagationListener;
32 import org.sat4j.specs.ContradictionException;
33 import org.sat4j.specs.IVecInt;
34
35 /**
36 *
37 * @author daniel
38 * @since 2.1
39 */
40 public abstract class Clauses {
41 /**
42 * Perform some sanity check before constructing a clause a) if a literal is
43 * assigned true, return null (the clause is satisfied) b) if a literal is
44 * assigned false, remove it c) if a clause contains a literal and its
45 * opposite (tautology) return null d) remove duplicate literals e) if the
46 * clause is empty, return null f) if the clause if unit, transmit it to the
47 * object responsible for unit propagation
48 *
49 * @param ps
50 * the list of literals
51 * @param voc
52 * the vocabulary used
53 * @param s
54 * the object responsible for unit propagation
55 * @return null if the clause should be ignored, the (possibly modified)
56 * list of literals otherwise
57 * @throws ContradictionException
58 * if discovered by unit propagation
59 */
60 public static IVecInt sanityCheck(IVecInt ps, ILits voc,
61 UnitPropagationListener s) throws ContradictionException {
62 // si un litt???ral de ps est vrai, retourner vrai
63 // enlever les litt???raux falsifi???s de ps
64 for (int i = 0; i < ps.size();) {
65 // on verifie si le litteral est affecte
66 if (voc.isUnassigned(ps.get(i))) {
67 // on passe au literal suivant
68 i++;
69 } else {
70 // Si le litteral est satisfait, la clause est
71 // satisfaite
72 if (voc.isSatisfied(ps.get(i))) {
73 // on retourne la clause
74 return null;
75 }
76 // on enleve le ieme litteral
77 ps.delete(i);
78
79 }
80 }
81
82 // on trie le vecteur ps
83 ps.sortUnique();
84
85 // ???limine les clauses tautologiques
86 // deux litt???raux de signe oppos???s apparaissent dans la m???me
87 // clause
88 for (int i = 0; i < ps.size() - 1; i++) {
89 if (ps.get(i) == (ps.get(i + 1) ^ 1)) {
90 // la clause est tautologique
91 return null;
92 }
93 }
94
95 if (propagationCheck(ps, s))
96 return null;
97
98 return ps;
99 }
100
101 /**
102 * Check if this clause is null or unit
103 *
104 * @param p
105 * the list of literals (supposed to be clean as after a call to
106 * sanityCheck())
107 * @param s
108 * the object responsible for unit propagation
109 * @return true iff the clause should be ignored (because it's unit)
110 * @throws ContradictionException
111 * when detected by unit propagation
112 */
113 static boolean propagationCheck(IVecInt ps, UnitPropagationListener s)
114 throws ContradictionException {
115 if (ps.size() == 0) {
116 throw new ContradictionException("Creating Empty clause ?"); //$NON-NLS-1$
117 } else if (ps.size() == 1) {
118 if (!s.enqueue(ps.get(0))) {
119 throw new ContradictionException("Contradictory Unit Clauses"); //$NON-NLS-1$
120 }
121 return true;
122 }
123
124 return false;
125 }
126
127 }