Drools:在多次执行后停止触发规则

Drools : stop a rule from firing after a number of executions

我正在尝试弄清楚如何在多次执行后阻止规则触发。 假设您购买 X 件商品(当前价格为 10 美元)可享受折扣,但每位客户只能享受此折扣 2 次。

所以我的规则如下:

 rule "Fixed Price Promotion Item X"

 when
     $o : Order( )
     $ol1 : OrderLine( item.code == "X" )
 then
    FixedPrice fixedPrice = new FixedPrice(8.80, $ol1.getItem().getPrice().doubleValue(), $ol1.getItem().getBarcode(), 1, "e78fbca5ed014f49806ad667aea80965" , "Happy Mother's day!! This is a fixed price promotion" );
    insertLogical (fixedPrice); //here I grant a promotion

最初,我想有另一个规则可以在满足我的条件时插入一个事实,这样它就可以阻止我的升级规则触发。

我会有这样的东西:

declare LimitReachedOut
   promotionId : String
end

rule "stop Promotion"
when
     Number($numOfGrantedProm : intValue) from accumulate ( 
                                FixedPrice(promotionId == "123",
                                        $count: quantity),
                                sum($count)
            )
then
    if( $numOfGrantedProm == 2  ){ //this cause an issue, even > 1 or >=2 will keep inserting new LimitReachedOut("123") recursively.
        insertLogical (new LimitReachedOut("123"));
     }
end  


rule "Fixed Price Promotion Item X"
 when
     not( LimitReachedOut( promotionId == "123" ) )
     $o : Order( )
     $ol1 : OrderLine( item.code == "X" ) 
 then
    FixedPrice fixedPrice = new FixedPrice(8.80, $ol1.getItem().getPrice().doubleValue(), $ol1.getItem().getBarcode(), 1, "123" , "Happy Mother's day!! This is a fixed price promotion" );
    insertLogical (fixedPrice);

 end

还有其他方法吗?

非常感谢您的评论。

我找到了一种方法来完成我想要的,如果有人也想使用它,我会把它张贴在这里。 我基本上不执行规则的 RHS,在那里放置一个条件子句。

首先,我使用 accumulate 计算插入到工作内存中的事实数(FixedPrice 的 id 为 link 此特定规则的事实,称为 ruleId)函数。 其次,RHS if( $numOfGrantedProm < 2 ) 中的条件意味着我将执行条件 if (create & insert fixedPrice fact) 内的块最多两次。 无论如何都会触发该规则,但 if 条件将避免执行代码块。

rule "Fixed Price Promotion Item X"

when
   $o : Order( )
   $ol1 : OrderLine( item.code == "X" )
   Number($numOfGrantedProm : intValue) from accumulate ( 
                                           FixedPrice(ruleId == 
                           "e78fbca5ed014f49806ad667aea80965",$count:quantity),
                           sum($count)
                      )
then
   if( $numOfGrantedProm < 2  ){ 
       FixedPrice fixedPrice = new FixedPrice(8.80,
                                   $ol1.getItem().getPrice().doubleValue(),
                                   $ol1.getItem().getBarcode(), 
                                   1, 
                                   "e78fbca5ed014f49806ad667aea80965" , 
                                   "Happy Mother's day!! This is a fixed price 
                                    promotion" );
       insertLogical (fixedPrice); //here I grant a promotion enter code here
    }
end