gpt4 book ai didi

python - 使用 Clips 专家系统中的 Python 函数

转载 作者:太空狗 更新时间:2023-10-30 03:07:46 26 4
gpt4 key购买 nike

我尝试使用 PyClips 在 Clips 中构建规则,以动态地从 Python 解释器检索数据。为此,我注册了一个外部函数,如 the manual 中所述。 .

下面的代码是问题的玩具示例。我这样做是因为我有一个包含大量数据的应用程序,采用 SQL 数据库的形式,我想使用 Clips 对其进行推理。但是,如果我可以简单地将 Clips 直接“插入”到 Python 的命名空间中,我不想浪费时间将所有这些数据转换为 Clips 断言。

但是,当我尝试创建规则时,出现错误。我做错了什么?

import clips

#user = True

#def py_getvar(k):
# return globals().get(k)
def py_getvar(k):
return True if globals.get(k) else clips.Symbol('FALSE')

clips.RegisterPythonFunction(py_getvar)

print clips.Eval("(python-call py_getvar user)") # Outputs "nil"

# If globals().get('user') is not None: assert something
clips.BuildRule("user-rule", "(neq (python-call py_getvar user) nil)", "(assert (user-present))", "the user rule")
#clips.BuildRule("user-rule", "(python-call py_getvar user)", "(assert (user-present))", "the user rule")

clips.Run()
clips.PrintFacts()

最佳答案

我在 PyClips 支持小组获得了一些帮助。解决方案是确保您的 Python 函数返回一个 clips.Symbol 对象并使用 (test ...) 来评估规则的 LHS 中的函数。 Reset() 的使用似乎也是激活某些规则所必需的。

import clips
clips.Reset()

user = True

def py_getvar(k):
return (clips.Symbol('TRUE') if globals().get(k) else clips.Symbol('FALSE'))

clips.RegisterPythonFunction(py_getvar)

# if globals().get('user') is not None: assert something
clips.BuildRule("user-rule", "(test (eq (python-call py_getvar user) TRUE))",
'(assert (user-present))',
"the user rule")

clips.Run()
clips.PrintFacts()

关于python - 使用 Clips 专家系统中的 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3247952/

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