簡體   English   中英

CLIPS規則的LHS中的Python函數被多次調用-如何等待變量包含值?

[英]Python function in LHS of CLIPS rule called multiple times - how to wait for a variable to contain a value?

在這里閱讀如何在clips規則的lhs上調用python函數。

現在,我有以下規則:

(defrule python_func_lhs "comment me"
    (object (is-a clips_TEST_CLASS) (some_slot ?some_slot_value))
    (test (eq (python-call python_print (str-cat "some_slot: " ?some_slot_value)) TRUE))
    =>
    ;(assert (do_something))
)

我的問題是python函數被調用了兩次,第一次打印

some_slot:無

接着

some_slot:some_slot_value

似乎包含python函數的第二個規則部分不會“等待”要匹配的LHS規則的第一部分。

一旦LHS規則的第一部分匹配,如何使剪輯僅調用python函數一次? 換句話說,我想等到?some_slot_value變量具有值。

如果可能的話,我想避免創建多個規則並使用“控制事實”。

不同的規則引擎具有不同的方法來指定何時完成對象更改,但是通常來說,當您對一個對象進行多個不同的更改時,模式匹配過程將為每個更改調用一次。 您在問題中包括了代碼片段,而不是可復制的示例,因此我只能猜測問題的原因,但是可能您正在做的是創建對象,然后在一個單獨的步驟中對其進行修改。 創建對象時,模式匹配會延遲到處理完所有插槽替代之后,並且類似地,在進行對象修改時,您可以使用Modify-instance函數將一組插槽更改集合為一個更改。 如果需要在多個函數調用之間延遲模式匹配,則可以使用pattern-match-delay函數。

CLIPS> 
(defclass clips_TEST_CLASS
   (is-a USER)
   (slot some_slot))
CLIPS>    
(definstances initial
   (i1 of clips_TEST_CLASS (some_slot some_slot_value)))   
CLIPS> 
(deffunction pcall (?v) (printout t ?v crlf) TRUE)
CLIPS> 
(defrule python_func_lhs 
    (object (is-a clips_TEST_CLASS) (some_slot ?some_slot_value))
    (test (pcall (str-cat "some_slot: " ?some_slot_value)))
    =>)
CLIPS> (reset)
some_slot: some_slot_value
CLIPS> (make-instance i2 of clips_TEST_CLASS (some_slot some_other_value))
some_slot: some_other_value
[i2]
CLIPS> (make-instance i3 of clips_TEST_CLASS)
some_slot: nil
[i3]
CLIPS> (send [i3] put-some_slot another_value)
some_slot: another_value
another_value
CLIPS> 
(object-pattern-match-delay
   (make-instance i4 of clips_TEST_CLASS)
   (send [i4] put-some_slot still_another_value))
some_slot: still_another_value
still_another_value
CLIPS> (modify-instance [i4] (some_slot value))
some_slot: value
TRUE
CLIPS>

暫無
暫無

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

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