Dear Jess users,

I am trying to use Jess in a diagnostic system for which a natural approach
would be backwards chaining. I've experimented with the backward chaining as
advised elsewhere but yet its behaviour remains somewhat mysterious to me.
Facts in my system should be stored in templates. Suppose we have the
following template:

(deftemplate MAIN::zz extends MAIN::__fact "zz"
 (slot a)
 (slot b)
 (slot zz)
)

The knowledge base consists of the following rules which based on values of
some slots in the template update value of another slot in this template. In
the example we are interested to determine the value of slot 'zz' as soon as
possible. The engine should ask the user for a value of 'a' or 'b' slots
whenever it needs them, of course.

;; rules
(do-backward-chaining zz)
(defrule r001
    ?factid <- (zz (a yes) (zz nil))
 =>
  (modify ?factid (zz yes))
)

(defrule r002
    ?factid <- (zz (a no) (b no) (zz nil))
 =>
  (modify ?factid (zz yes))
)

(defrule r003
    ?factid <- (zz (a no) (b yes) (zz nil))
 =>
  (modify ?factid (zz no))
)

I have experimented with backwards chaining using the following rules to
help Jess find out about the values of 'a' and 'b'. The salience factor is
used because the sequence in which questions should be asked is very
important.

(defrule need_a
  (declare (salience 2))
  (exists (need-zz (a ?v&:(neq ?v nil))))
  ?fzz <- (explicit (zz))
=>
  (printout t "What is a?: yes/no" crlf)
  (bind ?zza (read))
  (modify ?fzz (a ?zza))
  (bind ?itr (run-query search_to_clean_a ?zza))
  (while (?itr hasNext)
      (bind ?token (call ?itr next))
      (retract (call ?token fact 1))
  )
)

(defrule need_b
  (declare (salience 1))
  (exists (need-zz (b ?v&:(neq ?v nil))))
  ?fzz <- (explicit (zz))
=>
  (printout t "What is b?: yes/no" crlf)
  (bind ?zzb (read))
  (modify ?fzz (b ?zzb))
  (bind ?itr (run-query search_to_clean_b ?zzb))
  (while (?itr hasNext)
      (bind ?token (call ?itr next))
      (retract (call ?token fact 1))
  )
)

(defquery search_to_clean_a
  (declare (variables ?val))
  (need-zz (a ?x))
  (test (neq ?x ?val))
)

(defquery search_to_clean_b
  (declare (variables ?val))
  (need-zz (b ?x))
  (test (neq ?x ?val))
)

(deffacts zzfacts
  (zz )
)

This approach won't work unfortunately. Jess will ask about the value of 'a'
endlessly activating and deactivating rule 'need_a' all the time, even when
the engine is executed with (run 1). The query 'search_to_clean_a' is never
actually executed, only the fact (MAIN::__query-trigger-search_to_clean_a
yes) is asserted but it never activates the query.

Do I make a mistake somewhere, or what is wrong here actually? Could someone
please suggest a solution? Maybe I use backward chaining needlessly? Thank
you in advance!
Waldek

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