Drools - 积累逻辑

Drools - Accumulate logic

我需要帮助编写以下要求的累积逻辑:

要求:某些规则将提供应用于全局值的百分比。另一组规则应使用合计总百分比来确定结果。

例如:0.75 是作为输入(阈值)传递的全局值。 规则 1 可能适用固定值的 -10%,即。 0.75 - (0.75 * 0.10) = 0.675 规则 2 将适用 + 20% 的更新值。即,0.675 + (0.675 * 0.20) = 0.81

我的全局值为 0.75(阈值)

使用以下规则,我尝试应用适用于固定值的百分比:

//class 导入 全局双 FIXED_THRESHOLD;

//规则

rule "Prediction Rule_2"
lock-on-active true
no-loop true
salience (2)
when
    LossInput (airBagDeployed == 'Y' ,  driveable == 'N')
    result : RuleResult(predictedTotalsThreshold == 0)
then
    insert(new ControlFact( -10.0 ) ); //Reduce -10% to global value 0.75 - 0.75* 0.10  = 0.675
    System.err.println("New control fact added to working memory....");
end

rule "Prediction Rule_1"
lock-on-active true
no-loop true
salience (1)
when
    LossInput (airBagDeployed == 'Y' ,  driveable == 'N', make == 'Honda' )
    result : RuleResult(predictedTotalsThreshold == 0)
then
    insert(new ControlFact( 20.0 ) ); // Add 20% to the updated aggregate (0.20 % of 0.675).
    System.err.println("New control fact added to working memory....");
end

我尝试了下面的累加逻辑,但显然是错误的。它始终只适用于固定值而不是更新值。

rule "Aggregate All Threshold"
no-loop true
when
         $aggregateTotalsThresholdPercentage : Number() from accumulate(
                                               ControlFact( $totalsThreshold : totalsThresholdPercentage  ),
                                               sum( ( FIXED_THRESHOLD + ( FIXED_THRESHOLD * $totalsThreshold ) / 100  ) ) )

            ruleResult: RuleResult(predictedTotalsThreshold == 0)

then

        ruleResult.setPredictedTotalsThreshold($aggregateTotalsThresholdPercentage.doubleValue());
    update(ruleResult);    
 end

POJO:

public class LossInput{
    private String airBagDeployed;
    private String driveable;
    private String make;
}


public class ControlFact {
    public double totalsThresholdPercentage;

}

public class RuleResult {
   private double predictedTotalsThreshold;
}


//insert facts in working memory
        kieSession.insert(lossInput);
        kieSession.insert(ruleResult);
        kieSession.setGlobal("FIXED_THRESHOLD", new Double(0.75));
        kieSession.fireAllRules();

请帮助累积逻辑,以便在每次应用百分比阈值时应用更新的值。

您不能以这种方式使用 accumulate/sum,因为您为每个 ControlFact 添加了 FIXED_THRESHOLD

像在 "Prediction..." 规则中一样插入 ControlFacts(没有所有规则属性)。使用每个 ControlFact 来更新 RuleResult 的 predictedTotalsThreshold。 "Aggregate" 规则将重复触发,因此您需要确保收回使用的 ControlFact。

rule "Aggregate All Threshold"
when
  ControlFact( $ttp: totalsThresholdPercentage );
  $res: RuleResult( $ptt: predictedTotalsThreshold)
then
  double nt = $ptt + $ptt*$ttp/100;
  modify( $res ){ setPredictedTotalsThreshold( $nt ) }
  retract( $res );
end