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

发布于 2024-12-28 20:19:04 字数 619 浏览 1 评论 0原文

我正在试验 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2025-01-04 20:19:04

有一个放错位置的括号,过早地关闭了规则,遗漏了 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,可以绑定并打印结果出来:

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

There's a misplaced bracket that closes the rule too soon leaving out the 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

If you want to verify that the addf actually returned 42, the result could be binded and printed it out:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文