我正在试验PyClips,并希望将其与Python紧密集成,这样当激活规则时,它就会调用python函数。
这是我到目前为止所知道的:
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 )
""")然而,当我断言‘动物是鸭子’的事实时,我的python函数没有被调用:
>>> clips.Assert("(animal-is duck)")
<Fact 'f-0': fact object at 0x7fe4cb323720>
>>> clips.Run()
0我做错了什么?
发布于 2012-01-26 20:56:34
有一个错误的括号太快地关闭了规则,遗漏了python-call
clips.Build("""
(defrule duck
(animal-is duck)
=>
(assert (sound-is quack))
(printout t "it's a duck" crlf))
(python-call addf 40 2 ) ^
""") ^ |
| this one
|
should go here如果您想验证addf是否实际返回42,可以将结果绑定并打印出来:
clips.Build("""
(defrule duck
(animal-is duck)
=>
(assert (sound-is quack))
(printout t \"it's 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 thttps://stackoverflow.com/questions/8973556
复制相似问题