JESS: [EXTERNAL] Iterate over a Java List in Jess

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

JESS: [EXTERNAL] Iterate over a Java List in Jess

Hunter Mcmillen
Is there anyway to iterate over a Java List object from Jess? 

I have a Java object that I store in a Jess variable:

engine.getGlobalContext().setVariable("bwapi", new Value(bwapi));

one of the methods that can be called on bwapi returns a Java ArrayList, and I would like to be able to iterate over this list to check certain properties of its elements. 

Right now I am doing this:

engine.executeCommand("(bind ?units (create$ =(?bwapi getMyUnits)))");
engine.executeCommand("foreach ?u ?units (printout t ?u crlf))");

but when the units are supposed to be printed all I get is:

=
<Java-Object:java.util.ArrayList>

Any ideas?

Thanks,
Hunter M
Reply | Threaded
Open this post in threaded view
|

RE: JESS: [EXTERNAL] Iterate over a Java List in Jess

Friedman-Hill, Ernest

That “create$” call is creating a list with one element, the ArrayList. Iterating over the Jess list returns just the ArrayList – not what you want.

 

In recent versions of Jess, the “foreach” function actually knows about Iterators and the like, so you can iterate over the ArrayList directly:

 

(foreach ?u (?bwapi getMyUnits) (printout t (?u toString) crlf))

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Hunter McMillen
Sent: Tuesday, December 13, 2011 8:56 PM
To: jess-users
Subject: JESS: [EXTERNAL] Iterate over a Java List in Jess

 

Is there anyway to iterate over a Java List object from Jess? 

 

I have a Java object that I store in a Jess variable:

 

engine.getGlobalContext().setVariable("bwapi", new Value(bwapi));

 

one of the methods that can be called on bwapi returns a Java ArrayList, and I would like to be able to iterate over this list to check certain properties of its elements. 

 

Right now I am doing this:

 

engine.executeCommand("(bind ?units (create$ =(?bwapi getMyUnits)))");

engine.executeCommand("foreach ?u ?units (printout t ?u crlf))");

 

but when the units are supposed to be printed all I get is:

 

=

<Java-Object:java.util.ArrayList>

 

Any ideas?

 

Thanks,

Hunter M