0

一段时间以来,我一直在努力解决这个问题,但没有找到关于如何完成我将要说明的内容的参考。假设我有一个单元格网格,每个单元格对应于以下模板:

(deftemplate cell 
    (slot x)
    (slot y)
    (slot type (allowed-values urban rural lake hill gate border))
)

现在我的网格中的单元格类型是用(assert (cell (x <x_coord>) (y <y_coord>) (type <some_type>))语句随机生成的,我想定义以下规则,它检查 3x3 范围内的所有单元格,以中心单元格为中心,并根据检查的单元格类型采取行动:

(defrule inspect
    (cell (x ?xval) (y ?yval))
    ; ...
=>
    (loop-for-count (?i -1 1) do
        (loop-for-count (?j -1 1) do
            ; get "type" of cell @ coordinates (- ?x ?i ), (+ ?y ?j)
            ; do stuff according to type (i.e. assert other facts)
        )
    )
)

我将如何在 CLIPS 规则的 RHS 上查找特定标准(在本例中为单元格坐标)的事实?我知道如何在 LHS 上进行模式匹配,但我很想知道是否也可以在 RHS 上这样做。提前致谢。

4

1 回答 1

2

使用事实集查询函数(基本编程指南中的第 12.9.12 节):

CLIPS> 
(deftemplate cell 
   (slot x)
   (slot y)
   (slot type (allowed-values urban rural lake hill gate border)))
CLIPS> 
(deftemplate inspect
   (slot x)
   (slot y))
CLIPS> 
(deffacts example
   (inspect (x 3) (y 3))
   (cell (type urban) (x 1) (y 1))
   (cell (type rural) (x 2) (y 3))
   (cell (type lake) (x 4) (y 4))
   (cell (type border) (x 4) (y 4))
   (cell (type hill) (x 3) (y 5))
   (cell (type gate) (x 3) (y 3)))
CLIPS> 
(defrule inspect
   ; Changed to inspect so the example does
   ; not fire this rule for every cell
   (inspect (x ?xval) (y ?yval))  
   =>
   (do-for-all-facts ((?c cell))
                     (and (<= (- ?xval 1) ?c:x (+ ?xval 1))
                          (<= (- ?yval 1) ?c:y (+ ?yval 1)))
      (printout t ?c:type " " ?c:x " " ?c:y crlf)))
CLIPS> (reset)
CLIPS> (run)
rural 2 3
lake 4 4
border 4 4
gate 3 3
CLIPS>
于 2014-02-02T17:27:48.400 回答