[python-nl] Problem passing instance (self) when calling CLIPS
Dirkjan Ochtman
dirkjan at ochtman.nl
Fri Dec 25 14:46:58 CET 2009
On Fri, Dec 25, 2009 at 12:33, Schneider <f.schneider at de-bleek.demon.nl> wrote:
> import clips
>
>
>
> class Callable(object):
>
> def __init__(self, func):
>
> self._func = func
>
> clips.RegisterPythonFunction(func)
>
> r = clips.BuildRule("%s-rule" % func.__name__,
>
> "?f <- (duck)",
>
> ""(retract ?f)
>
> (python-call %s \"arg2py\")""" % func.__name__,
>
> "The %s rule" % func.__name__)
>
> print r.PPForm()
>
> print "%s being initialized" % self._func.__name__
>
>
>
> def __call__(self, *args, **kwargs):
>
> print "I am never executed"
>
> return self._func(*args, **kwargs)
>
>
>
> class MyObject(object):
>
>
>
> @Callable
>
> def MyMethod(self):
>
> print "I am being called by CLIPS with the arg", self
>
>
>
> def Assert(self):
>
> self.data = "DATA"
>
> clips.Assert("(duck)")
>
>
>
> if __name__ == "__main__":
>
> clips.Reset()
>
> myObject = MyObject()
>
> myObject.Assert()
>
> clips.Run(100)
>
> print "Bye"
>
>
>
> The output looks like:
>
>
>
> (defrule MAIN::MyMethod-rule "The MyMethod rule"
>
> ?f <- (duck)
>
> =>
>
> (retract ?f)
>
> (python-call MyMethod "arg2py"))
>
>
>
> MyMethod being initialized
>
> I am being called by CLIPS with the arg arg2py
>
> Bye
>
>
>
> Please note that the __call__ method in the decorator is not being called
> from CLIPS.
That's because you aren't calling any MyMethod here?
> I don’t understand the internals but assume that calling a Python function
> from a C program will not invoke __call__.
I think "calling", which you can override in pure Python by defining a
__call__ method, uses a call hook on the C-registered type. IOW, just
attaching a method called __call__ to your C-defined class probably
won't work, you need to attach it to the call hook on the C-level
struct.
Hope that helps (I haven't actually worked much with the C API myself),
Dirkjan
More information about the Python-nl
mailing list