I have a Java object (Representation) that contains a HashMap of Java objects
(Property). I'd like to be able to select only the Representations that
have a Property with a given name and value.
The Java code for the Representation and Property classes, as well as the
accompanying Jess code that I have so far, is as follows:
------------Representation.java---------------------------
package jessexample;
import java.util.*;
public class Representation {
private String name;
private Map properties;
public Representation(String name) {
this.name = name;
this.properties = new HashMap();
}
public void addProperty(Object key, Object value) {
this.properties.put(key, value);
}
public Object getProperty(Object key) {
return this.properties.get(key);
}
}
-------------Property.java------------------------------------
package jessexample;
public class Property {
private String name;
private String description;
private Object value;
public Property(String name, String description, Object value) {
this.name = name;
this.description = description;
this.value = value;
}
// Getters and setters for name, description, and value fields
}
-----------Jess code-----------------
(deftemplate representation
(slot name
(type STRING))
(slot object))
(import jessexample.*)
; Create a new Representation object called repA, and add an
example-property whose value is 1
(bind ?repA (new Representation "repA"))
(bind ?example-property-A (new Property "example-property" "an example
property" 1))
(?repA addProperty "example-property" ?example-property-A)
(assert (representation (name "repA") (object ?repA)))
; Create a new Representation object called repB, and add an
example-property whose value is 0
(bind ?repB (new Representation "repB"))
(bind ?example-property-B (new Property "example-property" "an example
property" 0))
(?repB addProperty "example-property" ?example-property-B)
(assert (representation (name "repB") (object ?repB)))
; Now check for all representations that have example-property whose value
is 1
(defrule check-example-property
(representation (name ?name) (object ?object))
;;;;;;; not sure what should go here - the following doesn't seem to be
valid ;;;
(bind ?thisObject (?object getProperty "example-property"))
(bind ?thisPropValue (?thisObject getValue))
(test (eq ?thisPropValue 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
=>
(printout t "Representation " ?name " has example-property whose value =
1" crlf)
)
Thanks!
Ernest Friedman-Hill wrote:
>
> What I gave below certainly works within a rule -- can you describe
> to me the specific scenario you're trying to work out?
>
> On Sep 27, 2007, at 5:53 PM, marthawashington wrote:
>
>>
>> I have a similar question regarding accessing Java collections from
>> Jess.
>> How do you do this from inside a rule? I've read through the book and
>> searched the forum but unfortunately am still not clear on this.
>>
>> Thanks!
>>
>>
>> Ernest Friedman-Hill wrote:
>>>
>>> You deal with Java objects from Jess the same way you deal with them
>>> from Java. There is no special syntax for dealing with collections --
>>> you just call the Java methods. To call a method "y" on a Java object
>>> in variable ?x with argument "z", you write
>>>
>>> (?x y "z")
>>>
>>> Here you want to call "get" on the ArrayList to get an element, and
>>> "getValue" on the result; it looks like
>>>
>>> (bind ?theObject (?arraylist get 1))
>>> (?theObject getValue "value1")
>>>
>>> of course you can eliminate the extra variable ?theObject and write
>>>
>>> ((?arraylist get 1) getValue "value1")
>>>
>>> Now, I'm a tiny bit concerned that you didn't actually mean
>>> "ArrayList" but rather "array" in your original post; which is it?
>>>
>>>
>>> On Sep 26, 2007, at 3:15 PM, Andrew Meng wrote:
>>>
>>>> Hello,
>>>>
>>>> I have a java arraylist collection declared in Jess. In a rule's
>>>> defination, I want to get some value from a object contained in
>>>> arraylist. like,
>>>> (> arraylist[1].getValue("value1") arraylist[0].getValue
>>>> ("value2") )
>>>>
>>>> Can anyone tell me what is the right way to do it in Jess?
>>>>
>>>> Thanks a lot in advance!
>>>> Andrew
>>>>
>>>> Discover the new Windows Vista Learn more!
>>>
>>> ---------------------------------------------------------
>>> 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]
>>> --------------------------------------------------------------------
>>>
>>>
>>>
>>
>> --
>> View this message in context: http://www.nabble.com/JESS%3A-How-to-
>> access-info-in-a-objects-cotained-in-a-arraylist.-
>> tf4524484.html#a12931035
>> Sent from the Jess mailing list archive at Nabble.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]
>> --------------------------------------------------------------------
>
> ---------------------------------------------------------
> 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]
> --------------------------------------------------------------------
>
>
>
--
View this message in context:
http://www.nabble.com/JESS%3A-How-to-access-info-in-a-objects-cotained-in-a-arraylist.-tf4524484.html#a12943509
Sent from the Jess mailing list archive at Nabble.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]
--------------------------------------------------------------------