使用 PyClips,我尝试在 Clips 中构建规则,从 Python 解释器动态检索数据。为此,我注册了一个外部函数,如下所示手册 http://pyclips.sourceforge.net/manual/pyclips-overview.html#SECTION003250000000000000000.
下面的代码是该问题的一个玩具示例。我这样做是因为我有一个应用程序,其中包含大量数据(采用 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)