package org.mandarax.reference;

/*
 * 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
 */

import org.apache.commons.collections.iterators.IteratorChain;
import org.mandarax.kernel.*;

import java.util.*;

/**
 * Simple clause class for internal use in the inference engine.
 *
 * @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 1.0
 *        Prova re-integration modifications
 */
public final class TmpClause extends AbstractClause {
	public List negativeLiterals = null;
	public List positiveLiterals = null;

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

	/**
	 * Constructor.
	 *
	 * @param posLit positive literals
	 * @param negLit negative literals
	 */
	public TmpClause( List posLit, List negLit ) {
		this();

		negativeLiterals = negLit;
		positiveLiterals = posLit;
	}

	/**
	 * Apply a set of substitutions.
	 *
	 * @param r a collection of replacements
	 * @return a clause that is the result of applying the replacements
	 */
	public Clause apply( Collection r ) {
		TmpClause c = new TmpClause();

		c.positiveLiterals = new Vector(positiveLiterals.size());
		c.negativeLiterals = new Vector(negativeLiterals.size());

		for( Iterator it = positiveLiterals.iterator(); it.hasNext(); ) {
			c.positiveLiterals.add(((Fact) it.next()).apply(r));
		}

		for( Iterator it = negativeLiterals.iterator(); it.hasNext(); ) {
			c.negativeLiterals.add(((Fact) it.next()).apply(r));
		}

		return c;
	}

	/**
	 * Apply a substitutions.
	 *
	 * @param r a replacement
	 * @return a clause that is the result of applying the replacement
	 */
	public Clause apply( Replacement r ) {
		TmpClause c = new TmpClause();

		c.positiveLiterals = new Vector(positiveLiterals.size());
		c.negativeLiterals = new Vector(negativeLiterals.size());

		for( Iterator it = positiveLiterals.iterator(); it.hasNext(); ) {
			c.positiveLiterals.add(((Fact) it.next()).apply(r));
		}

		for( Iterator it = negativeLiterals.iterator(); it.hasNext(); ) {
			c.negativeLiterals.add(((Fact) it.next()).apply(r));
		}

		return c;
	}

	/**
	 * Get the key.
	 *
	 * @return the key object
	 */
	public Object getKey() {
		try {
			return ((Fact) negativeLiterals.get(0)).getPredicate();
		} catch( Throwable t ) {
			LOG_KB.error("Error getting key of a clause !", t);
		}

		return null;
	}

	public boolean isBound() {
		return false;
	}

	/**
	 * Get the negative literals.
	 *
	 * @return a list containing the negative literals
	 */
	public List getNegativeLiterals() {
		return negativeLiterals;
	}

	/**
	 * Get the positive literals.
	 *
	 * @return a list containing the positive literals
	 */
	public List getPositiveLiterals() {
		return positiveLiterals;
	}

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

	/**
	 * Indicate whether this is the empty clause.
	 *
	 * @return true if there are no literals, false otherwise
	 */
	public boolean isEmpty() {
		return negativeLiterals.isEmpty() && positiveLiterals.isEmpty();
	}

	/**
	 * 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 false;
	}

	/**
	 * Convert the receiver to a string.
	 *
	 * @return the string representation of this object
	 */
	public String toString() {
		StringBuffer buf = new StringBuffer();
		boolean first = true;
		buf.append("+[");
		if( positiveLiterals != null ) {
			for( Iterator it = positiveLiterals.iterator(); it.hasNext(); ) {
				if( first ) {
					first = false;
				} else {
					buf.append(",");
				}
				buf.append(it.next().toString());
			}
		}
		buf.append("] , -[");
		first = true;
		if( negativeLiterals != null ) {
			for( Iterator it = negativeLiterals.iterator(); it.hasNext(); ) {
				if( first ) {
					first = false;
				} else {
					buf.append(",");
				}
				buf.append(it.next().toString());
			}
		}
		buf.append("]");
		return new String(buf);
	}

	/**
	 * Get an iterator iterating over the predicates contained in this clause set.
	 *
	 * @return an iterator
	 */
	public Iterator predicates() {
		List chain = new ArrayList();
		if( negativeLiterals != null ) {
			for( int i = 0; i < negativeLiterals.size(); i++ ) {
				chain.add(((ClauseSet) negativeLiterals.get(i)).predicates());
			}
		}
		if( positiveLiterals != null ) {
			for( int i = 0; i < positiveLiterals.size(); i++ ) {
				chain.add(((ClauseSet) positiveLiterals.get(i)).predicates());
			}
		}
		return new IteratorChain(chain);
	}

}
