Do calls to defquery through function calls incorporate the defqueries in the rete network? I have tried a number of ways to collect numerical values from facts asserted by rules and to add them up so that a total value could be asserted to working memory. For example, if you run the code below, whatever the order of the rules in the file, "marker-2" prints before "marker-1" and ?x = 0. Is this because, although Defquery and Defrule are both subclass of HasLHS, the rete algorithm orders rule firing only on the basis of the LHS of the rules whereas run-query* can only appear on the RHS?

I have found a work-around which is to put on the LHS (not (not (clause))) which amounts to a there-exists statement and where the clause is the uninstantiated fact called by the defquery. If you several defquiries, you need (not (not (and clauses...))).

Alternatively, I might have fundamentally misunderstood something about the rete algorithm and its Jess implementation. Equally likely is that someone will have a much simpler and more elegant means of achieving what I want here.

The code:

(clear)
(watch all)
(deftemplate a (slot b)(slot c))
(deftemplate d (slot e)(slot f))
(deftemplate g (slot b)(slot e)(slot f))
(deftemplate h (slot j)(slot k)(slot n))

(assert (a (b x)(c y)))
(assert (a (b x)(c m)))
(assert (d (e w)(f 42)))
(assert (d (e w) (f 5)))
(assert (h (j x)(k w)(n nothing)))


(facts)


(defquery example-query
       (declare (variables ?q ?r))
       (g (b ?q)(e ?r)(f ?s)))

(deffunction example-function (?q ?r)
       (bind ?all-examples (run-query* example-query ?q ?r))
       (bind ?total 0.0)
       (while (?all-examples next)
               (printout t "some marker" crlf)
               (bind ?total (+ ?total (?all-examples getInt s))))
       (return ?total))

(defrule example-rule-2
        (h (j ?j)(k ?k)(n ?m))
        (g (f ?))
        =>
        (printout t "marker-2" crlf)
        (bind ?x (example-function ?j ?k))
        (printout t "?x = " ?x crlf))

(defrule example-rule-1
       (a (b ?b)(c ?c))
       (d (e ?e)(f ?f))
       =>
        (printout t "marker-1" crlf)
       (assert (g (b ?b)(e ?e)(f ?f))))

(run)


--
Scott Moss
Professor of Social Simulation
Centre for Policy Modelling
Manchester Metropolitan University
Aytoun Building
Manchester M1 3GH

http://cfpm.org/~scott

(t) +44 (0)161 247 3886
(f) +44 (0)161 247 6802
(m) +44 (0)7740 942564
--------------------------------------------------------------------
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