【问题标题】:How to modify local variables in a rule? CLIPS Production System如何修改规则中的局部变量? CLIPS制作系统
【发布时间】:2020-05-24 10:16:58
【问题描述】:

我是 CLIPS 的新手,如果能对我的问题有任何想法,我将不胜感激。总而言之,我有一个模板,它用 sudoku 等数值绘制 3x3,每个值 ?v 都有其行 ?f1、列 ?c1 和一个称为“状态”的字符串类型的特征,该特征可以是活动的或非活动的。

(deftemplate box
  (slot row)
  (slot column)
  (slot value)
  (slot state
  (allowed-values active inactive)
  (default inactive)))

(deffacts initial-values
  (box (row 1) (column 1) (value 1))
  (box (row 1) (column 2) (value 1))
  (box (row 1) (column 3) (value 1))
  (box (row 2) (column 1) (value 2))
  (box (row 2) (column 2) (value 3))
  (box (row 2) (column 3) (value 1))
  (box (row 3) (column 1) (value 2))
  (box (row 3) (column 2) (value 3))
  (box (row 3) (column 3) (value 56)))

我写了一个规则来验证是否有一个值在同一行和列中都没有重复。如果此条件为 TRUE,我想将状态更改为活动状态,因为默认情况下所有值都处于非活动状态,我已查看语法但无法处理这些变量。

 (defrule uniqueness
   (exists (box (row ?f1) (column ?c1) (value ?v)(state inactive))
           (not (and (box (row ?f2) (column ?c2) (value ?v))
                     (test (or (!= ?f1 ?f2) (!= ?c1 ?c2))))))

   =>
   (printout t "There are values that are not repeated" crlf)
   ;;(modify (state active)) ;;this line causes me problems
   )

【问题讨论】:

    标签: clips expert-system


    【解决方案1】:

    exists 条件元素只匹配一次,而不管它包含多少匹配项,因此在 exists 范围之外,引用绑定在其中的事实和变量是没有意义的。如果您想大致了解至少一个事实是否与一组条件匹配,则使用 exists,但如果您需要对特定事实执行操作,则不要使用它。

    要解决您的问题,请删除存在,然后将匹配框模式的事实绑定到一个变量,以便您可以将该变量与修改命令一起使用:

    (defrule uniqueness
       ?b <- (box (row ?f1) (column ?c1) (value ?v) (state inactive))
       (not (and (box (row ?f2) (column ?c2) (value ?v))
                 (test (or (!= ?f1 ?f2) (!= ?c1 ?c2)))))
    
       =>
       (printout t "There are values that are not repeated" crlf)
       (modify ?b (state active)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 2021-02-05
      • 1970-01-01
      • 2017-03-25
      • 2017-07-16
      • 1970-01-01
      • 2013-07-17
      相关资源
      最近更新 更多