gpt4 book ai didi

python - 如何使用 PyClips 激活规则以调用 python 函数

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:16 27 4
gpt4 key购买 nike

我正在试验 PyClips我想将它与 Python 紧密集成,以便在激活规则时调用 python 函数。

这是我目前所拥有的:

import clips

def addf(a, b):
return a + b

clips.RegisterPythonFunction(addf)

clips.Build("""
(defrule duck
(animal-is duck)
=>
(assert (sound-is quack))
(printout t "it’s a duck" crlf))
(python-call addf 40 2 )
""")

但是,当我断言“动物是鸭子”这一事实时,我的 python 函数没有被调用:

>>> clips.Assert("(animal-is duck)")
<Fact 'f-0': fact object at 0x7fe4cb323720>
>>> clips.Run()
0

我做错了什么?

最佳答案

有一个放错地方的括号过早地关闭了规则,遗漏了 python-call:

clips.Build("""
(defrule duck
(animal-is duck)
=>
(assert (sound-is quack))
(printout t "it's a duck" crlf))
(python-call addf 40 2 ) ^
""") ^ |
| this one
|
should go here

如果你想验证 addf 确实返回了 42,结果可以被绑定(bind)并打印出来:

clips.Build("""
(defrule duck
(animal-is duck)
=>
(assert (sound-is quack))
(printout t \"it's a duck\" crlf)
(bind ?tot (python-call addf 40 2 ))
(printout t ?tot crlf))
""")


clips.Assert("(animal-is duck)")
clips.Run()
t = clips.StdoutStream.Read()
print t

关于python - 如何使用 PyClips 激活规则以调用 python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8973556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com