【问题标题】:CLIPS counting facts or template instances that match the patternCLIPS 计数匹配模式的事实或模板实例
【发布时间】:2014-09-24 00:57:42
【问题描述】:

首先我声明:

(deftemplate worker (插槽编号 (输入字符串) (默认?DERIVE))

(时薪 (类型浮动) (默认?DERIVE)))

然后我补充:

(assert(worker(id "a")(salary 30.0)))

(assert(worker(id "b")(salary 40.0)))

(assert(worker(id "c")(salary 60.0)))

(assert(worker(id "d")(salary 70.0)))

(assert(worker(id "e")(salary 10.0)))

我如何计算我有多少“工人”?

例如,我如何计算有多少工人的薪水超过 30?

【问题讨论】:

    标签: clips expert-system


    【解决方案1】:

    使用事实集查询函数:

    CLIPS> 
    (deftemplate worker
       (slot id (type STRING) (default ?DERIVE))
       (slot salary (type FLOAT) (default ?DERIVE)))
    CLIPS> (assert (worker (id "a") (salary 30.0)))
    <Fact-1>
    CLIPS> (assert (worker (id "b") (salary 40.0)))
    <Fact-2>
    CLIPS> (assert (worker (id "c") (salary 60.0)))
    <Fact-3>
    CLIPS> (assert (worker (id "d") (salary 70.0)))
    <Fact-4>
    CLIPS> (assert (worker (id "e") (salary 10.0)))
    <Fact-5>
    CLIPS> (find-all-facts ((?f worker)) (> ?f:salary 30.0))
    (<Fact-2> <Fact-3> <Fact-4>)
    CLIPS> (length$ (find-all-facts ((?f worker)) (> ?f:salary 30.0)))
    3
    CLIPS> (do-for-all-facts ((?f worker)) (> ?f:salary 30.0) (printout t ?f:id crlf))
    b
    c
    d
    CLIPS>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多