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