CLIPS:允许符号

时间:2016-10-26 09:32:09

标签: symbols 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”(不允许),但事实并非如此。为什么呢?

祝你好运。 尼古拉

1 个答案:

答案 0 :(得分:2)

约束检查是静态地(在解析期间)和动态地(在执行期间)完成的。默认情况下,仅启用静态约束检查。实例的插槽分配是在调用消息时动态完成的,因为在执行消息处理程序期间,非法值可能会被替换为合法值。

在下面的情况中,definstances在定义时不会生成错误,因为无效值可以在运行时替换,但是defrule确实会生成错误,因为对象模式直接获取槽的值而不使用消息合格。

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>