PyCLIPS を使用していると仮定します。
import clips
def clips_callable(f):
def wf(*args, **kwargs):
if f(*args, **kwargs):
return clips.Symbol("TRUE")
else:
return clips.Symbol("FALSE")
clips.RegisterPythonFunction(wf, f.__name__)
@clips_callable
def pyprint(s):
print s
print "".join(map(str, s))
clips.Load("test.clp")
clips.Reset()
clips.Run()
# assert a fact.
a = clips.Assert("(order (part-id p1) (quantity 20))")
clips.Run()
test.clp
次のようになります。
(deffunction MAIN::print ($?value)
(python-call pyprint ?value)
; (printout t ?value)
)
(deftemplate MAIN::order
(slot part-id)
(slot quantity)
)
おまけとしてデコレータを含めた@clips_callable
ので、クリップから Python 関数を簡単に呼び出すことができます。