在Clips Expert系统中使用Python函数 [英] Using Python Functions From the Clips Expert System

查看:179
本文介绍了在Clips Expert系统中使用Python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using PyClips, I'm trying to build rules in Clips that dynamically retrieve data from the Python interpreter. To do this, I register an external function as outlined in the manual.

下面的代码是该问题的一个示例.之所以这样做,是因为我有一个应用程序,该应用程序具有SQL数据库形式的大量数据集,我想使用Clips来说明这一点.但是,如果我可以简单地将Clips直接插入"到Python的命名空间中,我就不会浪费时间将所有这些数据转换为Clips断言.

The code below is a toy example of the problem. I'm doing this because I have an application with a large corpus of data, in the form of a SQL database, which I want to reason with using Clips. However, I don't want to waste time converting all this data into Clips assertions, if I can simply "plug" Clips directly into Python's namespace.

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

However, when I try to create the rule, I get an error. What am I doing wrong?

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()来激活某些规则.

I received some help on the PyClips support group. The solution is to ensure your Python function returns a clips.Symbol object and use (test ...) to evaluate functions in the LHS of rules. The use of Reset() also appears to be necessary to activate certain rules.

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()

这篇关于在Clips Expert系统中使用Python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆