【问题标题】:Using Python Functions From the Clips Expert System使用 Clips 专家系统中的 Python 函数
【发布时间】:2011-03-15 22:42:43
【问题描述】:

使用 PyClips,我试图在 Clips 中构建规则,以便从 Python 解释器动态检索数据。为此,我按照the manual 中所述注册了一个外部函数。

下面的代码是该问题的一个玩具示例。我这样做是因为我有一个包含大量数据的应用程序,以 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()

【问题讨论】:

    标签: python machine-learning expert-system clips


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您的问题与(neq (python-call py_getvar user) 'None') 有关。显然剪辑不喜欢嵌套语句。似乎试图将函数调用包装在等式语句中会做坏事。但是,您永远不会断言该值,因为您的函数返回 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,但这应该可以满足您的需求。

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

      【讨论】:

      • 很抱歉,您的解决方案不起作用。我犯了同样的错误。请注意,目标是在任意 Clips 表达式中使用 Python 函数的结果,因此删除 (neq ...) 是不可接受的。
      • (neq...) 返回符号 TRUE 或 FALSE - 尽管将 Nil(由 get_var 返回)与字符串“None”的比较取反的结果将始终不为假,意思是或 TRUE,无论全局变量包含什么。但是,如果您想要基于函数输入的符号 TRUE 或 FALSE,只需从该函数返回值。
      猜你喜欢
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2011-12-22
      • 2011-01-23
      • 1970-01-01
      相关资源
      最近更新 更多