CLIPS:测试正确的事实

CLIPS: testing correct facts

我有一系列规则和一组初始 (assert) 事实。现在,我想添加这些事实,运行 规则,然后应用另一组规则来检查 (run) 之后的当前现有事实是否包含正确的事实,而不再触发以前的规则并且不破坏当前的事实。然后,我想继续应用新事实,运行宁新规则,测试新插入的事实等。

我该怎么做?我的测试(批处理)文件是这样的:

(clear) ; just in case
(load constructs.clp) ; All loaded in the MAIN module.

(assert (blabla))
(assert (blabla2))
(run)

;; Code (rules/functions... I'm still wondering how to do it) to check
;; current facts

(assert (blabla3))
(assert (blabla4))
(run)

;; More tests.

(exit)

我试图为每个 deftemplate T 创建一个具有相同插槽的 deftemplate T-copy 并且它们应用 (assert (testing)) 事实来首先制作副本。然后我触发一组具有测试目的和更高显着性的规则 "halts" (run) 执行完成后,以避免触发先前的规则(我正在测试的规则)。这种方法的问题,除了需要太多的步骤之外,是我不知道原始规则的显着性,所以我不能确定测试规则会有更高的优先级。

我知道 defmodule 构造和焦点堆栈,但我还没有理解它们。如果我的猜测是正确的,我想我可以将我所有的测试规则放在一个特定的模块中,并将焦点放在该模块上以避免执行任何 MAIN 规则。如果有什么问题,我会 (halt) 在其中一个测试规则或 (exit) 批处理脚本中执行。如果一切正确,我弹出测试模块返回 MAIN,添加更多断言,再次 (run),然后他们用新测试再次推送测试模块以查看一切是否仍然正确。

但我不确定我的假设是否正确我想看一个示例,说明我应该如何进行测试。

PD:此外,我的 CLIPS 版本不支持事实集查询。

这里的总体思路是将核心规则组与执行测试的规则分离到单独的模块中,然后使用 focus 命令执行测试规则。

CLIPS> (defmodule MAIN (export ?ALL))
CLIPS> (deftemplate MAIN::point (slot x) (slot y))
CLIPS> 
(defrule MAIN::r1
   =>
   (assert (point (x 1) (y 2)))
   (assert (point (x 1) (y 5))))
CLIPS> (defmodule TESTING (import MAIN ?ALL))
CLIPS> 
(defrule TESTING::horizontal
   (point (x ?x1) (y ?y))
   (point (x ?x2&:(< ?x2 ?x1)) (y ?y))
   =>
   (printout t "Horizonal issue " ?y crlf))
CLIPS> (reset)
CLIPS> (agenda)
0      r1: *
For a total of 1 activation.
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (point (x 1) (y 2))
f-2     (point (x 1) (y 5))
For a total of 3 facts.
CLIPS> (focus TESTING)
TRUE
CLIPS> (agenda)
CLIPS> (run)
CLIPS> 
(defrule MAIN::r2
   =>
   (assert (point (x 3) (y 2))))
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (point (x 1) (y 2))
f-2     (point (x 1) (y 5))
f-3     (point (x 3) (y 2))
For a total of 4 facts.
CLIPS> (focus TESTING)
TRUE
CLIPS> (run)
Horizonal issue 2
CLIPS> 
(defrule TESTING::vertical
   (point (x ?x) (y ?y1))
   (point (x ?x) (y ?y2&:(< ?y2 ?y1)))
   =>
   (printout t "Vertical issue " ?x crlf))
CLIPS> (focus TESTING)
TRUE
CLIPS> (agenda)
0      vertical: f-2,f-1
For a total of 1 activation.
CLIPS> (run)
Vertical issue 1
CLIPS>