【问题标题】:clips use string to make a compare condition剪辑使用字符串进行比较条件
【发布时间】:2018-07-30 21:19:43
【问题描述】:

我对 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 and =) 不是字符串比较的语法,而且我的英文还不够好,我什至不知道代码中的“?l”是什么意思

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

谢谢。

【问题讨论】:

    标签: string syntax compare conditional-statements clips


    【解决方案1】:

    = 用于比较数字。

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

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

    【讨论】:

    • 嗨 noxdafox 谢谢你的回答,你能告诉我如何在我的代码中使用它吗,我不太明白,什么是“foo”和“bar”?
    【解决方案2】:

    使用 = 比较数字,使用 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> 
    

    【讨论】:

    • 嗨,Gary Riley,非常感谢你的榜样,几分钟前我已经和你一起做了,@noxdafox 帮助,我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2021-06-10
    • 2010-12-24
    • 2011-10-16
    • 2012-06-12
    相关资源
    最近更新 更多