2016-10-26 58 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>