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.mandarax.kernel.Constructor;
import org.mandarax.kernel.LObject;
import org.mandarax.kernel.Term;
import org.mandarax.util.TermIterator;

/**
 * Abstract super class for logical entities such as complex terms and facts.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.1
 */
public abstract class Complex extends LObject {

	protected Term[]  terms = null;
	protected Class[] types = null;
	
	/** Cache the string representation for optimized debugging. */
	protected transient String cachedToString = null;

	/**
	 * Constructor.
	 */
	protected Complex() {
		super ();
	}

	/**
	 * Get the object constructing the complex, i.e. the predicate or function.
	 * @return the constructor
	 */
	protected abstract Constructor getConstructor();
	/**
	 * Get the terms (as an array).
	 * @return an array of terms
	 */
	public Term[] getTerms() {
		return terms;
	}
	/**
	 * Set the terms (as an array).
	 * @param t an array of terms
	 */
	public void setTerms(Term[] t) {
		cachedToString = null;
		
		// new in 3.2 , more checks, enforce all checks done in setTerm
		if (this.getConstructor()==null) throw new IllegalStateException("Constructor (predicate or function) must be set before terms can be set in " + this);
		if (this.terms==null) this.terms = new Term[this.getConstructor().getStructure().length];
		for (int i=0;i<t.length;i++) {
			setTerm(t[i],i);
		}
	}
	/**
	 * Set a term at a certain position. Note that in case the type of the term does not
	 * match the type required by the constructor we do not throw an exception.
	 * Instead, we just do not set the term.
	 * <br>As from 3.2, some new checks have been added an runtime exceptions are thrown if 
	 * one of these tests fails
	 * @param t a term
	 * @param i a position
	 */
	public void setTerm(Term t, int i) {
		cachedToString = null;

		// check whether the predicate is there and whether the slot is available
		if(getConstructor () == null) {
			throw new IllegalStateException("Constructor (predicate or function) must be set before terms can be set in \"" + this + "\"");
		}
		if(types == null) {
			throw new IllegalStateException("Types must be set before terms can be set in " + this);
		}
		if(terms.length<=i) {
			String msg = new StringBuffer()
				.append("Not enough terms slots in \"")
				.append(this)
				.append("\" to set term  \"")
				.append(t)
				.append("\" at position ")
				.append(i)
				.toString();
			throw new IllegalArgumentException(msg);	
		}
		if(i<0) {
			String msg = new StringBuffer()
				.append("Cannot set term at position 0 in \"")
				.append(this)
				.append("\"")
				.toString();
			throw new IllegalArgumentException(msg);	
		}

		// check whether the type matches the method type and if so, set the term
		if (t==null || types[i].isAssignableFrom (t.getType ())) {
			terms[i] = t;
		}
		else {
			String msg = new StringBuffer()
				.append("Cannot set term \"")
				.append(t)
				.append("\" in \"")
				.append(this)
				.append("\" at position ")
				.append(i)
				.append(" - the term type is \"")
				.append(t.getType())
				.append("\" but expected is \"")
				.append(types[i])
				.append("\"")
				.toString();
			throw new IllegalArgumentException(msg);
		}
	}
	/**
	 * Iterate the terms.
	 * @return a term iterator
	 */
	public TermIterator terms() {
		return new TermIterator (terms);
	}

	/**
	 * Convert the receiver to a string.
	 * @return the string representation of this object
	 */
	public String toString() {
		if (cachedToString == null) {
			StringBuffer buf = new StringBuffer ();

			buf.append (getConstructor ().getName ());
			buf.append ("(");

			boolean first = true;

			for(TermIterator e = terms (); e.hasMoreTerms (); ) {
				if(first) {
					first = false;
				} else {
					buf.append (",");
				}

				Term nextTerm = e.nextTerm ();

				buf.append ((nextTerm == null)
							? "null"
							: nextTerm.toString ());
			}

			buf.append (")");

			cachedToString = new String (buf);
		}
		return cachedToString;
	}
}
