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