クリップエキスパートシステムからPython関数を使用する
-
15-09-2020 - |
質問
Pyclipsを使用して、Pythonインタプリタからデータを動的に検索するクリップのルールを構築しようとしています。これを行うには、マニュアルの外部関数を登録します。。
以下のコードは問題の玩具例です。私はこれをやっています。私はSQLデータベースの形で大きなコーパスを持つアプリケーションを持っています。ただし、このデータをすべて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()
. 解決
Pyclipsサポートグループでいくつかの助けを受けました。解決策は、Python関数がCLIPS.SYMBOオブジェクトを返し、ルールのLHSの関数を評価するために使用する(TEST ...)を使用することです。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()
. 他のヒント
あなたの問題は(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()
>>>
.