【问题标题】:Error while calculating discount using CLIPS rule engine使用 CLIPS 规则引擎计算折扣时出错
【发布时间】:2019-12-12 10:35:34
【问题描述】:

我是剪辑规则引擎的新手,必须在移动应用程序的剪辑中进行跟踪。 如果一个人购买 x 数量的产品 A,给他 y 折扣。

以下是我编写的代码,用于首先检查我的 productid 是否在订单中。

(deftemplate Producttemp
(slot productid (type INTEGER))
(slot umid)
(slot quantity (type INTEGER))
)

(deffacts orders
(Producttemp (productid 123) (umid CG) (quantity 4))
(Producttemp (productid 456) (umid CG) (quantity 2))
)



(defrule checkorder
=>
 (printout t "Enter the productid: ")
  (bind ?p1 (readline))
   (printout t "Enter another quantity: ")
   (bind ?p2 (readline))

   (do-for-all-facts ((?o Producttemp)) 
       (and (eq ?p1 ?o:p1)
            (eq ?p2 ?o:p2))
       (printout t ?p1 " is a " ?o: productid" in order " ?p2 crlf)))
)

我收到以下错误。

Defining defrule: checkorder 
[PRCCODE3] Undefined variable o: referenced in RHS of defrule.

【问题讨论】:

    标签: clips


    【解决方案1】:

    最后一个打印输出语句中的 ?o:product 之间有一个空格。规则末尾还有一个无关紧要的右括号。加载此规则时不会出现任何语法错误:

    (defrule checkorder
       =>
       (printout t "Enter the productid: ")
       (bind ?p1 (readline))
       (printout t "Enter another quantity: ")
       (bind ?p2 (readline))
    
       (do-for-all-facts ((?o Producttemp)) 
           (and (eq ?p1 ?o:p1)
                (eq ?p2 ?o:p2))
           (printout t ?p1 " is a " ?o:productid" in order " ?p2 crlf))
    )
    

    您的规则还有两个问题。在事实查询中,您通过引用 ?o:p1 引用 Producttemp 事实的 p1p2 槽和 ?o:p2,但这些插槽不存在。也许您想要 productidquantity 插槽。您还可以使用 readline 函数来获取输入。这将返回一个字符串,但您的事实槽包含符号和整数,而不是字符串,因此使用 eq 函数对这些值进行的任何比较都将失败,因为类型不同。您应该改用 read 函数。

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 2012-03-24
      • 1970-01-01
      • 2014-09-28
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2016-04-29
      相关资源
      最近更新 更多