必须如何实现 "forall" 以在特定模板的每个实例上循环
How must be implemented "forall" to loop on every instance of a certain template
我的代码:
(deftemplate person
(slot name (type STRING))
(slot age (type STRING))
(multislot title (type STRING))
(slot areaofknowledge (type SYMBOL))
(slot yearTEDtalk (type STRING))
)
(deffacts init
(assert (person
(name John)
(age 30)
(title Bla bla bla )
(areaofknowledge Bla)
(yearTEDtalk 2020)
)
)
(assert (person
(name Laura)
(age 50)
(title Bla bla bla )
(areaofknowledge Bla)
(yearTEDtalk 2019)
)
)
)
(defrules rules
(defrule assignpersontotalk
(forall (person
(name ?nameTED)
(title ?titleTED)
(year ?yearTED)
)
)
=>
(assert (TEDtalk-on ?titleTED ?yearTED ?nameTED ))
)
)
但似乎这不是通过所有人进行循环的正确方法,因为我得到:
我没有在 Google 上找到解决方案,Clips 手册仅说明您必须使用 foreall (patter1) (pattern2),这并不是很有帮助。
forall 条件元素至少需要两种模式。它以这种形式转换模式:
(forall <pattern-1>
<pattern-2>
.
.
.
<pattern-n>)
到这个表格:
(not (and <pattern-1>
(not (and <pattern-2>
.
.
.
<pattern-n>))))
无论如何,您不必显式迭代与规则匹配的事实:
CLIPS>
(deftemplate person
(slot name)
(slot age)
(slot title)
(slot areaofknowledge)
(slot yearTEDtalk))
CLIPS>
(deffacts init
(person
(name John)
(age 30)
(title "Bla bla bla")
(areaofknowledge Bla)
(yearTEDtalk 2020))
(person
(name Laura)
(age 50)
(title "Bla bla bla")
(areaofknowledge Bla)
(yearTEDtalk 2019)))
CLIPS>
(defrule assignpersontotalk
(person
(name ?nameTED)
(title ?titleTED)
(yearTEDtalk ?yearTED))
=>
(assert (TEDtalk-on ?titleTED ?yearTED ?nameTED)))
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (watch activations)
CLIPS> (reset)
<== f-0 (initial-fact)
==> f-0 (initial-fact)
==> f-1 (person (name John) (age 30) (title "Bla bla bla") (areaofknowledge Bla) (yearTEDtalk 2020))
==> Activation 0 assignpersontotalk: f-1
==> f-2 (person (name Laura) (age 50) (title "Bla bla bla") (areaofknowledge Bla) (yearTEDtalk 2019))
==> Activation 0 assignpersontotalk: f-2
CLIPS> (run)
FIRE 1 assignpersontotalk: f-2
==> f-3 (TEDtalk-on "Bla bla bla" 2019 Laura)
FIRE 2 assignpersontotalk: f-1
==> f-4 (TEDtalk-on "Bla bla bla" 2020 John)
CLIPS>
我的代码:
(deftemplate person
(slot name (type STRING))
(slot age (type STRING))
(multislot title (type STRING))
(slot areaofknowledge (type SYMBOL))
(slot yearTEDtalk (type STRING))
)
(deffacts init
(assert (person
(name John)
(age 30)
(title Bla bla bla )
(areaofknowledge Bla)
(yearTEDtalk 2020)
)
)
(assert (person
(name Laura)
(age 50)
(title Bla bla bla )
(areaofknowledge Bla)
(yearTEDtalk 2019)
)
)
)
(defrules rules
(defrule assignpersontotalk
(forall (person
(name ?nameTED)
(title ?titleTED)
(year ?yearTED)
)
)
=>
(assert (TEDtalk-on ?titleTED ?yearTED ?nameTED ))
)
)
但似乎这不是通过所有人进行循环的正确方法,因为我得到:
我没有在 Google 上找到解决方案,Clips 手册仅说明您必须使用 foreall (patter1) (pattern2),这并不是很有帮助。
forall 条件元素至少需要两种模式。它以这种形式转换模式:
(forall <pattern-1>
<pattern-2>
.
.
.
<pattern-n>)
到这个表格:
(not (and <pattern-1>
(not (and <pattern-2>
.
.
.
<pattern-n>))))
无论如何,您不必显式迭代与规则匹配的事实:
CLIPS>
(deftemplate person
(slot name)
(slot age)
(slot title)
(slot areaofknowledge)
(slot yearTEDtalk))
CLIPS>
(deffacts init
(person
(name John)
(age 30)
(title "Bla bla bla")
(areaofknowledge Bla)
(yearTEDtalk 2020))
(person
(name Laura)
(age 50)
(title "Bla bla bla")
(areaofknowledge Bla)
(yearTEDtalk 2019)))
CLIPS>
(defrule assignpersontotalk
(person
(name ?nameTED)
(title ?titleTED)
(yearTEDtalk ?yearTED))
=>
(assert (TEDtalk-on ?titleTED ?yearTED ?nameTED)))
CLIPS> (watch rules)
CLIPS> (watch facts)
CLIPS> (watch activations)
CLIPS> (reset)
<== f-0 (initial-fact)
==> f-0 (initial-fact)
==> f-1 (person (name John) (age 30) (title "Bla bla bla") (areaofknowledge Bla) (yearTEDtalk 2020))
==> Activation 0 assignpersontotalk: f-1
==> f-2 (person (name Laura) (age 50) (title "Bla bla bla") (areaofknowledge Bla) (yearTEDtalk 2019))
==> Activation 0 assignpersontotalk: f-2
CLIPS> (run)
FIRE 1 assignpersontotalk: f-2
==> f-3 (TEDtalk-on "Bla bla bla" 2019 Laura)
FIRE 2 assignpersontotalk: f-1
==> f-4 (TEDtalk-on "Bla bla bla" 2020 John)
CLIPS>