简体   繁体   English

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

[英]Using Python Functions From the Clips Expert System

Using PyClips, I'm trying to build rules in Clips that dynamically retrieve data from the Python interpreter. 使用PyClips,我正在尝试在Clips中构建规则,从Python解释器动态检索数据。 To do this, I register an external function as outlined in the manual . 为此,我按照手册中的说明注册了一个外部功能。

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. 我这样做是因为我有一个带有大量数据的应用程序,以SQL数据库的形式,我想使用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. 但是,我不想浪费时间将所有这些数据转换为Clips断言,如果我可以简单地将Clips直接“插入”Python的命名空间中。

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

I received some help on the PyClips support group. 我在PyClips支持小组上得到了一些帮助。 The solution is to ensure your Python function returns a clips.Symbol object and use (test ...) to evaluate functions in the LHS of rules. 解决方案是确保您的Python函数返回一个clips.Symbol对象并使用(test ...)来评估规则的LHS中的函数。 The use of Reset() also appears to be necessary to activate certain rules. 使用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()

Your problem has something to do with the (neq (python-call py_getvar user) 'None') . 你的问题与(neq (python-call py_getvar user) 'None') Apparently clips doesn't like the nested statement. 显然,剪辑不喜欢嵌套语句。 It appears that trying to wrap a function call in an equality statement does bad things. 看起来试图在一个相等的语句中包装函数调用会带来不好的事情。 However you'll never assert the value anyway as your function returns either Nil or the value. 但是,当函数返回Nil或值时,您永远不会断言值。 Instead what you'll want to do is this: 相反,你要做的是:

def py_getvar(k):
    return clips.Symbol('TRUE') if globals.get(k) else clips.Symbol('FALSE')

then just change "(neq (python-call py_getvar user) 'None')" to "(python-call py_getvar user)" 然后只需更改"(neq (python-call py_getvar user) 'None')""(python-call py_getvar user)"

And that should work. 那应该有用。 Haven't used pyclips before messing with it just now, but that should do what you want. 刚刚在搞乱之前没有使用pyclips,但这应该做你想要的。

HTH! HTH!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择将CLIPS专家系统暴露为Web应用程序的体系结构 - Choice of architecture for exposing CLIPS expert system as web application Python 中的专家系统,具有一定的问题顺序 - Expert system in Python with certain order of questions 应该在Python中使用知识引擎专家系统吗? - Should a use a Knowledge Engine for Expert System in Python? Python中(专家系统)的后向和前向链接算法 - Backward and forward chaining algorithm for (expert system) in Python 为什么用clipspy在Python中创建专家系统会出现这个错误? - Why does this error occur when creating an expert system in Python using clipspy? 使用 Python 分割音频片段 - Dividing audio clips using Python 使用pyclips并将片段导入为python模块 - using pyclips and import clips as a python module 如何使用 Selenium 和 Python 从 https://www.twitch.tv/directory/game/Overwatch/clips?range=7d 抓取前 10 个剪辑的 href 属性 - How to scrape the href attributes of the top 10 clips from https://www.twitch.tv/directory/game/Overwatch/clips?range=7d using Selenium and Python 如何使用 python 重复部分剪辑 n 次 - How to repeat parts of clips n number of time using python 如何向用户显示问题并使用“if”“else”语句专家系统 django 进行比较? - how to display questions to user and compare them using "if" "else" statements expert system django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM