Hi,

This made me physically shudder a bit when I read it. I love having
this mailing list because it's a bounteous source of both wonderful
and frightening ideas. This one is frightening.

Actually, it sounds quite reasonable, until you think about how
run-query is implemented. It asserts a trigger fact, collects the
results, and retracts the trigger fact. In some versions of Jess, as
it will in 6.0 final, it even calls run() so that backwards chaining
can take place during a query.

Now, pattern-matching is actually done during assert() or retract()
calls -- that's how the Rete algorithm works. So what haoppens here is
that you call (reset), which asserts (initial-fact), which matches the
LHS of your rule, which triggers the (test) CE, which calls
(run-query), which wants, then, to assert a fact... So we're going to
do an assertion while another assertion is going on; the Rete network
is not guaranteed to be reentrant (that's why assert is synchronized
internally.)

Actually, though, looking at the relevant Jess code, this shouldn't
actually cause any real problems, and I might have expected it to
work. It took me a few minutes to see why it doesn't. Read manual
section 2.8.7, which states fairly clearly that your rule will have an
(initial-fact) pattern inserted as the first pattern, and the (test)
will be evaluated immediately after that pattern is matched. Then
think about what happens during a (reset): first (initial-fact) is
asserted, then the facts in (deffacts) constructs are asserted.
So what happens is that the (test) CE is evaluated before any of the
(a) facts exist! If you modify your defrule and deffacts to look
like the below, your program will actually work! (*shudder*)

(defrule r1
        (ok-to-run-query)
        (test (> (sum (run-query q) 3) 10))
        =>
        (printout t "r1" crlf)
)

(deffacts ff
        (a 0 0 1)
        (a 0 1 2)
        (a 0 2 3)
        (a 1 0 4)
        (a 1 1 5)
        (a 1 2 6)
        (a 2 0 7)
        (a 2 1 8)
        (a 2 2 9)
        (ok-to-run-query)
)



I think Al-Akhras, Khaled wrote:
[Charset iso-8859-1 unsupported, filtering to ASCII...]
> I am trying to use a defquery on LHS to match if the sum is greater than a
> threshold.  But when I run the attached file, the rule doesn't fire.
> 
> Thanks,
> Khaled
> 
> test.clp
> ---------
> (clear)
> 
> (defquery q
>       (a ?i ?j ?v)
> )
> 
> (deffunction sum (?__it ?__n)
>       (bind ?s 0)
>       (while (?__it hasNext)
>               (bind ?token (call ?__it next))
>               (bind ?fact (call ?token fact 2))
>               (bind ?slot (fact-slot-value ?fact __data))
>               (bind ?datum (nth$ ?__n ?slot))
>               (bind ?s (+ ?s ?datum))
>       )
>       return ?s
> )
> 
> (defrule r1
>       (test (> (sum (run-query q) 3) 10))
>       =>
>       (printout t "r1" crlf)
> )
> 
> (deffacts ff 
>       (a 0 0 1)
>       (a 0 1 2)
>       (a 0 2 3)
>       (a 1 0 4)
>       (a 1 1 5)
>       (a 1 2 6)
>       (a 2 0 7)
>       (a 2 1 8)
>       (a 2 2 9)
> )
> 
> (reset)
> (printout t "sum = " (sum (run-query q) 3) crlf)
> (run)
> 
> 
> Jess> (batch test.clp)
> sum = 45
> 0
> Jess>
> 
> ---------------------------------------------------------------------
> 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
Org. 8920, MS 9012                  [EMAIL PROTECTED]
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550

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