编程语言
首页 > 编程语言> > 从剪辑专家系统使用Python函数

从剪辑专家系统使用Python函数

作者:互联网

使用PyClips,我正在尝试在Clips中构建规则,从Python解释器动态检索数据.为此,我注册了the manual中概述的外部函数.

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

但是,当我尝试创建规则时,我收到错误.我究竟做错了什么?

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,machine-learning,expert-system,clips
来源: https://codeday.me/bug/20190627/1299443.html