在 CLIPS 中定义 "anonymous" 规则
Defining "anonymous" rules in CLIPS
我知道CLIPS中的规则通常使用defrule
来命名。此规则称为 rule1
:
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule rule1
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
但是我还是不知道规则有没有必要命名。是否可以定义一个规则而不给它一个名字,像这样?
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
来自基本编程指南的第 5 部分:
(defrule <rule-name> [<comment>]
[<declaration>]
<conditional-element>*
=>
<action>*)
需要规则名称。
要动态生成规则名称,请使用创建唯一符号的 gensym* 函数:
CLIPS>
(deffunction create-anonymous (?conditions ?actions)
(bind ?str (str-cat "(defrule " (gensym*) " "
?conditions " => " ?actions ")"))
(build ?str))
CLIPS> (deftemplate animal (slot species))
CLIPS>
(create-anonymous "(animal (species ?name))"
"(printout t ?name crlf)")
TRUE
CLIPS> (rules)
gen1
For a total of 1 defrule.
CLIPS> (ppdefrule gen1)
(defrule MAIN::gen1
(animal (species ?name))
=>
(printout t ?name crlf))
CLIPS>
我知道CLIPS中的规则通常使用defrule
来命名。此规则称为 rule1
:
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule rule1
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
但是我还是不知道规则有没有必要命名。是否可以定义一个规则而不给它一个名字,像这样?
(deftemplate animal (slot species))
(deffacts animal
(animal (species dog)))
(defrule
(animal (species ?name))
=>
(printout t ?name crlf))
(reset)
(run)
(exit)
;
来自基本编程指南的第 5 部分:
(defrule <rule-name> [<comment>]
[<declaration>]
<conditional-element>*
=>
<action>*)
需要规则名称。
要动态生成规则名称,请使用创建唯一符号的 gensym* 函数:
CLIPS>
(deffunction create-anonymous (?conditions ?actions)
(bind ?str (str-cat "(defrule " (gensym*) " "
?conditions " => " ?actions ")"))
(build ?str))
CLIPS> (deftemplate animal (slot species))
CLIPS>
(create-anonymous "(animal (species ?name))"
"(printout t ?name crlf)")
TRUE
CLIPS> (rules)
gen1
For a total of 1 defrule.
CLIPS> (ppdefrule gen1)
(defrule MAIN::gen1
(animal (species ?name))
=>
(printout t ?name crlf))
CLIPS>