Clips 中的触发规则无法正常工作
Firing rule in Clips not working properly
我是新的ti剪辑,正在尝试编写触发规则集的规则。
规则的条件是
计算产品1、产品2、产品3的折扣为10,前提是产品1存在(必填)。并且商品总数大于等于6。
(deftemplate Product
(slot productNumber)
(slot quantity))
(defrule sum_of_quantity
(exists (Product (productNumber 1 | 2 | 3)(quantity ?q&:(> ?q 1))))
=>
(bind ?totalQuantity 0)
(do-for-all-facts ((?p Product))
(or (eq ?p:productNumber 1)
(eq ?p:productNumber 2)
(eq ?p:productNumber 3))
(bind ?totalQuantity (+ ?totalQuantity ?p:quantity)))
(if (>= ?totalQuantity 6) then
(printout t "TotalQuantity is " ?totalQuantity "and discoun is 10" crlf)))
(deffacts input1
(Product (productNumber 2)(quantity 3))
(Product (productNumber 3)(quantity 3)))
输入的事实如图所示,即使没有强制产品,计算结果也显示为 10。
建议。
CLIPS (6.31 6/12/19)
CLIPS>
(deftemplate Product
(slot productNumber)
(slot quantity))
CLIPS>
(defrule sum_of_quantity
(exists (Product (productNumber 1) (quantity ?q&:(> ?q 1))))
=>
(bind ?totalQuantity 0)
(do-for-all-facts ((?p Product))
(or (eq ?p:productNumber 1)
(eq ?p:productNumber 2)
(eq ?p:productNumber 3))
(bind ?totalQuantity (+ ?totalQuantity ?p:quantity)))
(if (>= ?totalQuantity 6) then
(printout t "TotalQuantity is " ?totalQuantity " and discount is 10" crlf)))
CLIPS> (assert (Product (productNumber 1) (quantity 6)))
<Fact-1>
CLIPS> (run)
TotalQuantity is 6 and discount is 10
CLIPS> (reset)
CLIPS> (assert (Product (productNumber 1) (quantity 3)))
<Fact-1>
CLIPS> (assert (Product (productNumber 2) (quantity 2)))
<Fact-2>
CLIPS> (assert (Product (productNumber 3) (quantity 4)))
<Fact-3>
CLIPS> (run)
TotalQuantity is 9 and discount is 10
CLIPS> (reset)
CLIPS> (assert (Product (productNumber 2) (quantity 2)))
<Fact-1>
CLIPS> (assert (Product (productNumber 3) (quantity 4)))
<Fact-2>
CLIPS> (run)
CLIPS>
我是新的ti剪辑,正在尝试编写触发规则集的规则。 规则的条件是
计算产品1、产品2、产品3的折扣为10,前提是产品1存在(必填)。并且商品总数大于等于6。
(deftemplate Product
(slot productNumber)
(slot quantity))
(defrule sum_of_quantity
(exists (Product (productNumber 1 | 2 | 3)(quantity ?q&:(> ?q 1))))
=>
(bind ?totalQuantity 0)
(do-for-all-facts ((?p Product))
(or (eq ?p:productNumber 1)
(eq ?p:productNumber 2)
(eq ?p:productNumber 3))
(bind ?totalQuantity (+ ?totalQuantity ?p:quantity)))
(if (>= ?totalQuantity 6) then
(printout t "TotalQuantity is " ?totalQuantity "and discoun is 10" crlf)))
(deffacts input1
(Product (productNumber 2)(quantity 3))
(Product (productNumber 3)(quantity 3)))
输入的事实如图所示,即使没有强制产品,计算结果也显示为 10。
建议。
CLIPS (6.31 6/12/19)
CLIPS>
(deftemplate Product
(slot productNumber)
(slot quantity))
CLIPS>
(defrule sum_of_quantity
(exists (Product (productNumber 1) (quantity ?q&:(> ?q 1))))
=>
(bind ?totalQuantity 0)
(do-for-all-facts ((?p Product))
(or (eq ?p:productNumber 1)
(eq ?p:productNumber 2)
(eq ?p:productNumber 3))
(bind ?totalQuantity (+ ?totalQuantity ?p:quantity)))
(if (>= ?totalQuantity 6) then
(printout t "TotalQuantity is " ?totalQuantity " and discount is 10" crlf)))
CLIPS> (assert (Product (productNumber 1) (quantity 6)))
<Fact-1>
CLIPS> (run)
TotalQuantity is 6 and discount is 10
CLIPS> (reset)
CLIPS> (assert (Product (productNumber 1) (quantity 3)))
<Fact-1>
CLIPS> (assert (Product (productNumber 2) (quantity 2)))
<Fact-2>
CLIPS> (assert (Product (productNumber 3) (quantity 4)))
<Fact-3>
CLIPS> (run)
TotalQuantity is 9 and discount is 10
CLIPS> (reset)
CLIPS> (assert (Product (productNumber 2) (quantity 2)))
<Fact-1>
CLIPS> (assert (Product (productNumber 3) (quantity 4)))
<Fact-2>
CLIPS> (run)
CLIPS>