Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

java - How to achieve below objective in drools

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...