Hi Marcin,
First of all, be careful how you do your pattern matching. The symbol
frog
and the string
"frog"
are not the same things. This was causing a problem in your (pet-kind)
rule. BTW -- It also helps to be consistent in how you name your
variables. I prefer being verbose rather than abbreviated when it comes to
variable names since it makes my code self-documenting. Try this solution.
Cheers,
Jason
====================================
; pet.clp
(clear)
(watch all)
(do-backward-chaining color)
(do-backward-chaining kind)
(defrule print-pet-color
(color ?pet-name ?color-name)
=>
(printout t "Color of " ?pet-name " is " ?color-name crlf))
(defrule frog-color
(need-color ? ?)
(kind ?pet-name frog)
=>
(printout t "firing frog-color" crlf)
(assert (color ?pet-name green)))
(defrule pet-kind
(need-kind ? ?)
=>
(printout t "Pet name? ")
(bind ?pet-name (read))
(printout t "Pet type? ")
(bind ?type (read))
(assert (kind ?pet-name ?type)))
; Program
(reset)
(run)
/** OUTPUT
MAIN::print-pet-color: +1+1+1+1+2+t
MAIN::frog-color: +1+1+1=1=1+2+1+1+1+2+t
MAIN::pet-kind: +1+1+1=1=1+2+t
==> Focus MAIN
==> f-0 (MAIN::initial-fact)
==> f-1 (MAIN::need-color nil nil)
==> f-2 (MAIN::need-kind nil frog)
==> Activation: MAIN::pet-kind : f-2,
FIRE 1 MAIN::pet-kind f-2,
Pet name? Bob
Pet type? frog
==> f-3 (MAIN::kind Bob frog)
==> Activation: MAIN::frog-color : f-1,, f-3
FIRE 2 MAIN::frog-color f-1,, f-3
firing frog-color
==> f-4 (MAIN::color Bob green)
==> Activation: MAIN::print-pet-color : f-0, f-4
FIRE 3 MAIN::print-pet-color f-0, f-4
Color of Bob is green
<== Focus MAIN
3
**/
On Mon, Aug 25, 2008 at 1:26 PM, Marcin Krol <[EMAIL PROTECTED]> wrote:
> Hello everyone,
>
> I'm new to Jess and struggling with making one example work, based on
> problem from http://en.wikipedia.org/wiki/Backward_chaining
>
> The goal is to find the pet's color if given that pet is a frog or a
> canary.
>
> I have defined such rules:
>
> ===============
> (do-backward-chaining color)
> (do-backward-chaining kind)
>
> (defrule print-Fritz-color
> (color ?name ?val) =>
> (printout t "Color of " ?name " is " ?val crlf)
> )
>
> (defrule frog-color
> (need-color ?name ?)
> (kind ?name frog)
> =>
> (printout t "firing frog-color" crlf)
> (assert (color ?name "green"))
> )
>
>
> (defrule pet-kind
> (need-kind ?name ?type) =>
> (printout t "Pet name? ")
> (bind ?name (read))
> (printout t "Pet kind? ")
> (bind ?type (read))
> (assert (kind ?name ?type))
> )
>
>
> TRUE
> Jess> TRUE
> Jess> TRUE
> Jess> TRUE
> Jess> TRUE
> Jess> (reset)
> TRUE
> Jess> (agenda)
> [Activation: MAIN::pet-kind f-2, ; time=3 ; salience=0]
> For a total of 1 activations.
> Jess> (run)
> Pet name? Fritz
> Pet kind? frog
> 1
> Jess> (facts)
> f-0 (MAIN::initial-fact)
> f-1 (MAIN::need-color nil nil)
> f-2 (MAIN::need-kind nil frog)
> f-3 (MAIN::kind Fritz frog)
> For a total of 4 facts.
> Jess>
>
>
> ===================
>
> Note that pet-kind rule fires, because it asks me about the pet name and
> type.
>
> What I don't get is why frog-color rule doesn't fire -- after all, it
> clearly has following facts asserted:
>
> f-1 (MAIN::need-color nil nil)
> f-3 (MAIN::kind Fritz frog)
>
> ..and the rule conditions defined:
>
> (defrule frog-color
> (need-color ?name ?)
> (kind ?name frog)
>
>
>
> Why doesn't the damn thing fire??
>
> Regards,
> Marcin
>
>
>
>
> --------------------------------------------------------------------
> 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]
> --------------------------------------------------------------------
>
>
--
-----------------------------------------------------------
Jason Morris
Morris Technical Solutions LLC
[EMAIL PROTECTED]
(517) 304-5883