剪辑使用字符串来进行比较条件

时间:2018-02-20 13:33:04

标签: string syntax compare conditional-statements clips

我对Clips专家系统非常陌生 我正在寻找用于比较之前规则中的文本的语法

像这样

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

当我从上面得到答案时,例如"男性"我希望下面的规则有效。

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

但似乎(test和=)不是字符串比较的语法,我的英语不够好,我甚至都不知道"?l"在代码意味着

有人可以帮我解决这个问题吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

=用于比较数字。

对于字符串,您需要使用eq函数。

In [1]: (eq "foo" "bar")
FALSE
In [2]: (eq "foo" "foo")
TRUE

答案 1 :(得分:0)

使用=来比较数字和eq以比较任何类型的值。在您的GetShirt规则中,令牌?l是一个变量,它绑定到性别槽的值,以便可以在表达式中使用(=?l Male)。在对常量进行简单比较时,不必使用测试条件元素。您只需在模式中使用常量:

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>