Surgeon General's Warning -- using queries on the LHS of a rule can be
hazardous to your health! Sometimes you can make it work, but often it
just hurts your brain!

What's happening here is that the (test) CE is evaluated every time
the pattern immediately before it is matched. That means that when the
first, second, third, and fourth (banana jay ?) facts are asserted,
the (test) fails, but when the fifth is asserted, it passes; then the
next two times, it fails again. There's nothing to remove that
activation from the agenda when the number becomes more than 5, so it
eventually fires. 

As in most cases involving queries on the LHS, rewriting it as

(defrule five-bananas
   (banana ?x ?y)
 =>
   (if (= (count-query-results banana-count ?x) 5) then
     (printout t "Got one: " ?x crlf))
 )

Fixes the problem and is easier to understand.


I think Joseph Kopena wrote:
> Hi,
> 
> I've been looking around and trying a little to decipher the source, 
> but I can't figure this problem out.  What's happening here such that 
> joe and jay both fire the five-bananas rule?
> 
> Thanks
> 
> (defquery banana-count
>   (declare (variables ?x))
>   (banana ?x ?y)
> )
> 
> (defrule five-bananas
>   (banana ?x ?y)
>   (test (= (count-query-results banana-count ?x) 5))
> =>
>   (printout t "Got one: " ?x crlf)
> )
> 
> (assert
>   (banana joe b1)
>   (banana joe b2)
>   (banana joe b3)
>   (banana joe b4)
>   (banana joe b5)
> 
>   (banana jim c1)
>   (banana jim c2)
> 
>   (banana jay d1)
>   (banana jay d2)
>   (banana jay d3)
>   (banana jay d4)
>   (banana jay d5)
>   (banana jay d6)
>   (banana jay d7)
> )
> 
> (run)
> 
> -- 
> - joe kopena
> 
> --------------------------------------------------------------------
> 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]
> --------------------------------------------------------------------
> 



---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550         http://herzberg.ca.sandia.gov

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