我有一个单槽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>
答案 0 :(得分:0)
默认情况下启用静态约束检查(在解析期间发生)。动态约束检查(在代码执行期间发生)不是(参见“基本编程指南”的第11节)。如果启用它,您将在示例中遇到约束违规(尽管您需要使用X以外的槽值来断言事实 - 不允许重复事实,并且在执行期间,此检查将在约束检查之前发生)。 / p>
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>