PYKE规则添加相似语句的计数

PYKE rules to add the count of similar statements

我是PYKE新手,在PYKE中写规则遇到了一个小问题

我的 kfb 文件中有以下语句。

ent_rel(1, sam, helsen,2)
ent_rel(1, sam, dunkin,1)
ent_rel(1, pirate, sam,2)

ent_rel(2, van, helsen,2)
ent_rel(2, sam, helsen,2)
ent_rel(2, pirate, bay,1)
ent_rel(2, van, burger,1)

ent_rel(3, burger, house,1)
ent_rel(3, sam, helsen,1)

我想编写一个生成如下输出的规则:

ent_rel1(sam, helsen,5)
ent_rel1(sam, dunkin,1)
ent_rel1(pirate, sam,2)
ent_rel1(pirate, bay,1)
ent_rel1(van, helsen,2)
ent_rel1(van, burger,1)
ent_rel1(burger, house,1)

我只是想添加类似的语句而不考虑 ID。

我写了下面的规则,但这给出了不同的输出。

relationship_cnt
    foreach
        a.ent_rel($id1, $f1, $s1, $n1)
        a.ent_rel($id2, $f2, $s2, $n2)
        check $id1 != $id2
        check $f1 == $f2
        check $s1 == $s2
        $tot = $n1 + $n2
    assert
        a.ent_rel1($f1,$s1,$tot)

输出:

ent_rel1('sam', 'helsen', 4)
ent_rel1('sam', 'helsen', 3)

我明白为什么我的输出不正确,正如我提到的 $id1 和 $id2。它在两个不同的 id 中查找相同的名称 "sam" 和 "helsen" 并添加它们。

但是我无法编写正确的规则。如果有任何帮助,我将不胜感激。

谢谢

我的解决方案:

relationship_cnt
    foreach
        data.ent_rel($id1, $f1, $s1, $n1)

        python tot = $n1

        forall
            data.ent_rel($id2, $f1, $s1, $n2)
            check $id1 != $id2
            python tot = tot + $n2

        $tot = int(tot)
    assert
        data.ent_rel1($f1, $s1, $tot)

使用 forall 遍历每个元组 ($f1, $s1),确保使用 check 的唯一 $id。为了求和,请使用 python 变量,因为 $n2 的值未绑定在 forall 前提之外;参见 pyke: Notes on Forall and Notany Premises