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:
021: import org.mandarax.kernel.ClauseSetChangeEvent;
022: import org.mandarax.kernel.Fact;
023: import org.mandarax.kernel.Predicate;
024: import org.mandarax.kernel.Prerequisite;
025: import org.mandarax.kernel.Replacement;
026: import org.mandarax.kernel.Term;
027:
028: /**
029: * Reference implementation of the Prerequisite interface.
030: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
031: * @version 3.4 <7 March 05>
032: * @since 2.0
033: * Prova re-integration modifications
034: * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
035: * @version 3.4 <7 March 05>
036: */
037: public class PrerequisiteImpl extends FactImpl implements Prerequisite {
038: private boolean negatedAF = false;
039: private Object extra = null;
040:
041: /**
042: * Constructor.
043: */
044: public PrerequisiteImpl() {
045: super ();
046: }
047:
048: /**
049: * Constructor.
050: * @param aPredicate a predicate
051: * @param terms the terms
052: * @param negatedAF whether the prerequisite is negated
053: */
054: PrerequisiteImpl(Predicate aPredicate, Term[] terms,
055: boolean negatedAF) {
056: super (aPredicate, terms);
057: setNegatedAF(negatedAF);
058: }
059:
060: /**
061: * Indicates whether the prerequisite is negated (negation as failure)
062: * @return true if the prerequisite is negated, false otherwise
063: */
064: public boolean isNegatedAF() {
065: return negatedAF;
066: }
067:
068: /**
069: * Set the negated (as failure) flag.
070: * @param value true or false
071: */
072: public void setNegatedAF(boolean value) {
073: boolean fireEvent = negatedAF != value;
074: negatedAF = value;
075: if (fireEvent)
076: fireClauseSetChangeEvent(new ClauseSetChangeEvent(this ,
077: this , this , ClauseSetChangeEvent.OTHER));
078:
079: }
080:
081: /**
082: * Convert the object to a string.
083: * @return the string representation of the object
084: */
085: public String toString() {
086: return negatedAF ? ("not " + super .toString()) : super
087: .toString();
088: }
089:
090: /**
091: * Apply a set of replacements to a fact. Returns a new fact!
092: * @return the fact resulting from the application of the replacement
093: * @param r a collection of replacement
094: * Prova: processing of "extra"
095: */
096: public Fact applyToFact(java.util.Collection r) {
097: Term[] t = getTerms();
098: Term newTerm = null;
099:
100: // create a new fact
101: PrerequisiteImpl p = new PrerequisiteImpl();
102: p.setNegatedAF(isNegatedAF());
103: p.setExtra(getExtra());
104: copyStructure(p);
105: for (int i = 0; i < t.length; i++) {
106: newTerm = t[i];
107: for (java.util.Iterator it = r.iterator(); it.hasNext();) {
108: newTerm = newTerm.apply((Replacement) it.next());
109: }
110: p.setTerm(newTerm, i);
111: }
112: return p;
113: }
114:
115: /**
116: * Apply a single replacement to a fact. Returns a new fact!
117: * @return the fact (clause) resulting from the application of the replacement
118: * @param r a replacement
119: */
120: public Fact applyToFact(Replacement r) {
121: Term[] t = getTerms();
122: // create a new fact
123: PrerequisiteImpl p = new PrerequisiteImpl();
124: copyStructure(p);
125: p.setNegatedAF(this .isNegatedAF());
126: for (int i = 0; i < t.length; i++) {
127: p.setTerm(t[i].apply(r), i);
128: }
129: return p;
130: }
131:
132: /**
133: * Compares objects.
134: * @param obj the object to compare this object with
135: * @return true if the objects are equal, false otherwise
136: */
137: public boolean equals(Object obj) {
138: if (obj instanceof Prerequisite && super .equals(obj)) {
139: return isNegatedAF() == ((Prerequisite) obj).isNegatedAF();
140: }
141: return false;
142: }
143:
144: /**
145: *
146: * @return
147: */
148: public Object getExtra() {
149: return extra;
150: }
151:
152: /**
153: *
154: * @param _extra
155: */
156: public void setExtra(Object _extra) {
157: extra = _extra;
158: }
159:
160: }
|