001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.kernel.meta;
019:
020: import org.mandarax.kernel.*;
021:
022: /**
023: * "Instanceof" predicates. E.g., if the type is <code>ArrayList</code> and the objectType is <code>List,
024: * the predicate has one argument of the type <code>List</code>, and holds if the list instance is
025: * an instance of <code>ArrayList</code>.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A> <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
027: * @version 3.4 <7 March 05>
028: * @since 3.3
029: */
030: public class InstanceOfPredicate implements Predicate {
031:
032: private Class type = null;
033: private Class objectType = Object.class;
034: private Class[] structure = new Class[] { objectType };
035: private static final String[] slotNames = { "object" };
036: private String name = null;
037:
038: /**
039: * Constructor.
040: * @param type the type of the class
041: * @param objectType the expected type of the object
042: * @param name the name of this predicate
043: */
044: public InstanceOfPredicate(Class type, Class objectType) {
045: super ();
046: this .type = type;
047: this .objectType = objectType;
048: this .structure[0] = objectType;
049: this .name = "is instance of " + type.getClass();
050: }
051:
052: /**
053: * Get the structure of the function.
054: * @return the structure of the function
055: */
056: public Class[] getStructure() {
057: return structure;
058: }
059:
060: /**
061: * Perform the function using an array of terms as parameters.
062: * @return the result of the computation
063: * @param parameter an array of terms
064: * @param session a session object
065: * @throws java.lang.UnsupportedOperationException
066: * @throws java.lang.IllegalArgumentException
067: */
068: public Object perform(Term[] parameter, Session session)
069: throws IllegalArgumentException,
070: UnsupportedOperationException {
071: boolean result = false;
072: try {
073: Object obj = parameter[0].resolve(session);
074: if (obj != null)
075: type.isAssignableFrom(obj.getClass());
076: } catch (Exception x) {
077: throw new IllegalArgumentException();
078: }
079: return Boolean.valueOf(false);
080: }
081:
082: /**
083: * Get the slot names.
084: * @return the slot names
085: */
086: public String[] getSlotNames() {
087: return slotNames;
088: }
089:
090: /**
091: * Set the slot names.
092: * Not supported.
093: * @param slotNames the new slot names
094: */
095: public void setSlotNames(String[] arg0) {
096: throw new UnsupportedOperationException();
097: }
098:
099: /**
100: * Indicates whether the slot names can be edited.
101: * @see org.mandarax.kernel.Predicate#slotNamesCanBeEdited()
102: */
103: public boolean slotNamesCanBeEdited() {
104: return false;
105: }
106:
107: /**
108: * Returns the name.
109: * @return the name of the object
110: */
111: public String getName() {
112: return name;
113: }
114:
115: /**
116: * Indicates whether this predicate is executable.
117: * @return a boolean
118: */
119: public boolean isExecutable() {
120: return true;
121: }
122: }
|