Search code examples
drools

Drools: best practice in how to compare properties in two lists


I have two lists of objects

A. Setting: {String command, String setting}

B. Recommendation: {String command: String recommendedSetting, String: risk}

Using drools I want to check if any settings objects do not match the recommendation where commands are equal but setting != recommendedSetting.

I have this working using a global list for B where A objects are added as facts and all rules fired.

I can also input the two lists as facts and do a nested loop but that does not seem like the right way.

import com.demo.drools.model.Setting;
import com.demo.drools.model.Recommendation;
import java.util.List;

global List<Recommendation> recommendations;
global List<Recommendation> suggestedRecommendations;

dialect  "mvel"

rule "Check Setting For Recommendation"
    when
        $recommendation : Recommendation() from recommendations;
        $setting: Setting(parameter == $recommendation.parameter && setting != $recommendation.setting)
    then
        suggestedRecommendations.add($recommendation);
end

I was hoping for some guidance as to whether this is the best practice/most efficient method.


Solution

  • If you could avoid having the list as facts and to simply insert their elements into your session, then I would suggest you to leave Drools make the loop for you:

    rule "Check Setting For Recommendation"
    when
      $r: Recommendation();
      Setting(parameter == $r.parameter, setting != $r.setting)
    then
      suggestedRecommendations.add($r);
    end
    

    Hope it helps,