The left hand side (LHS) of rules consists of conditional elements, which are 
patterns to match before the rule fires.  The only binding of variables on the 
LHS comes from matches of parts of the conditional elements.  An S-expression 
that begins with "(bind ..." on the LHS tries to match a bind template 
instance; it does not bind a variable.

With the exception of the test construct, matches can only be made with facts, 
including shadow facts 
<http://herzberg.ca.sandia.gov/jess/docs/70/memory.html#shadow_facts>.  The 
representation deftemplate could be replaced with a template for a shadow fact. 
 Using your Java example below:

(deftemplate Representation
    (declare (from-class Representation) (include-variables TRUE)))

(deftemplate Property
    (declare (from-class Property) (include-variables TRUE)))

Instances of these shadow fact deftemplates (or defclasses) would need to be 
made known to the Jess engine with "definstance" or "add" in Jess 7.0.

However, the HashMap properties of Representation are not available at the top 
level of the data structure via getters and setters that satisfy the Javabean 
specification.  Only the top level "properties" might be matched within a 
conditional element in the LHS.

A HashMap or other Java collection inside of a fact does not lend itself to 
explicit matching.  Jess has no equivalent to the DESTRUCTURING-BIND macro of 
ANSI Common Lisp 
<http://www.lisp.org/HyperSpec/Body/mac_destructuring-bind.html>.  HashMap 
entries must be accessed through functions, such as functions on the right hand 
side (RHS) of rules, including Iterators, or as value-returning functions 
within conditional elements that qualify matches with the "=" pattern syntax.

Assuming Representation has a getName() method, then a slightly different 
version of your rule might be written:
(defrule check-example-property
    ?Representation <- (Representation (name ?representation-name))
    (Property (name ?property-name) (description ?description)
              (value ?value&=(?Representation getProperty ?property-name)))
    =>
    (printout t "Representation " ?representation-name
              " has matching fact Property called " ?property-name
              " with description " ?description
              " whose value is " ?value crlf))

Bob Kirby

At 09:01 AM 9/28/2007, marthawashington wrote:

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

Reply via email to