簡體   English   中英

如何使用PyClips激活規則以調用python函數

[英]How to get a rule activation to call a python function, using PyClips

我正在嘗試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

我究竟做錯了什么?

有一個放錯位置的括號,太早關閉了規則,而python-callpython-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 t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM