CLIPS规则不匹配 [英] CLIPS rule dont match

查看:169
本文介绍了CLIPS规则不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Clips中的规则匹配有问题,特别是我不明白为什么该规则不起作用.

I have a problem with a rule match in Clips, in particular i can't understand why this rule doesn't actives.

(deffunction get-unknow-col (?col)
    (bind ?facts (length (find-all-facts ((?a a-cell)) (and (eq ?a:y ?col) (eq ?a:content unk)))))
    (return ?facts)
)

(deffunction get-boat-pieces-col (?col)
    (bind ?facts (length (find-all-facts ((?a a-cell)) (and (eq ?a:y ?col) (and (neq ?a:content unk) (neq ?a:content water))))))
    (return ?facts)
)

(defrule mark-remaining-unk-cells-col (declare (salience 40))
    (k-per-col (col ?y) (num ?num))
    (test (= (+ (get-unknow-col ?y) (get-boat-pieces-col ?y)) ?num))
=>
    (do-for-all-facts ((?cell a-cell)) (and (eq ?cell:y ?y) (eq ?cell:content unk))
        (modify ?cell (content boat-piece))
    )
)

但实际上,我有正确的值,实际上是在运行:

But in (facts) I have the correct values, in fact running:

(k-per-col (col 9) (num 1))

(get-unknow-col 9)
1
(get-boat-pieces-col 9)
0
CLIPS> (= (+ (get-unknow-col 9) (get-boat-pieces-col 9)) 1)
TRUE

该规则仅在num为0(正确)时有效:

The rule instead works only if num is 0 (correctly):

FIRE   75 mark-remaining-unk-cells-col: f-137
***** Y:8 num: 0 get-unknown-col: 0 get-boat-pieces-col 0
FIRE   76 mark-remaining-unk-cells-col: f-136
***** Y:7 num: 0 get-unknown-col: 0 get-boat-pieces-col 0

为什么当num = 1,get-unknow-col = 1,get-boat-pieces-col = 0且测试为true时它不激活?我哪里错了?

Why it doesnt activate when num=1, get-unknow-col=1, get-boat-pieces-col=0 and the test is true? Where i'm wrong?

推荐答案

这是《 CLIPS基本编程指南》第5.4.2节测试条件元素"中的相关行为描述:

This is the relevant behavior description from the CLIPS Basic Programming Guide, Section 5.4.2 Test Conditional Element:

当所有进行中的CE都满足时,将对测试CE进行评估.这 表示如果 可以通过不止一种模式来满足进行中的CE 实体.为了引起对测试CE的重新评估,一种模式 必须先更改与CE匹配的CE实体.

A test CE is evaluated when all proceeding CEs are satisfied. This means that a test CE will be evaluated more than once if the proceeding CEs can be satisfied by more than one group of pattern entities. In order to cause the reevaluation of a test CE, a pattern entity matching a CE prior to the test CE must be changed.

当确定每个词的事实时,将对测试CE进行评估.如果在断言任何或所有a-cell事实之前发生这种情况,那么您将不会获得与先断言所有a-cell事实然后断言k-col事实一样的结果.

When the k-per-col fact is asserted the test CE will be evaluated. If this occurs before any or all a-cell facts have been asserted, then you will not get the same results as if you had asserted all of the a-cell facts first and then the k-per-col fact.

为了确保可预测的行为,由测试CE评估的表达式应始终为一组特定的参数返回相同的值.在这种情况下,即使参数?col与先前的调用相同,get-unknow-col和get-boat-pieces-col函数也可以返回不同的值.

In order to insure predictable behavior, expressions evaluated by the test CE should always return the same value for a specific set of arguments. In this case, the get-unknow-col and get-boat-pieces-col functions can return different values even if the parameter ?col is the same as a previous call.

某些基于规则的语言提供了收集"条件元素,该条件元素可以轻松地计算与模式匹配的事实的数量,但是不幸的是,CLIPS并未提供此条件.为了添加此功能,您需要创建事实和规则来跟踪感兴趣的值.

Some rule-based language provide a "collect" conditional element which allows to easily count the number of facts matching a pattern, but unfortunately CLIPS does not provide this. In order to add this functionality, you'd need to create facts and rules which track the values of interest.

         CLIPS (6.31 6/12/19)
CLIPS> 
(deftemplate a-cell
   (slot id (default-dynamic (gensym*))) 
   (slot x)
   (slot y)
   (slot content (allowed-values water left right middle top bot sub unk)))
CLIPS> 
(deftemplate track-a-cell
   (slot x (default any))
   (slot y (default any))
   (slot content (default any))
   (multislot matches))
CLIPS>    
(deffacts trackers
   (track-a-cell (x any) (y 9) (content unk)))
CLIPS>    
(defrule add-a-cell-match
   (declare (salience 10))
   ?t <- (track-a-cell (x ?x1) (y ?y1) (content ?c1) (matches $?m))
   (a-cell (id ?id) (x ?x2) (y ?y2) (content ?c2))
   (test (and (not (member$ ?id ?m))
              (or (eq ?x1 any) (eq ?x1 ?x2))
              (or (eq ?y1 any) (eq ?y1 ?y2))
              (or (eq ?c1 any) (eq ?c1 ?c2))))
   =>
   (modify ?t (matches ?m ?id)))
CLIPS>    
(defrule remove-a-cell-match
   (declare (salience 10))
   ?t <- (track-a-cell (x ?x1) (y ?y1) (content ?c1) (matches $?b ?id $?e))
   (not (and (a-cell (id ?id) (x ?x2) (y ?y2) (content ?c2))
             (test (and (or (eq ?x1 any) (eq ?x1 ?x2))
                            (or (eq ?y1 any) (eq ?y1 ?y2))
                            (or (eq ?c1 any) (eq ?c1 ?c2))))))
   =>
   (modify ?t (matches ?b ?e)))
CLIPS> 
(deffacts init
   (a-cell (x 0) (y 9) (content water))
   (a-cell (x 1) (y 9) (content unk))
   (a-cell (x 2) (y 9) (content water))
   (a-cell (x 3) (y 9) (content water))
   (a-cell (x 5) (y 9) (content water))
   (a-cell (x 6) (y 9) (content unk))
   (a-cell (x 7) (y 9) (content water))
   (a-cell (x 8) (y 9) (content water))
   (a-cell (x 9) (y 9) (content water))
   (a-cell (x 4) (y 9) (content unk)))
CLIPS> (reset)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-2     (a-cell (id gen1) (x 0) (y 9) (content water))
f-3     (a-cell (id gen2) (x 1) (y 9) (content unk))
f-4     (a-cell (id gen3) (x 2) (y 9) (content water))
f-5     (a-cell (id gen4) (x 3) (y 9) (content water))
f-6     (a-cell (id gen5) (x 5) (y 9) (content water))
f-7     (a-cell (id gen6) (x 6) (y 9) (content unk))
f-8     (a-cell (id gen7) (x 7) (y 9) (content water))
f-9     (a-cell (id gen8) (x 8) (y 9) (content water))
f-10    (a-cell (id gen9) (x 9) (y 9) (content water))
f-11    (a-cell (id gen10) (x 4) (y 9) (content unk))
f-14    (track-a-cell (x any) (y 9) (content unk) (matches gen10 gen6 gen2))
For a total of 12 facts.
CLIPS> (retract 3 7)
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-2     (a-cell (id gen1) (x 0) (y 9) (content water))
f-4     (a-cell (id gen3) (x 2) (y 9) (content water))
f-5     (a-cell (id gen4) (x 3) (y 9) (content water))
f-6     (a-cell (id gen5) (x 5) (y 9) (content water))
f-8     (a-cell (id gen7) (x 7) (y 9) (content water))
f-9     (a-cell (id gen8) (x 8) (y 9) (content water))
f-10    (a-cell (id gen9) (x 9) (y 9) (content water))
f-11    (a-cell (id gen10) (x 4) (y 9) (content unk))
f-16    (track-a-cell (x any) (y 9) (content unk) (matches gen10))
For a total of 10 facts.
CLIPS> (assert (a-cell (x 1) (y 9) (content unk)))
<Fact-17>
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-2     (a-cell (id gen1) (x 0) (y 9) (content water))
f-4     (a-cell (id gen3) (x 2) (y 9) (content water))
f-5     (a-cell (id gen4) (x 3) (y 9) (content water))
f-6     (a-cell (id gen5) (x 5) (y 9) (content water))
f-8     (a-cell (id gen7) (x 7) (y 9) (content water))
f-9     (a-cell (id gen8) (x 8) (y 9) (content water))
f-10    (a-cell (id gen9) (x 9) (y 9) (content water))
f-11    (a-cell (id gen10) (x 4) (y 9) (content unk))
f-17    (a-cell (id gen11) (x 1) (y 9) (content unk))
f-18    (track-a-cell (x any) (y 9) (content unk) (matches gen10 gen11))
For a total of 11 facts.
CLIPS> 

这篇关于CLIPS规则不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆