문제

사용 PyClips,나를 구축 하려는 규칙에서 클립하는 동적으로 검색하는 데이터는 파이썬.이를 위해 등록 외부의 함수에서 설명 설명.

아래 코드는 장난감의 예는 문제입니다.나는 이 일을 하기 때문에 나는 응용 프로그램으로 큰 콘 데이터의 형태로,SQL 데이터베이스는 원하는 이유와 함께 사용하여 클립이 있습니다.그러나 나는 원하지 않는 시간을 낭비 변환 모든 데이터로 클립을 주장을 수 있다면,단순히"플러그인"클립에 직접 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 지원 그룹에 도움을 받았습니다.해결책은 파이썬 함수가 클립을 반환하고 규칙의 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()
.

다른 팁

문제 할 수있는 뭔가가와 (neq (python-call py_getvar user) 'None').분명히 클립을 좋아하지 않는다 중첩된 문입니다.그것은 나타납니다려 함수 호출에 평등 문은 나쁜 것들입니다.그러나지 않을 것 주장 가치를 어쨌든 당신의 기능을 반환하거나 없거나 값으로 설정합니다.대신 당신이 원하는 다음을 수행해야 합니다.

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()
>>>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top