0
votes

I'm trying to learn CLIPS and this is a very basic question. I have facts for two people (person (name "jim")(age 22)) and (person (name "sally")(age 32)) and I simply want a rule that will print out who is older, or assert some fact like ( older (name "xxx")). I was trying

(defrule compare "is jim older"
( person ( name "jim")(age ?jims_age))
( person ( name "sally")( age ?sallys_age))
(> ?jims_age ?sallys_age)
=>
(printout t "jim is older" crlf))
 

Which never activates. So what is the right way to compare jim and sally's ages? I can't find an easy example that does this.

Thank you!

1

1 Answers

0
votes

You need to use the test conditional element.

(defrule compare "is jim older"
  (person (name "jim") (age ?jims_age))
  (person (name "sally") (age ?sallys_age))
  (test (> ?jims_age ?sallys_age))
  =>
  (printout t "jim is older" crlf))

Your rule made generic:

(defrule compare "Compare two persons' age"
  (person (name ?name1) (age ?age1))
  (person (name ?name2) (age ?age2))
  (test (and (neq ?name1 ?name2) 
             (> ?age1 ?age2)))
  =>
  (printout t ?name1 " is older than " ?name2 crlf))

Explained in the 5.4.2 Test Conditional Element chapter of the Basic Programming Guide.