4

PyClips を使用して、Python インタープリターからデータを動的に取得するルールを Clips に構築しようとしています。これを行うには、マニュアルで概説されているように、外部関数を登録します。

以下のコードは、問題のおもちゃの例です。私がこれを行っているのは、Clips を使用して推論したい SQL データベースの形式で、大量のデータのコーパスを持つアプリケーションがあるためです。ただし、Clip を 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()
4

2 に答える 2

3

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()
于 2010-07-28T15:18:29.573 に答える
1

あなたの問題は(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を使用していませんが、それはあなたが望むことをするはずです。

チッ!

>>> 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()
>>>
于 2010-07-19T20:44:39.970 に答える