Search code examples
stringsyntaxcompareconditional-statementsclips

clips use string to make a compare condition


I'm very very new to Clips expert system I'm looking for what is a syntax to use for compare a text from previous rules

like this

(defrule GetGender
(declare (salience 100))
(printout t "What's your gender ? (Male/Female): ")
(bind ?response (read))
(assert (Gender (gender ?response))))

and when I get answer from above like "Male" I want the rule below active.

(defrule GetShirt
(declare (salience 99))
(Gender (gender ?l))
(test (= ?l Male))
=>
(printout t "What's your shirt color ? (Blue/Black): ")
(bind ?response (read))
(assert (Shirt (shirt ?response))))

But seem (test and =) is not a syntax for string compare, and my English is not good enough, I don't even know about what "?l" in the code means

could somebody help me to fix this out please ?

Thank you.


Solution

  • Use = for comparing numbers and eq for comparing values of any type. In your GetShirt rule, the token ?l is a variable that is bound to the value of the gender slot so that it can be used in the expression (= ?l Male). When making simple comparisons to constants, it's not necessary to use the test conditional element. You can simply use the constant within the pattern:

    CLIPS> 
    (deftemplate response
       (slot attribute)
       (slot value))
    CLIPS> 
    (defrule GetGender
       =>
       (printout t "What's your gender ? (Male/Female): ")
       (bind ?response (read))
       (assert (response (attribute gender) (value ?response))))
    CLIPS> 
    (defrule GetShirt
       (response (attribute gender) (value Male))
       =>
       (printout t "What's your shirt color ? (Blue/Black): ")
       (bind ?response (read))
       (assert (response (attribute shirt) (value ?response))))
    CLIPS> (reset)
    CLIPS> (run)
    What's your gender ? (Male/Female): Male
    What's your shirt color ? (Blue/Black): Blue
    CLIPS>