Hom*_*lli 5 python expert-system clips pyclips
我正在试验PyClips,我想将其与 Python 紧密集成,以便当激活规则时,它会调用 python 函数。
\n\n这是我到目前为止所拥有的:
\n\nimport clips\n\ndef addf(a, b):\n return a + b\n\nclips.RegisterPythonFunction(addf)\n\nclips.Build("""\n(defrule duck\n (animal-is duck)\n =>\n (assert (sound-is quack))\n (printout t "it\xe2\x80\x99s a duck" crlf))\n (python-call addf 40 2 )\n""")\nRun Code Online (Sandbox Code Playgroud)\n\n然而,当我断言“动物是鸭子”这一事实时,我的 python 函数没有被调用:
\n\n>>> clips.Assert("(animal-is duck)")\n<Fact \'f-0\': fact object at 0x7fe4cb323720>\n>>> clips.Run()\n0\nRun Code Online (Sandbox Code Playgroud)\n\n我究竟做错了什么?
\n有一个放错位置的括号,过早地关闭了规则,遗漏了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
Run Code Online (Sandbox Code Playgroud)
如果你想验证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 t
Run Code Online (Sandbox Code Playgroud)