有值列表。如果项目存在多次,增加价值?
Having list with values. If item exists several times, add value?
我想开始使用 CLIPS,尝试实现一个非常简单的任务:
Item1 = 5
Item2 = 7
Item3 = 8
Item1 = 10
假设我有上面的变量或赋值。现在我想要一个脚本来订购具有最小值到最大值的项目。此外,具有相同名称(第 1 项)的项的值应相加。
已经用其他语言实现了这一点,但我完全迷失在 CLIPS 中。
NewList= {Item2=7,Item3=8,Item1=15}
CLIPS>
(defclass ENTRY
(is-a USER)
(slot key)
(slot value))
CLIPS>
(defclass DICTIONARY
(is-a USER)
(multislot entries))
CLIPS>
(defmessage-handler DICTIONARY find-key-position (?key)
(bind ?index 1)
(foreach ?e ?self:entries
(if (eq (send ?e get-key) ?key)
then
(return ?index))
(bind ?index (+ ?index 1)))
(return FALSE))
CLIPS>
(defmessage-handler DICTIONARY add (?key ?value)
(bind ?position
(send ?self find-key-position ?key))
(if ?position
then
(bind ?entry (nth$ ?position ?self:entries))
(send ?entry put-value (+ (send ?entry get-value) ?value))
else
(bind ?position (+ (length$ ?self:entries) 1))
(bind ?entry
(make-instance of ENTRY (key ?key) (value ?value)))
(slot-direct-insert$ entries ?position ?entry)))
CLIPS>
(defmessage-handler DICTIONARY display ()
(printout t "{")
(foreach ?e ?self:entries
(printout t " " (send ?e get-key) "=" (send ?e get-value)))
(printout t " }" crlf))
CLIPS>
(deffunction sort-by-value (?e1 ?e2)
(> (send ?e1 get-value) (send ?e2 get-value)))
CLIPS>
(defmessage-handler DICTIONARY sort ()
(bind ?self:entries (sort sort-by-value ?self:entries))
TRUE)
CLIPS> (make-instance d1 of DICTIONARY)
[d1]
CLIPS> (send [d1] display)
{ }
CLIPS> (send [d1] add item1 5)
TRUE
CLIPS> (send [d1] add item2 7)
TRUE
CLIPS> (send [d1] add item3 8)
TRUE
CLIPS> (send [d1] display)
{ item1=5 item2=7 item3=8 }
CLIPS> (send [d1] add item1 10)
15
CLIPS> (send [d1] display)
{ item1=15 item2=7 item3=8 }
CLIPS> (send [d1] sort)
TRUE
CLIPS> (send [d1] display)
{ item2=7 item3=8 item1=15 }
CLIPS>
我想开始使用 CLIPS,尝试实现一个非常简单的任务:
Item1 = 5
Item2 = 7
Item3 = 8
Item1 = 10
假设我有上面的变量或赋值。现在我想要一个脚本来订购具有最小值到最大值的项目。此外,具有相同名称(第 1 项)的项的值应相加。
已经用其他语言实现了这一点,但我完全迷失在 CLIPS 中。
NewList= {Item2=7,Item3=8,Item1=15}
CLIPS>
(defclass ENTRY
(is-a USER)
(slot key)
(slot value))
CLIPS>
(defclass DICTIONARY
(is-a USER)
(multislot entries))
CLIPS>
(defmessage-handler DICTIONARY find-key-position (?key)
(bind ?index 1)
(foreach ?e ?self:entries
(if (eq (send ?e get-key) ?key)
then
(return ?index))
(bind ?index (+ ?index 1)))
(return FALSE))
CLIPS>
(defmessage-handler DICTIONARY add (?key ?value)
(bind ?position
(send ?self find-key-position ?key))
(if ?position
then
(bind ?entry (nth$ ?position ?self:entries))
(send ?entry put-value (+ (send ?entry get-value) ?value))
else
(bind ?position (+ (length$ ?self:entries) 1))
(bind ?entry
(make-instance of ENTRY (key ?key) (value ?value)))
(slot-direct-insert$ entries ?position ?entry)))
CLIPS>
(defmessage-handler DICTIONARY display ()
(printout t "{")
(foreach ?e ?self:entries
(printout t " " (send ?e get-key) "=" (send ?e get-value)))
(printout t " }" crlf))
CLIPS>
(deffunction sort-by-value (?e1 ?e2)
(> (send ?e1 get-value) (send ?e2 get-value)))
CLIPS>
(defmessage-handler DICTIONARY sort ()
(bind ?self:entries (sort sort-by-value ?self:entries))
TRUE)
CLIPS> (make-instance d1 of DICTIONARY)
[d1]
CLIPS> (send [d1] display)
{ }
CLIPS> (send [d1] add item1 5)
TRUE
CLIPS> (send [d1] add item2 7)
TRUE
CLIPS> (send [d1] add item3 8)
TRUE
CLIPS> (send [d1] display)
{ item1=5 item2=7 item3=8 }
CLIPS> (send [d1] add item1 10)
15
CLIPS> (send [d1] display)
{ item1=15 item2=7 item3=8 }
CLIPS> (send [d1] sort)
TRUE
CLIPS> (send [d1] display)
{ item2=7 item3=8 item1=15 }
CLIPS>