/*
 * Copyright (C) 1999-2004 <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
 */
package org.mandarax.kernel;

import java.util.Iterator;

import org.apache.commons.collections.iterators.ArrayIterator;

/**
 * Goal of a derivation step.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.0
 * Prova re-integration
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 3.3.1 <9 August 2004>
 */
public final class Goal extends AbstractClause {

    private Fact                  fact        = null;
    private static java.util.List posLiterals = new java.util.Vector ();
    private java.util.List        negLiterals = null;

    /**
     * Constructor.
     * @param query a query fact
     */
    public Goal(Fact query) {
        super ();

        fact = query;
    }

    /**
     * Add a clause set listener.
     * Nothing to do here.
     * @param l the listener
     */
    public void addClauseSetChangeListener(ClauseSetChangeListener l) {}

    /**
     * Apply a set of substitutions to a fact.
     * @see org.mandarax.kernel.Replacement
     * @return the clause that is the result of applying the replacements
     * @param r the collection of replacements
     */
    public Clause apply(java.util.Collection r) {
        return(fact != null)
              ? new Goal (fact.applyToFact (r))
              : null;
    }

    /**
     * Apply a substitution to a fact.
     * @param r the replacement applied
     * @return the clause that is the result of applying the replacement
     */
    public Clause apply(Replacement r) {
        return(fact != null)
              ? new Goal (fact.applyToFact (r))
              : null;
    }

    /**
     * Get the key for the object.
     * @return the key object
     */
    public Object getKey() {
        return fact.getKey ();
    }

	 public boolean isBound() {
		 return false;
	 }

    /**
     * Get the negative literals.
     * @return the list of negative literals
     */
    public java.util.List getNegativeLiterals() {
        if(negLiterals == null) {
            negLiterals = new java.util.ArrayList (1);
            negLiterals.add (fact);
        }
        return negLiterals;
    }

    /**
     * Get the positive literals.
     * @return the list of positive literals
     */
    public java.util.List getPositiveLiterals() {
        return posLiterals;
    }

    /**
     * Indicates whether the clause is atomic.
     * @return a boolean
     */
    public boolean isAtomic() {
        return true;
    }

    /**
     * Indicates whether the clause is the empty clause.
     * @return true if the goal is empty, false otherwise
     */
    public boolean isEmpty() {
        return getNegativeLiterals ().isEmpty ();
    }

    /**
     * Remove a clause set listener.
     * Nothing to do here.
     * @param l the listener
     */
    public void removeClauseSetChangeListener(ClauseSetChangeListener l) {}

    /**
     * Convert the object to a string.
     * @return the string presentation of this object
     */
    public String toString() {
        return "? " + fact.toString ();
    }

    /**
     * Indicates whether the object (usually a term or a clause set) can be performed
     * using the java semantics.
     * @return a boolean
     */
    public boolean isExecutable() {
        return fact.isExecutable ();
    }
	/**
	 * Get an iterator iterating over the predicates contained in this clause set.
	 * @return an iterator
	 */
	public Iterator predicates() {
		return fact==null?new ArrayIterator(new Predicate[]{}):fact.predicates();
	}
}
