Hi!
 
I assume the Country class provides a method
 
    public State[] getStates();
 
The statement (defclass country Country) defines a template containing, among others, a multislot called "states" that can be bound on the LHS. Refer to the Jess Function List to see what you can do with multislots. Specifically, 
 
    (foreach <variable> <multifield-expression> <action>*)
 
can be used to iterate a multifield and process its members.
 
I further assume the State class provides methods
 
    public String getName();
    public void setName(String newName);
 
Therefore, you can use (get ?state name) and (set ?state name "Northern Virginia") in order to get and set a state's name.
 
Now, here is your rule:
 
;;
;; Jess Script
;;
 
(defclass country Country)
 
(defrule search-country
    (country (name "USA") (states $?states))
    =>
    (foreach ?state $?states
        (bind ?state-name (get ?state name))
        (if (eq ?state-name "Virginia")
            (set ?state name "Northern Virginia"))))
 
The rule will fire for every country instance whose name is equal to "USA".
 
I hope this is what you were looking for.
 
Thomas
 
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Adarsh Jain
Sent: Wednesday, October 11, 2000 8:01 PM
To: Thomas Barnekow
Cc: [EMAIL PROTECTED]
Subject: Re: JESS: Using multiple objects in LHS

Hi,
I will wirte the rule and describe what I am looking for.

;;If the name attr in Country instance is "USA", then call a method on Country object
;;that would return an array of State objects. Change the name attribute of the State object
;;to "Northern Virginia" if the name attr is "Virginia".
 (Defclass country Country)

(defrule search-country
        (country (name ?name) (Object ?country))
        (test (eq "USA" ?name)
=>
        (call ?country getStates)

Now, this would get me the list of all states in USA. 2 questions here..
1. How do I proceed further.
2. I have a number of countries for which the rule has to look for USA. So, do I
have to Funcalls for each country and if so do I have to change the LHS for search-country.

Thanks,
Adarsh
 

Thomas Barnekow wrote:

Hi!

> I would like to have a rule work on multiple instances of an object
> i.e. multiple instances on the LHS. Now do I have to do
>
>         Pop[] pop = new Pop[100];
>         Funcall f = new Funcall("definstance", rete);
>         f.add(new Value("pop", RU.ATOM));
>         f.add(new Value(pop[0]));
>         f.execute(rete.getGlobalContext());
>
> for each instance. Can I use a pattern to assert all instances of Pop
> object. If I can do it, how do I represent it in the rule.

I'm not sure whether I understand what you mean.
What does the rule look like? Version 1 or 2?

;; Version 1
(defrule rule-1
        (pop (OBJECT ?o-1) ...)
        =>
        ;; RHS
)

;; Version 2
(defrule rule-2
        (pop (OBJECT ?o-1) ...)
        ...
        (pop (OBJECT ?o-n) ...)
        =>
        ;; RHS
)

No matter what kind of rule you have, you need to create instances one by one. You can reuse the same Funcall object for every instance, though:

  // Create objects
  Pop[] pop = new Pop[100];
  ...

  // Prepare Funcall
  Funcall f = new Funcall("definstance", rete);
  f.setLength(3);
  f.set(new Value("pop", RU.ATOM), 1);

  // Modify Funcall and create instances
  for (int i = 0 ; i < 100 ; i++) {
    f.set(new Value(pop[i]), 2);
    f.execute(rete.getGlobalContext());
  }

Greetings, Thomas

-- 
Adarsh Jain
Longitude Systems, Inc
Ph: 703-818-5436
 

Reply via email to