Shanij P.S
Shanij P.S

Reputation: 162

Hashmap from global variable is not able to access in drools when condition

I have flowing drools rules :

import com.....AEAlertDetails;
import function com....DroolsUtility.log;
import com....AeAlerts;
import function com....utility.AEAlertUtility.addAeAlerts;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com....model.AlertType;

dialect "java"

global HashMap<String,AlertType> aeRuleMap;
global List aeAlertsList;


rule "My Rule" salience 4
no-loop true
    when
       $aeAlertDetails : AEAlertDetails( aERuleMatched == false, san == aeRuleMap.get("1").isSan() )
    then
      //
end

From Java class I set like :

List<AeAlerts> aeAlertsList = new ArrayList<>();

kieSession.setGlobal("aeAlertsList", aeAlertsList);

HashMap<String,AlertType> aeRuleMap=new HashMap<>();

aeRuleMap.put("1", new AlertType(true,true,true,true));

aeRuleMap.put("2", new AlertType(true,true,true,true));

kieSession.setGlobal("aeRuleMap", aeRuleMap);

kieSession.fireAllRules();






package com......evaluation.model;

public class AlertType {

    private boolean isSan;
    private boolean isPep;
    private boolean isEdd;
    private boolean isPrb;
    
    private String name;

    public AlertType() {
        super();
    }

    public AlertType(boolean isSan, boolean isPep, boolean isEdd, boolean isPrb) {
        super();
        this.isSan = isSan;
        this.isPep = isPep;
        this.isEdd = isEdd;
        this.isPrb = isPrb;
    }

    public boolean isSan() {
        return isSan;
    }

    public void setSan(boolean isSan) {
        this.isSan = isSan;
    }

    public boolean isPep() {
        return isPep;
    }

    public void setPep(boolean isPep) {
        this.isPep = isPep;
    }

    public boolean isEdd() {
        return isEdd;
    }

    public void setEdd(boolean isEdd) {
        this.isEdd = isEdd;
    }

    public boolean isPrb() {
        return isPrb;
    }

    public void setPrb(boolean isPrb) {
        this.isPrb = isPrb;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I need to get the Boolean value from the Map and compare with the fact level variable value . For that purpose I added this line in the drool :

san == aeRuleMap.get("1").isSan()

Due to this line in the rule, the drool evaluation is stuck for sometime and throwing exception . I need this condition checking in the when part itself.

How to do I write it in drool for getting a key from global hashmap and compare

Upvotes: 0

Views: 80

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15179

There's a number of ways. I'd probably go with this:

HashMap( $alertType: this["1"] ) from aeRuleMap
$aeAlertDetails : AEAlertDetails( aERuleMatched == false, 
                                  san == $alertType.isSan())

Or you could do like:

$alertType: AlertType( $san: san ) from aeRuleMap.get("1")
$aeAlertDetails : AEAlertDetails( aERuleMatched == false, 
                                  san == $san)

You don't indicate what kind of exception is being thrown, so I can't be entirely sure of why what you have doesn't work. Just remember that you mustn't be doing realtime updates to the globals and expecting them to be reflected in your rules. If your exception is actually some form of concurrent modification exception, you're likely misusing the global concept entirely.

Upvotes: 1

Related Questions