On Mar 29, 2007, at 11:41 PM, [EMAIL PROTECTED] wrote:

In Jess 7 that's ever so easy! Once passed back to Jess, to get the
Strings from the list, how do I loop through because previously I used:

(defglobal ?*ArrayList* = (new java.util.ArrayList))
(for (bind ?i 0) (< ?i (?*ArrayList* size))(++ ?i)

for a normal Java ArrayList but this time its a Jess list?

The "foreach" function works for both ValueVectors and Java Collections:

(foreach ?s ?x
    (do something with element ?s))

where ?x could be your RU.LIST, as returned by your Userfunction, or it could be the ?*ArrayList* above.

I would have
prefered to have stuck with Java ArrayLists throughout but I guess that is
not possible when creating a userfunction as you can't return a Java
ArrayList? Thanks.


You sure can; you didn't ask how to do that! Just create the List from the Array (personally I'd use java.util.Arrays.asList() to make a List here) and then return it using the one-argument Value constructor. It comes through as an RU.JAVA_OBJECT and you can iterate over it using either of the two methods above.




On Mar 29, 2007, at 6:14 PM, [EMAIL PROTECTED] wrote:

Hi, I've been writing my own userfunctions in Java for Jess and have
managed fine returning strings, integers and doubles to Jess but now I
want to return an array but have come stuck and don't know the
syntax well
enough to know how to do it. Error = "The constructor Value(String
[], int)
is undefined". Once I pass back my Java array of type String to my
Jess
code I will be hoping to iterate through it. Thanks.

------------------------------------------------
public Value call(ValueVector vv, Context c) throws JessException {
        String s = vv.get(1).stringValue(c);
        int i = vv.get(2).intValue(c);
        String list[] = splitMessage(s, i);
        return new Value(list, RU.LIST);
}

In Jess 7, one easy way to turn any kind of array into a ValueVector
is to use the static "objectToValue()" method in the jess.RU class.

return RU.objectToValue(String[].class, list);

This will return a Value of type RU.LIST containing a ValueVector of
RU.STRING objects.

in Jess 6, you have to do it yourself;

ValueVector vv = new ValueVector();
for (String s: list)
     vv.arg(s);
return new Value(vv, RU.LIST);

---------------------------------------------------------
Ernest Friedman-Hill
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com

--------------------------------------------------------------------
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 owner-jess- [EMAIL PROTECTED]
--------------------------------------------------------------------




--------------------------------------------------------------------
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 owner-jess- [EMAIL PROTECTED]
--------------------------------------------------------------------

---------------------------------------------------------
Ernest Friedman-Hill
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com

--------------------------------------------------------------------
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