How to get rule activation for calling a python function using PyClips

I am experimenting with PyClips , and I want to integrate it with Python, so when the rule is activated, it calls python.

Here is what I still have:

import clips def addf(a, b): return a + b clips.RegisterPythonFunction(addf) clips.Build(""" (defrule duck (animal-is duck) => (assert (sound-is quack)) (printout t "it's a duck" crlf)) (python-call addf 40 2 ) """) 

However, when I state that "the animal is a duck", my python function is NOT called:

 >>> clips.Assert("(animal-is duck)") <Fact 'f-0': fact object at 0x7fe4cb323720> >>> clips.Run() 0 

What am I doing wrong?

+4
source share
1 answer

There's an inappropriate bracket that closes the rule too fast, leaving python-call :

 clips.Build(""" (defrule duck (animal-is duck) => (assert (sound-is quack)) (printout t "it a duck" crlf)) (python-call addf 40 2 ) ^ """) ^ | | this one | should go here 

If you want to make sure addf really returned 42, the result can be bound and printed:

 clips.Build(""" (defrule duck (animal-is duck) => (assert (sound-is quack)) (printout t \"it a duck\" crlf) (bind ?tot (python-call addf 40 2 )) (printout t ?tot crlf)) """) clips.Assert("(animal-is duck)") clips.Run() t = clips.StdoutStream.Read() print t 
+2
source

Source: https://habr.com/ru/post/1392482/


All Articles