如何将两个规则合二为一?

时间:2015-04-23 16:34:48

标签: jess

我想知道如何结合我的两条规则,例如:

(defrule Rules::pants
  (declare (auto-focus TRUE))
(answer (ident color) (text red))
  (answer (ident pants) (text yes))
  =>
(printout t "you are wearing red pants"))

(defrule Rules::shirt
  (declare (auto-focus TRUE))
(answer (ident shirt) (text blue))
  (answer (ident red) (text yes))
  =>
(printout t "you are wearing blue shirt"))

如果我写下这两条规则:

(defrule Rules::pants
  (declare (auto-focus TRUE))
(answer (ident red) (text yes))
  (answer (ident pants) (text yes))
(answer (ident shirt) (text yes))
  (answer (ident blue) (text yes))
  =>
(printout t "you are wearing blue shirt and red pants"))

我希望它像OR语句一样,在满足任何条件时触发。

1 个答案:

答案 0 :(得分:0)

天真的答案是

(defrule Rules::pants      
  (or (and (answer (ident red) (text yes))
           (answer (ident pants) (text yes)))
      (and (answer (ident shirt) (text yes))
           (answer (ident blue) (text yes)))
  )
 =>
   (printout t "you are wearing blue shirt or red pants")
)

第一个障碍是如果此人有红色裤子蓝色衬衫,则此规则会触发两次。最有可能的是,这并不重要,因为这个人没有被识别出来,所以我们可以假设只有一个人在T台上。

已编辑如果属性彼此不关联,即当颜色和项目可以自由组合时,则会发生第二次阻碍。考虑一个蓝色牛仔裤和红色格仔衬衫的乡下人。这条规则会被解雇,因为有"裤子"和"衬衫"和"红色"和"蓝色",足以根据模式进行匹配。但OP在评论中断言(见下文),有一些方法可以避免这种情况。