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

标签 python clips expert-system pyclips

我正在试验 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/

相关文章:

python - np.where( ) 不改变所有的值

python - Numpy Where 有两个以上的条件

python - Django,更改settings.py的默认位置或使用其他文件

python - 在 PyCLIPS 中注册 Python 方法的装饰器

language-features - 三值变量,最大值,最小值,实际值

java - 作为过程的功能的动态(树形)结构(以及Clojure中的实现)

python - instance.__dict__ & class.__dict__

java - 从 CLIPS 调用 Java 应用程序

android - 安卓平台专家系统

c++ - 如果事实不存在,CLIPS eval 将停止工作