专家系统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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值