0

我有这样定义的葡萄酒:

(deftemplate wine
  (slot name)
  (slot color)
  (slot certainty (type NUMBER) (default 0)))

并且列表自由葡萄酒定义如下:

(deffacts wines
  (wine (name "Chardonnay") (color white))
  (wine (name "Merlot") (color red))
  (wine (name "Cabernet sauvignon") (color red)))

现在,如果触发规则,我想增加列表中颜色槽设置为“红色”的项目的确定性值。

任何想法如何做到这一点?

4

1 回答 1

1

我是 CLIPS 的新手,所以我确信有更好的方法,但以下规则可以满足您的需求:

(defrule inc-wines-with-color
  (increase-all-with color ?color ?amount)
  (wine (name ?name) (color ?color))
  =>
  (assert (increase-certainty ?name ?amount)))

(defrule retract-inc-all-with
  ?f <- (increase-all-with $?)
  =>
  (retract ?f))

(defrule increase-wine-certainty
  (not (increase-all-with $?))
  ?ic <-(increase-certainty ?name ?amount)
  ?wine <- (wine (name ?name) (certainty ?c))
  =>
  (printout t "Incrementing " ?name " from " ?c " to " (+ ?amount ?c) crlf)
  (modify ?wine (certainty (+ ?amount ?c)))
  (retract ?ic))

以下是运行它的结果:

CLIPS> (reset)
CLIPS> (assert (increase-all-with color red 0.2))
<Fact-4>
CLIPS> (run)
Incrementing Merlot from 0 to 0.2
Incrementing Cabernet sauvignon from 0 to 0.2
CLIPS> (facts)
f-0     (initial-fact)
f-1     (wine (name "Chardonnay") (color white) (certainty 0))
f-7     (wine (name "Merlot") (color red) (certainty 0.2))
f-8     (wine (name "Cabernet sauvignon") (color red) (certainty 0.2))
For a total of 4 facts.

注意:您可能需要将冲突解决策略设置为 LEX 或 MEA,以保证规则的正确排序。

于 2012-07-18T14:37:57.110 回答