package org.mandarax.kernel;
/*
 * 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 java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.mandarax.util.PredicateUtils;

/**
 * Simple implementation of predicate.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.1
 */
public class SimplePredicate extends SimpleConstructor implements Predicate {
	private String[] slotNames = null;
    /**
     * Constructor.
     */
    public SimplePredicate() {
        super ();
    }
    /**
     * Constructor.
     * @param aName the name of the predicate
     * @param types the structure of the predicate (the types of the terms)
     */
    public SimplePredicate(String aName, Class[] types) {
        super (aName, types);
    }

    /**
     * Indicates whether the object (usually a term or a clause set) can be performed
     * using the java semantics.
     * @return false
     */
    public boolean isExecutable() {
        return false;
    }
    /**
     * Read the object from an object input stream.
     * @param in an input stream
     */
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject ();
        setName((String)in.readObject());
		setStructure((Class[]) in.readObject());
    }

    /**
     * Write the object to an object output stream.
     * @param out an output stream
     */
    private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException {
        out.defaultWriteObject ();
        out.writeObject (getName());
        out.writeObject (getStructure());
    }
    /**
     * Set the structure of the predicate or function.
     * Implemented here in order to support JDK 1.4 XML serialization.
     * @param struct the structure of the predicate or function
     */
    public void setStructure(Class[] struct) {
		super.setStructure(struct);
		// check for consistency with slot names
		if (struct!=null && slotNames!=null) {
			if (slotNames.length>struct.length) {
				// remove additional slot names
				String[] newSlotNames = new String[struct.length];
				System.arraycopy(slotNames,0,newSlotNames,0,struct.length);
				setSlotNames(newSlotNames);
			}
			if (slotNames.length<struct.length) {
				// remove additional slot names
				String[] newSlotNames = new String[struct.length];
				System.arraycopy(slotNames,0,newSlotNames,0,slotNames.length);
				for (int i=slotNames.length;i<struct.length;i++) {
					newSlotNames[i] = PredicateUtils.getSlotName(this,i);
				}
				setSlotNames(newSlotNames);
			}
		}
		else slotNames = null;
    }
    /**
     * Set the name of the predicate or function.
     * Implemented here in order to support JDK 1.4 XML serialization.
     * @param the name of the predicate or function
     */
    public void setName(String aName) {
        super.setName(aName);
    }
	/**
	 * Get the slot names.
	 * @return an array of strings, the length of the array is the same as
	 * the length of the array of terms (the structure of the predicate)
	 */
	public String[] getSlotNames() {
		if (slotNames==null) {
			Class[] struct = getStructure();
			int s = struct==null?0:struct.length;
			slotNames = new String[s];
			for (int i=0;i<slotNames.length;i++) slotNames[i] = PredicateUtils.getSlotName(this,i); 
		}
		return slotNames;
	}
	/**
	 * Set the slot names.
	 * @param names an array of strings, the length of the array is the same as
	 * the length of the array of terms (the structure of the predicate)
	 */
	public void setSlotNames(String[] names) {
		Class[] struct = getStructure();		
		if (names!=null && (struct!=null && names.length!=getStructure().length)) throw new IllegalArgumentException("Number of slot names and number of slots must match - cannot set slot names for predicate " + this);
		this.slotNames = names;
	}
	/**
	 * Indicates whether the slot names can be modified.
	 * @return a boolean
	 */
	public boolean slotNamesCanBeEdited() {
		return true;
	}
}
