如何在 CLIPS 中使用单个变量快速重申具有多个字段的事实

How to quickly reassert a fact with multiple fields using a single variable in CLIPS

假设我有这个模板:

(deftemplate TRIP::trip
   (multislot resort-sequence)
   (multislot place-sequence)
   (multislot days-distribution))

这条规则:

(defrule test
    ?p <- (trip (days-distribution $?days))
    =>
    ;change value of ?days

现在,由于 ?p 有 3 个字段,我想知道的是:是否可以重新声明事实 ?p 而不必分别绑定所有字段?

像这样: (assert (trip ?p (days-distribution $?days)))

编辑: 澄清, 从一个 trip-fact 我需要创建多个,所以我不能修改第一个

可以使用modify函数,但需要注意循环规则。您上面的规则一旦触发,将无限循环,因为新修改的 trip 事实会一遍又一遍地激活规则。

In [1]: (deftemplate trip 
      :   (multislot resort-sequence) 
      :   (multislot place-sequence) 
      :   (multislot days-distribution))                                                                                                                                                                    

In [2]: (defrule test 
      :   ?loop-prevention <- (new-trip) 
      :   ?p <- (trip (days-distribution $?days)) 
      :   => 
      :   (retract ?loop-prevention) 
      :   (modify ?p (days-distribution 1 2 3)))                                                                                                                                                  

In [3]: (assert (trip (resort-sequence a b c) (place-sequence d e f) (days-distribution 22 23 24)))                                                                                     
(trip (resort-sequence a b c) (place-sequence d e f) (days-distribution 22 23 24))

In [4]: (assert (new-trip))                                                                                                                                                                                 
(new-trip)

In [5]: (facts)                                                                                                                                                                                             
f-0     (initial-fact)
f-1     (trip (resort-sequence a b c) (place-sequence d e f) (days-distribution 22 23 24))
f-2     (new-trip)
For a total of 3 facts.

In [6]: (agenda)                                                                                                                                                                                            
0      test: f-2,f-1
For a total of 1 activation.

In [7]: (run)                                                                                                                                                                                               

In [8]: (facts)                                                                                                                                                                                             
f-0     (initial-fact)
f-3     (trip (resort-sequence a b c) (place-sequence d e f) (days-distribution 1 2 3))
For a total of 2 facts.