001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Vector;
025: import org.apache.commons.collections.iterators.IteratorChain;
026: import org.mandarax.kernel.AbstractClause;
027: import org.mandarax.kernel.Clause;
028: import org.mandarax.kernel.ClauseSet;
029: import org.mandarax.kernel.Fact;
030: import org.mandarax.kernel.Replacement;
031:
032: /**
033: * Simple clause class for internal use in the inference engine.
034: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
035: * @version 3.4 <7 March 05>
036: * @since 1.0
037: * Prova re-integration modifications
038: * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
039: * @version 3.4 <7 March 05>
040: */
041: public final class TmpClause extends AbstractClause {
042:
043: public List negativeLiterals = null;
044: public List positiveLiterals = null;
045:
046: /**
047: * Constructor.
048: */
049: public TmpClause() {
050: super ();
051: }
052:
053: /**
054: * Constructor.
055: * @param posLit positive literals
056: * @param negLit negative literals
057: */
058: public TmpClause(List posLit, List negLit) {
059: this ();
060:
061: negativeLiterals = negLit;
062: positiveLiterals = posLit;
063: }
064:
065: /**
066: * Apply a set of substitutions.
067: * @return a clause that is the result of applying the replacements
068: * @param r a collection of replacements
069: */
070: public Clause apply(Collection r) {
071: TmpClause c = new TmpClause();
072:
073: c.positiveLiterals = new Vector(positiveLiterals.size());
074: c.negativeLiterals = new Vector(negativeLiterals.size());
075:
076: for (Iterator it = positiveLiterals.iterator(); it.hasNext();) {
077: c.positiveLiterals.add(((Fact) it.next()).apply(r));
078: }
079:
080: for (Iterator it = negativeLiterals.iterator(); it.hasNext();) {
081: c.negativeLiterals.add(((Fact) it.next()).apply(r));
082: }
083:
084: return c;
085: }
086:
087: /**
088: * Apply a substitutions.
089: * @return a clause that is the result of applying the replacement
090: * @param r a replacement
091: */
092: public Clause apply(Replacement r) {
093: TmpClause c = new TmpClause();
094:
095: c.positiveLiterals = new Vector(positiveLiterals.size());
096: c.negativeLiterals = new Vector(negativeLiterals.size());
097:
098: for (Iterator it = positiveLiterals.iterator(); it.hasNext();) {
099: c.positiveLiterals.add(((Fact) it.next()).apply(r));
100: }
101:
102: for (Iterator it = negativeLiterals.iterator(); it.hasNext();) {
103: c.negativeLiterals.add(((Fact) it.next()).apply(r));
104: }
105:
106: return c;
107: }
108:
109: /**
110: * Get the key.
111: * @return the key object
112: */
113: public Object getKey() {
114: try {
115: return ((Fact) negativeLiterals.get(0)).getPredicate();
116: } catch (Throwable t) {
117: LOG_KB.error("Error getting key of a clause !", t);
118: }
119:
120: return null;
121: }
122:
123: public boolean isBound() {
124: return false;
125: }
126:
127: /**
128: * Get the negative literals.
129: * @return a list containing the negative literals
130: */
131: public List getNegativeLiterals() {
132: return negativeLiterals;
133: }
134:
135: /**
136: * Get the positive literals.
137: * @return a list containing the positive literals
138: */
139: public List getPositiveLiterals() {
140: return positiveLiterals;
141: }
142:
143: /**
144: * Indicates whether the clause is atomic.
145: * @return a boolean
146: */
147: public boolean isAtomic() {
148: return true;
149: }
150:
151: /**
152: * Indicate whether this is the empty clause.
153: * @return true if there are no literals, false otherwise
154: */
155: public boolean isEmpty() {
156: return negativeLiterals.isEmpty() && positiveLiterals.isEmpty();
157: }
158:
159: /**
160: * Indicates whether the object (usually a term or a clause set) can be performed
161: * using the java semantics.
162: * @return a boolean
163: */
164: public boolean isExecutable() {
165: return false;
166: }
167:
168: /**
169: * Convert the receiver to a string.
170: * @return the string representation of this object
171: */
172: public String toString() {
173: StringBuffer buf = new StringBuffer();
174: boolean first = true;
175: buf.append("+[");
176: if (positiveLiterals != null) {
177: for (Iterator it = positiveLiterals.iterator(); it
178: .hasNext();) {
179: if (first) {
180: first = false;
181: } else {
182: buf.append(",");
183: }
184: buf.append(it.next().toString());
185: }
186: }
187: buf.append("] , -[");
188: first = true;
189: if (negativeLiterals != null) {
190: for (Iterator it = negativeLiterals.iterator(); it
191: .hasNext();) {
192: if (first) {
193: first = false;
194: } else {
195: buf.append(",");
196: }
197: buf.append(it.next().toString());
198: }
199: }
200: buf.append("]");
201: return new String(buf);
202: }
203:
204: /**
205: * Get an iterator iterating over the predicates contained in this clause set.
206: * @return an iterator
207: */
208: public Iterator predicates() {
209: List chain = new ArrayList();
210: if (negativeLiterals != null) {
211: for (int i = 0; i < negativeLiterals.size(); i++) {
212: chain.add(((ClauseSet) negativeLiterals.get(i))
213: .predicates());
214: }
215: }
216: if (positiveLiterals != null) {
217: for (int i = 0; i < positiveLiterals.size(); i++) {
218: chain.add(((ClauseSet) positiveLiterals.get(i))
219: .predicates());
220: }
221: }
222: return new IteratorChain(chain);
223: }
224:
225: /**
226: * Indicates whether the clause is ground (= does not have variables).
227: * @return a boolean
228: */
229: public boolean isGround() {
230: if (this .positiveLiterals != null) {
231: for (int i = 0; i < this .positiveLiterals.size(); i++) {
232: if (!((Clause) this .positiveLiterals.get(i)).isGround())
233: return false;
234: }
235: }
236: if (this .negativeLiterals != null) {
237: for (int i = 0; i < this .negativeLiterals.size(); i++) {
238: if (!((Clause) this .negativeLiterals.get(i)).isGround())
239: return false;
240: }
241: }
242: return true;
243: }
244: }
|