Thomas Gentsch mentioned that a Userfunction equivalent to Java's
"instanceof" operator would be useful. I wrote a quick one that makes
use of the existing facilities offered by the Jess "import" function,
and I thought I'd share it. If you've got the Jess source, just tack
this onto the end of jess/ReflectFunction.java (it needs to be defined
in the jess package since it uses the package-protected findClass
method) and in the add() method at the top, insert

   addFunction(new InstanceOf(), table);

at the end of the list of addFunction() calls. Then you can use it
like this:

Jess> (import jess.*)
TRUE
Jess> (instanceof (engine) Rete)
TRUE
Jess> (instanceof (engine) String)
FALSE
Jess> (instanceof "asdf" String)
TRUE
Jess>   

Enjoy! This will be in the next Jess release.

----------------------------------------------------------------------

class InstanceOf implements Userfunction, Serializable {
    public String getName() { return "instanceof"; }

    public Value call(ValueVector vv, Context context)
        throws JessException {

        Object o = vv.get(1).externalAddressValue(context);
        String className = vv.get(2).stringValue(context);
        try {
            Class clazz = context.getEngine().findClass(className);
            return clazz.isInstance(o) ? Funcall.TRUE : Funcall.FALSE;
        } catch (ClassNotFoundException cnfe) {
            throw new JessException("instanceof",
                                    "Class not found: " + className,
                                    cnfe);
        }

    }





---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
Org. 8920, MS 9012                  [EMAIL PROTECTED]
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550

---------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the
list (use your own address!) List problems? Notify [EMAIL PROTECTED]
---------------------------------------------------------------------

Reply via email to