[python-nl] Problem passing instance (self) when calling CLIPS
Schneider
f.schneider at de-bleek.demon.nl
Fri Dec 25 12:33:04 CET 2009
Dear all,
I am building an app with Python 2.5 and the pyCLIPS 1.0.7 module. CLIPS is
a expert system coded in C.
I have a class which holds some data and has a method Assert which asserts a
fact in CLIPS. CLIPS processes the fact using a rule and calls the second
method MyMethod. This second method now should do something with the data.
In my program, the class could be some wxFrame, the Assert is a wxMenu event
handler and the MyMethod method could be some code to change the frame's
statusbar. So, on returning from CLIPS. I need the calling instance as an
argument so I can access the statusbar or whatever.
I use a decorator to register the python function in CLIPS and the rule
which should call the method. Initializing works fine, but I cannot pass
self to CLIPS when doing the assert and later return that value since CLIPS
cannot handle such data.
This is the simplified code and it's output.
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.
I don't understand the internals but assume that calling a Python function
from a C program will not invoke __call__.
Could somebody give me a hint on how to pass an argument like an integer or
string to CLIPS and use that value on retuning to fetch the instanc. Should
I look into weak references? Any other suggestions?
Alvast bedankt en een prettige kerst
Frans
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-nl/attachments/20091225/5d4eab66/attachment-0001.htm>
More information about the Python-nl
mailing list