CLIPS:从函数内部断言事实不遵守模板约束

CLIPS: asserting facts from within a function does not respect template constraints

我有一个单槽 deftemplate 定义,它对允许的符号有限制。如果我直接从顶层断言一个事实,约束将按预期工作(即我只能使用允许的符号之一)。但是,如果我在 deffunction 中执行此操作,则约束实际上不存在(请参阅下面的代码输出)。如何在我的函数中强制执行约束?

CLIPS> (clear)
CLIPS> (deftemplate test-template (slot myslot (type SYMBOL) (allowed-symbols A B C)))
CLIPS> (deffunction test-function (?s) (assert (test-template (myslot ?s))))
CLIPS> (assert (test-template (myslot X)))

[CSTRNCHK1] A literal slot value found in the assert command
does not match the allowed values for slot myslot.
CLIPS> (test-function X)
<Fact-1>
CLIPS> (facts)
f-0     (initial-fact)
f-1     (test-template (myslot X))
For a total of 2 facts.
CLIPS> 

默认启用静态约束检查(在解析期间发生)。动态约束检查(在代码执行期间发生)不是(请参阅基本编程指南的第 11 节)。如果您启用它,您将在您的示例中遇到约束冲突(尽管您需要使用 X 以外的槽值来断言一个事实——不允许重复的事实,并且在执行期间,此检查将在约束检查之前发生)。

CLIPS> (clear)
CLIPS> (deftemplate test-template (slot myslot (type SYMBOL) (allowed-symbols A B C)))
CLIPS> (deffunction test-function (?s) (assert (test-template (myslot ?s))))
CLIPS> (assert (test-template (myslot X)))

[CSTRNCHK1] A literal slot value found in the assert command
does not match the allowed values for slot myslot.
CLIPS> (test-function Y)
<Fact-1>
CLIPS> (set-dynamic-constraint-checking TRUE)
FALSE
CLIPS> (test-function Z)

[CSTRNCHK1] Slot value Z found in fact f-2     
does not match the allowed values for slot myslot.
[PRCCODE4] Execution halted during the actions of deffunction test-function.
<Fact-2>
CLIPS>