使用PyClips,我试图在片段中构建规则,动态地从Python解释器检索数据。为此,我注册了一个外部函数,如the manual中所述。
下面的代码是这个问题的一个玩具示例。我之所以这样做,是因为我有一个应用程序,它有一个SQL数据库形式的大型数据语料库,我想使用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()发布于 2010-07-28 23:18:30
我在PyClips支持组上得到了一些帮助。解决方案是确保Python函数返回一个clips.Symbol对象,并使用(测试...)评估规则的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()发布于 2010-07-20 04:44:40
你的问题与(neq (python-call py_getvar user) 'None')有关。显然,clips不喜欢嵌套语句。似乎试图将函数调用包装在一个相等语句中是不好的。但是,您永远不会断言该值,因为您的函数返回Nil或该值。相反,您要做的是:
def py_getvar(k):
return clips.Symbol('TRUE') if globals.get(k) else clips.Symbol('FALSE')然后只需将"(neq (python-call py_getvar user) 'None')"更改为"(python-call py_getvar user)"
这应该是可行的。在刚刚摆弄它之前,我没有使用过pyclips,但它应该可以做你想要的事情。
哈!
>>> import clips
>>> def py_getvar(k):
... return clips.Symbol('TRUE') if globals.get(k) else clips.Symbol('FALSE')
...
>>> clips.RegisterPythonFunction(py_getvar)
>>> clips.BuildRule("user-rule", "(python-call py_getvar user)", "(assert (user-
present))", "the user rule")
<Rule 'user-rule': defrule object at 0x00A691D0>
>>> clips.Run()
0
>>> clips.PrintFacts()
>>>https://stackoverflow.com/questions/3247952
复制相似问题