Another backward chaining example from Jess In Action

This is the multi-level backward chaining example from Jess In Action. My implementation of backward chaining had some problems with the original example so here is a new version of the shell that does the job.

CLIPS[001/01]> (deffunction fetch-price-from-database (?number)
CLIPS[001/02]: (if (eq ?number 1)
CLIPS[001/03]: then (return $1.99))
CLIPS[001/04]: (return nil))
CLIPS[002/01]> (deffunction fetch-number-from-database (?name)
CLIPS[002/02]: (if (eq ?name waffles)
CLIPS[002/03]: then (return 1))
CLIPS[002/04]: (return 0))
CLIPS[003/01]> (defrule price-check
CLIPS[003/02]: (do-price-check ?name)
CLIPS[003/03]: (price ?name ?price)
CLIPS[003/04]: =>
CLIPS[003/05]: (printout t "Price of " ?name " is " ?price crlf))
CLIPS[004/01]> (defrule find-price
CLIPS[004/02]: (need-price ?name ?)
CLIPS[004/03]: (item-number ?name ?number)
CLIPS[004/04]: =>
CLIPS[004/05]: (bind ?price (fetch-price-from-database ?number))
CLIPS[004/06]: (assert (price ?name ?price)))
CLIPS[005/01]> (defrule find-item-number
CLIPS[005/02]: (need-item-number ?name ?)
CLIPS[005/03]: =>
CLIPS[005/04]: (bind ?number (fetch-number-from-database ?name))
CLIPS[005/05]: (assert (item-number ?name ?number)))
CLIPS[006/01]> (do-backward-chaining item-number)
; + MAIN::find-price/0
CLIPS[007/01]> (do-backward-chaining price)
; + MAIN::price-check/0
CLIPS[008/01]> (ppdefrule price-check/0)
(defrule MAIN::price-check/0
(do-price-check ?name)
(assert (need-price ?name nil)))
CLIPS[009/01]> (ppdefrule find-price/0)
(defrule MAIN::find-price/0
(need-price ?name ?)
(assert (need-item-number ?name nil)))
CLIPS[010/01]> (reset)
CLIPS[011/01]> (assert (do-price-check waffles))
CLIPS[012/01]> (watch rules)
CLIPS[013/01]> (run)
FIRE 1 price-check/0: f-1
FIRE 2 find-price/0: f-2
FIRE 3 find-item-number: f-3
FIRE 4 find-price: f-2,f-4
FIRE 5 price-check: f-1,f-5
Price of waffles is $1.99
CLIPS[014/01]> (facts)
f-0 (initial-fact)
f-1 (do-price-check waffles)
f-2 (need-price waffles nil)
f-3 (need-item-number waffles nil)
f-4 (item-number waffles 1)
f-5 (price waffles $1.99)
For a total of 6 facts.

Inga kommentarer: