如何使用PyClips获取规则激活来调用python函数

2024-06-16 13:36:14 发布

您现在位置: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函数没有被调用:

^{pr2}$

我做错什么了?在


Tags: 函数importbuildreturnis规则def情况
1条回答
网友
1楼 · 发布于 2024-06-16 13:36:14

有一个放错地方的方括号过早地结束了规则,忽略了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,则可以绑定结果并将其打印出来:

^{pr2}$

相关问题 更多 >