0

在这里阅读了如何在剪辑规则的 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变量有值。

如果可能的话,我想避免创建多个规则并使用“控制事实”。

4

1 回答 1

2

不同的规则引擎有不同的方法来指定何时完成对象更改,但一般来说,当您对一个对象进行多次不同的更改时,每次更改都会调用一次模式匹配过程。您在问题中包含了一个代码片段,而不是可重现的示例,所以我只能猜测您的问题的原因,但您所做的可能是创建对象,然后在单独的步骤中对其进行修改。创建对象时,模式匹配会延迟,直到处理完所有插槽覆盖,类似地,在进行对象修改时,您可以使用 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>
于 2015-09-25T18:32:48.147 回答