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

How to combine two rules into one?

我想知道如何组合我的两个规则,例如:

(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 语句一样,在满足任何条件时触发。

天真的答案是

(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")
)

第一个障碍是,如果此人有红色裤子 蓝色衬衫,则此规则会触发两次。这很可能并不重要,因为此人的身份未被识别,因此我们可以假设只有一个人在走秀。

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