如果我有事实索引,是否可以获取事实信息?



我是新手,所以听起来可能很愚蠢,但我们开始了。

(deftemplate player
(slot nume)
(slot pozitie)
(slot goluri)
)
(deftemplate team
(slot nume)
(multislot players)
(slot plasamet)
(slot goluri (default 0))
)
(defrule goluriEchipa
?id <- (echipa (nume ?n) (players $?x ?y $?z)(goluri ?ge))
(player (nume ?y) (goluri ?gj))
=>
(modify ?id (goluri (+ ?gj ?ge)))
)

我知道为什么它卡在一个循环中,这是因为"goluri"中的总和一直在变化。因此,如果我像这样删除它,

(defrule goluriEchipa
?id <- (echipa (nume ?n) (jucatori $?x ?y $?z))
(jucator (nume ?y) (goluri ?gj))
=>
(modify ?id (goluri (+ ?gj ?ge)))
)

循环停止,但我仍然需要它的值。我有事实索引,我可以得到值吗? 我看到了一些?id:goluri在循环中工作的示例,但它在这里不起作用。

编辑:我忘了提,我的目标是在团队目标中添加所有球员进球的总和。

您正在寻找的函数是事实槽值,但您仍然会得到循环行为,因为即使槽中不存在槽,事实模式也可以通过更改槽来重新触发。

(defrule goluriEchipa
?id <- (echipa (nume ?n) (players $?x ?y $?z))
(jucator (nume ?y) (goluri ?gj))
=>
(bind ?ge (fact-slot-value ?id goluri))
(modify ?id (goluri (+ ?gj ?ge))))

相反,您可以使用事实查询函数:

CLIPS (6.31 2/3/18)
CLIPS> 
(deftemplate jucator
(slot nume)
(slot pozitie)
(slot goluri))
CLIPS> 
(deftemplate echipa
(slot nume)
(multislot players)
(slot plasamet)
(slot goluri (default 0)))
CLIPS>    
(deffacts start
(echipa (nume 1) (players Fred Bill Greg))
(jucator (nume Fred) (goluri 2))
(jucator (nume Bill) (goluri 1))
(jucator (nume Greg) (goluri 3))
(echipa (nume 2) (players Sam John Ralph))
(jucator (nume Sam) (goluri 2))
(jucator (nume John) (goluri 4))
(jucator (nume Ralph) (goluri 5)))
CLIPS>    
(defrule goluriEchipa
=>
(delayed-do-for-all-facts ((?id echipa)) TRUE
(bind ?sum 0)
(foreach ?p ?id:players
(bind ?sum (+ ?sum (do-for-fact ((?j jucator)) (eq ?j:nume ?p) ?j:goluri))))
(modify ?id (goluri ?sum))))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-2     (jucator (nume Fred) (pozitie nil) (goluri 2))
f-3     (jucator (nume Bill) (pozitie nil) (goluri 1))
f-4     (jucator (nume Greg) (pozitie nil) (goluri 3))
f-6     (jucator (nume Sam) (pozitie nil) (goluri 2))
f-7     (jucator (nume John) (pozitie nil) (goluri 4))
f-8     (jucator (nume Ralph) (pozitie nil) (goluri 5))
f-9     (echipa (nume 1) (players Fred Bill Greg) (plasamet nil) (goluri 6))
f-10    (echipa (nume 2) (players Sam John Ralph) (plasamet nil) (goluri 11))
For a total of 9 facts.
CLIPS> 

最新更新