/*
 * 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.meta;

import org.mandarax.kernel.*;

/**
 * "Instanceof" predicates. E.g., if the type is <code>ArrayList</code> and the objectType is <code>List,
 * the predicate has one argument of the type <code>List</code>, and holds if the list instance is
 * an instance of <code>ArrayList</code>.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A> <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
 * @version 3.3.1 <9 August 2004>
 * @since 3.3
 */
public class InstanceOfPredicate implements Predicate {

	private Class type = null;
	private Class objectType = Object.class;
	private Class[] structure = new Class[]{objectType};
	private static final String[] slotNames = {"object"};
	private String name = null; 

	/**
	 * Constructor.
	 * @param type the type of the class
	 * @param objectType the expected type of the object
	 * @param name the name of this predicate
	 */
	public InstanceOfPredicate(Class type,Class objectType) {
		super();
		this.type = type;
		this.objectType = objectType;
		this.structure[0] = objectType;
		this.name = "is instance of " + type.getClass();
	}
    /**
     * Get the structure of the function.
     * @return the structure of the function
     */
    public Class[] getStructure() {
        return structure;
    }

    /**
     * Perform the function using an array of terms as parameters.
     * @return the result of the computation
     * @param parameter an array of terms
     * @param session a session object
     * @throws java.lang.UnsupportedOperationException
     * @throws java.lang.IllegalArgumentException
     */
    public Object perform(Term[] parameter,Session session) throws IllegalArgumentException, UnsupportedOperationException {
    	boolean result = false;
    	try {    		
    		Object obj = parameter[0].resolve (session);
    		if (obj!= null) type.isAssignableFrom(obj.getClass());
        } catch(Exception x) {
            throw new IllegalArgumentException ();
        }
        return Boolean.valueOf(false);
    }

	/**
	 * Get the slot names.
	 * @return the slot names
	 */
	public String[] getSlotNames() {
		return slotNames;
	}
	/**
	 * Set the slot names.
	 * Not supported.
	 * @param slotNames the new slot names
	 */
	public void setSlotNames(String[] arg0) {
		throw new UnsupportedOperationException();
	}
	/**
	 * Indicates whether the slot names can be edited.
	 * @see org.mandarax.kernel.Predicate#slotNamesCanBeEdited()
	 */
	public boolean slotNamesCanBeEdited() {
		return false;
	}
	/**
	 * Returns the name.
	 * @return the name of the object
	 */
	public String getName() {
		return name;
	}
	/**
	 * Indicates whether this predicate is executable.
	 * @return a boolean
	 */
	public boolean isExecutable() {
		return true;
	}
}
