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