How to achieve below objective in drools

筅森魡賤 提交于 2019-12-14 03:23:17

问题


I am trying to do below things. Plesse check.

rule "new rule"
        salience -101
        dialect "mvel"
when
$pricingLineItem : PricingLineItem( $ackId : ackId, $prefix : prefix  )
$baseUpChargeConfig : BaseUpChargeConfig( $baseOptionId : baseOptionId,
                                          prefix == $prefix )
$pricingOptionType : PricingOptionType( ackId == $ackId,
                 $optionId : optionId, $optionValue : optionValue )
$baseOptionConfig : BaseOptionConfig( bOptionValue == $optionValue,
                   bOptionCode == $optionId ,id == $baseOptionId )
then
  $pricingLineItem.increment($baseOptionId);
  System.out.println("excuted - "+ $baseOptionId +" "+$baseOptionConfig);   
end

There will multiple BaseUpChargeConfig object match for one PricngLineItem. In the BaseUpChargeConfig object, we are getting all related BaseOptionConfig object and then trying to matche with PricingOptionType object of PricingLineItem. I need to take the best BaseUpChargeConfig object having maximum match with the PricingOptionType object of PricngLineItem.

EDIT

Say I have one PricingLineItem object with ackID, prefix value. Now, I have multiple set of BaseUpChargeConfig object based on prefix value of PricingLineItem.

Now on ackId value, I have certain set of PricingOptionType object in rule engine.

and Also on baseOptionId value, I have multiple BaseOptionConfig object.

In PricingOptionType and BaseOptionConfig object, I need to compare the optioncode and option value.

If both are matching, I need to collect all matched pricing option type for a perticuler BaseUpChrageConfig.

In the same way, this will check for all other BaseUpChrageConfig object BaseOptionConfig and match.

Now the highest matched BaseOptionConfig object ; we will select that BaseUpChargeConfig as best object for our purpose.

I hope it would be clear for you.

Currently I am doing through java method by passing all three and calculating in java.

public void matchOptions(BaseUpChargeConfig config, List pricingOptionList, List baseOptionList) {

if ((pricingOptionList != null && !pricingOptionList.isEmpty())
        && (baseOptionList != null && !baseOptionList.isEmpty())) {

    List<PricingOptionType> matchedOption = null;
    matchedOption = new ArrayList<PricingOptionType>();
    for (PricingOptionType pOption : pricingOptionList) {
        int matchCount = 0;

        for (BaseOptionConfig bConfig : baseOptionList) {
            boolean optioncodeMatch = pOption.getOptionCode() == bConfig.getBaseOptionCode();
            boolean optionValueMatch = pOption.getOptionValue() == bConfig.getBaseOptionValue();
            if (optioncodeMatch && optionValueMatch) {
                matchedOption.add(pOption);
                matchCount++;
            }
        }
        if (matchCount > 0) {
            if (bestBaseUpChargeConfig != null) {
                optionMatchCount = matchCount;
                bestBaseUpChargeConfig = config;
                matchedPrcOptionList = matchedOption;
            } else if (matchCount == optionMatchCount) {
                bestBaseUpChargeConfig = null;
                matchedOption = null;
                matchedPrcOptionList.clear();
            } else if (matchCount > optionMatchCount) {
                optionMatchCount = matchCount;
                bestBaseUpChargeConfig = config;
                matchedPrcOptionList = matchedOption;
            } else {
                // do nothing
            }
        }
    }

} else {
    // do nothing
}

}

Thanks


回答1:


This compiles with 5.5, so it shouldn't be a problem with 6.x either.

The duplication of the accumulate can't be helped unless you consider a more complicated evaluation involving derived facts.

rule "find best BaseUpChargeConfig"
when
// pick up some PricingLineItem
$pli: PricingLineItem( $prefix: prefix, $ackId : ackId )
// it should have a BaseUpChargeConfig with a matching prefix
$bucc: BaseUpChargeConfig( prefix == $prefix,
                           $baseOptionId : baseOptionId )
// count BaseOptionConfigs (linked to BaseUpChargeConfig) matching
// PricingOptionTypes, by option id/code and option value
accumulate(
    BaseOptionConfig( id == $baseOptionId,
                      $ocod: bOptionCode, $oval: bOptionValue )
    and          
    PricingOptionType( ackId == $ackId,
                       optionId == $ocod, optionValue == $oval );
      $count: count(1) )

// The $count computed above is the maximum if we don't have another
// BaseUpChargeConfig (for that prefix) where the count of the
// subordinate BaseOptionConfigs is greater than $count
not(
    ( BaseUpChargeConfig( this != $bucc,
                          prefix == $prefix,
                          $baseOptionId2 : baseOptionId )
      and
      accumulate(
          BaseOptionConfig( id == $baseOptionId2,
                            $ocod2: bOptionCode, $oval2: bOptionValue )
          and            
          PricingOptionType( ackId == $ackId,
                             optionId == $ocod2, optionValue == $oval2);
      $count2: count(1);
      $count2 > $count ) ) )
then
    System.out.println( "best BaseUpChargeConfig: " + $baseOptionId );
end


来源:https://stackoverflow.com/questions/28805373/how-to-achieve-below-objective-in-drools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!