约束检查是静态(解析期间)和动态(执行期间)完成的。默认情况下,只启用静态约束检查。当调用消息传递时,实例的槽分配是动态完成的,因为在执行消息处理程序期间可能会将非法值替换为合法值。
在以下情况下,由于可以在运行时替换无效值,因此定义不会在定义时生成错误,但是由于对象模式直接获取某个插槽的值而不使用消息传递。
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>