如果我有事实索引,可以得到事实信息吗?

Can get the fact info if i have the fact index?

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

(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 在 for 循环中工作的例子,但它在这里不起作用。

编辑:忘了说了,我的目标是在团队目标中加上所有玩家目标的总和。

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

(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>