2017-07-26 44 views
0

我想用在LHS約束變量來聲明一個規則的顯着性,以便與事實數據庫中定義的更嚴格的時間限制,優先規則。我想下面應該工作:在顯着性聲明中使用變量在規則定義

(set-salience-evaluation when-activated) 
(deffunction testsal (?a ?b) (integer (+ ?a ?b))) 
(defrule testr 
    (declare (salience (testsal ?a 4))) 
    (sal ?a) 
    ?tf <- (fire testr) 
    => 
    (printout t "Running testr") 
    (retract ?tf) 
) 
(assert (sal 3)) 
(assert (fire testr)) 

但這種失敗與錯誤:

[EVALUATN1] Variable a is unbound 
[PRCCODE6] This error occurred while evaluating arguments for the deffunction testsal. 

[PRNTUTIL8] This error occurred while evaluating the salience for defrule testr. 

ERROR: 
(defrule MAIN::testr 
    (declare (salience (testsal ?a 4) 

是否有使用綁定在LHS在規則的顯着性聲明一個變量的方法嗎?

如果不是,根據事實庫中的一些事實確定優先級的常用方法是什麼?請注意,我不想禁止規則觸發,我只想優先考慮其他規則,所以只需向LHS添加約束條件可能無效。

回答

0

使用全局變量,而不是一個事實:

CLIPS> (set-salience-evaluation when-activated) 
when-activated 
CLIPS> (defglobal ?*sal-a* = 0) 
CLIPS> 
(defrule testr 
    (declare (salience (+ ?*sal-a* 4))) 
    ?tf <- (fire testr) 
    => 
    (printout t "Running testr") 
    (retract ?tf)) 
CLIPS> (bind ?*sal-a* 3) 
3 
CLIPS> (assert (fire testr)) 
<Fact-1> 
CLIPS> (agenda) 
7  testr: f-1 
For a total of 1 activation. 
CLIPS> 
+0

但我不能做的顯着性取決於規則的接地,可以嗎?我的目標是制定一個可以通過兩個不同的基礎來激活的規則,並且我想優先考慮一個。 – morxa

+0

您可以在模式中綁定全局值:(sal?a&:(bind?* sal-a *?a))。但是,爲了使其正常工作,每個規則都必須有自己的全局變量。 –