我是名为CLIPS的专家系统语言的初学者。我需要帮助编写一个CLIPS脚本来决定,给出关于属性A,B和C(密度,电导率等)的三个事实,具有这些事实/属性的对象是否属于某种类型,比如TYPE1。我需要编写做出此决定所必需的规则。谢谢您的帮助。
KB
答案 0 :(得分:0)
优雅是主观的,但如果你想要可重用和可扩展的代码,尽可能多地表示信息,以便你可以编写通用规则。
CLIPS>
(deftemplate obj
(slot obj-name)
(slot print-name))
CLIPS>
(deftemplate opv
(slot obj-name)
(slot prop)
(slot val))
CLIPS>
(deftemplate type
(slot type-name)
(slot print-name))
CLIPS>
(deftemplate type-prop
(slot type-name)
(slot prop-name)
(slot prop-test)
(multislot values))
CLIPS>
(defrule is-type
?o <- (obj (obj-name ?obj-name))
?t <- (type (type-name ?type-name))
(forall (type-prop (type-name ?type-name)
(prop-name ?prop-name)
(prop-test ?prop-test)
(values $?values))
(opv (obj-name ?obj-name)
(prop ?prop-name)
(val ?val))
(test (funcall ?prop-test ?val (expand$ ?values))))
=>
(printout t (fact-slot-value ?o print-name)
" is "
(fact-slot-value ?t print-name)
crlf))
CLIPS>
(deffacts objects
(obj (obj-name thing1) (print-name "Thing 1"))
(opv (obj-name thing1) (prop color) (val red))
(opv (obj-name thing1) (prop weight) (val 5))
(obj (obj-name thing2) (print-name "Thing 2"))
(opv (obj-name thing2) (prop color) (val blue))
(opv (obj-name thing2) (prop weight) (val 20))
(obj (obj-name thing3) (print-name "Thing 3"))
(opv (obj-name thing3) (prop color) (val white))
(opv (obj-name thing3) (prop weight) (val 10)))
CLIPS>
(deffacts types
(type (type-name light) (print-name "light"))
(type-prop (type-name light) (prop-name weight) (prop-test <=) (values 5))
(type (type-name heavy) (print-name "heavy"))
(type-prop (type-name heavy) (prop-name weight) (prop-test >=) (values 20))
(type (type-name just-right) (print-name "just right"))
(type-prop (type-name just-right) (prop-name weight) (prop-test >=) (values 20))
(type-prop (type-name just-right) (prop-name color) (prop-test one-of) (values red green blue)))
CLIPS>
(deffunction one-of (?val $?rest)
(member$ ?val ?rest))
CLIPS> (reset)
CLIPS> (run)
Thing 2 is just right
Thing 2 is heavy
Thing 1 is light
CLIPS>