2016-10-26 86 views
0

予定義的類與用於槽的受限選擇:CLIPS:允許符號

(defclass TARGET (is-a USER) 
    (slot uuid 
     (type STRING)) 
    (slot function 
     (type SYMBOL) 
     (allowed-symbols a1 a2 b c d e f g)) 
) 

(make-instance target of TARGET 
    (uuid "a123") 
    (function zzz)  
) 

我預期CLIPS抱怨爲「ZZZ」(不允許),但它沒有。爲什麼?

此致敬禮。 Nicola

回答

2

約束檢查是靜態(解析期間)和動態(執行期間)完成的。默認情況下,只啓用靜態約束檢查。當調用消息傳遞時,實例的槽分配是動態完成的,因爲在執行消息處理程序期間可能會將非法值替換爲合法值。

在以下情況下,由於可以在運行時替換無效值,因此定義不會在定義時生成錯誤,但是由於對象模式直接獲取某個插槽的值而不使用消息傳遞。

CLIPS> (clear) 
CLIPS> 
(defclass TARGET (is-a USER) 
    (slot uuid 
     (type STRING)) 
    (slot function 
     (type SYMBOL) 
     (allowed-symbols a1 a2 b c d e f g))) 
CLIPS> 
(definstances static 
    (target1 of TARGET (uuid "a123") (function zzz))) 
CLIPS>  
(defrule static 
    (object (is-a TARGET) (function zzz)) 
    =>) 

[CSTRNCHK1] A literal restriction value found in CE #1 
does not match the allowed values for slot function. 

ERROR: 
(defrule MAIN::static 
    (object (is-a TARGET) 
      (function zzz)) 
    =>) 
CLIPS> (reset) 
CLIPS>   
(make-instance target2 of TARGET 
    (uuid "b456") 
    (function zzz)) 
[target2] 
CLIPS> 

如果啓用了動態約束檢查,你會看到在執行過程中的錯誤,當實例實際創建:

CLIPS> (set-dynamic-constraint-checking TRUE) 
FALSE 
CLIPS> 
(make-instance target3 of TARGET 
    (uuid "c789") 
    (function zzz)) 
[CSTRNCHK1] zzz for slot function of instance [target3] found in put-function primary in class TARGET 
does not match the allowed values. 
[PRCCODE4] Execution halted during the actions of message-handler put-function primary in class TARGET 
FALSE 
CLIPS> (reset) 
[CSTRNCHK1] zzz for slot function of instance [target1] found in put-function primary in class TARGET 
does not match the allowed values. 
[PRCCODE4] Execution halted during the actions of message-handler put-function primary in class TARGET 
CLIPS>