package ws.prova.reference;

/*
 * Copyright (C) 1999-2005 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import java.util.Map;

import org.mandarax.kernel.*;

/**
 * Reference implementation of the Prerequisite interface.
 *
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 3.3.1 <9 August 2004>
 * @since 2.0
 *        Prova re-integration modifications
 */
public class PrerequisiteImpl extends FactImpl implements Prerequisite {
	private boolean negatedAF = false;
	private Object extra = null;

	/**
	 * Constructor.
	 */
	public PrerequisiteImpl() {
		super();
	}

	/**
	 * Constructor.
	 *
	 * @param aPredicate a predicate
	 * @param terms      the terms
	 * @param negatedAF  whether the prerequisite is negated
	 */
	PrerequisiteImpl(Predicate aPredicate, Term[] terms, boolean negatedAF) {
		super(aPredicate, terms);
		setNegatedAF(negatedAF);
	}

	/**
	 * Indicates whether the prerequisite is negated (negation as failure)
	 *
	 * @return true if the prerequisite is negated, false otherwise
	 */
	public boolean isNegatedAF() {
		return negatedAF;
	}

	/**
	 * Set the negated (as failure) flag.
	 *
	 * @param value true or false
	 */
	public void setNegatedAF(boolean value) {
		boolean fireEvent = negatedAF != value;
		negatedAF = value;
		if (fireEvent) fireClauseSetChangeEvent(new ClauseSetChangeEvent(this, this, this, ClauseSetChangeEvent.OTHER));

	}

	/**
	 * Convert the object to a string.
	 *
	 * @return the string representation of the object
	 */
	public String toString() {
		return negatedAF ? ("not " + super.toString()) : super.toString();
	}

	/**
	 * Apply a set of replacements to a fact. Returns a new fact!
	 *
	 * @param r a collection of replacement
	 *          Prova: processing of "extra"
	 * @return the fact resulting from the application of the replacement
	 */
	public Fact applyToFact(java.util.Collection r) {
        if( isBound() )
            return this;
        Term[] t = getTerms();
		Term newTerm = null;

		// create a new fact
		PrerequisiteImpl p = new PrerequisiteImpl();
		p.setNegatedAF(isNegatedAF());
		p.setExtra(getExtra());
		copyStructure(p);
		for (int i = 0; i < t.length; i++) {
			newTerm = t[i];
            for (java.util.Iterator it = r.iterator(); it.hasNext();) {
				newTerm = newTerm.apply((Replacement) it.next());
			}
			p.setTerm(newTerm, i);
		}
		return p;
	}



	/**
	 * Apply a single replacement to a fact. Returns a new fact!
	 *
	 * @param r a replacement
	 * @return the fact (clause) resulting from the application of the replacement
	 */
	public Fact applyToFact(Replacement r) {
		Term[] t = getTerms();
		// create a new fact
		PrerequisiteImpl p = new PrerequisiteImpl();
		copyStructure(p);
		p.setNegatedAF(this.isNegatedAF());
		p.setExtra(getExtra());
		for (int i = 0; i < t.length; i++) {
			p.setTerm(t[i].apply(r), i);
		}
		return p;
	}

	/**
	 * Compares objects.
	 *
	 * @param obj the object to compare this object with
	 * @return true if the objects are equal, false otherwise
	 */
	public boolean equals(Object obj) {
		if (obj instanceof Prerequisite && super.equals(obj)) {
			return isNegatedAF() == ((Prerequisite) obj).isNegatedAF();
		}
		return false;
	}

	/**
	 * @return
	 */
	public Object getExtra() {
		return extra;
	}

	/**
	 * @param _extra
	 */
	public void setExtra(Object _extra) {
		extra = _extra;
	}

	/**
	 * Apply a set of replacements to a fact. Returns a new fact!
	 *
	 * @param r a collection of replacement
	 *          Prova: processing of "extra"
	 * @return the fact resulting from the application of the replacement
	 */
	public Fact applyToFactMinimally(java.util.Collection r, Map complexReplacements) {
		Term[] t = getTerms();
		
		boolean changed = false;
		Term[] newTerm = new Term[getTerms().length];
		for (int i = 0; i < t.length; i++) {
			newTerm[i] = t[i].applyMinimally(r,complexReplacements);
			if( newTerm[i]!=t[i] )
				changed = true;
		}		
		if( changed ) {
			// create a new fact
			PrerequisiteImpl p = new PrerequisiteImpl();
			p.setNegatedAF(isNegatedAF());
			p.setExtra(getExtra());
			copyStructure(p);
			p.setTerms(newTerm);
			return p;
		}
		return this;
	}

}
