Python函数在LHS中的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规则的第一部分匹配。

如何使剪辑只调用python函数一次,一旦LHS规则的第一部分被匹配?换句话说,我想等到?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>

最新更新