如何使用 PyClips 激活规则来调用 python 函数
我正在试验 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
我做错了什么?
I am experimenting with PyClips and I want to integrate it tightly with Python, so that when a rule is activated, it calls a python function.
Here is what I have so far:
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 )
""")
However, when I assert the fact 'animal-is duck', my python function is NOT being called:
>>> clips.Assert("(animal-is duck)")
<Fact 'f-0': fact object at 0x7fe4cb323720>
>>> clips.Run()
0
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个放错位置的括号,过早地关闭了规则,遗漏了
python-call
:如果您想验证
addf
是否实际返回 42,可以绑定并打印结果出来:There's a misplaced bracket that closes the rule too soon leaving out the
python-call
:If you want to verify that the
addf
actually returned 42, the result could be binded and printed it out: