'Unable to get a variable value in drools and use it to set in another field

drl file :

rule "pendingMsgSizeModified"
when
$tibcoEMSObj : TibcoEMSData(deliveredMsgCnt.contains("MB") && pendingMsgSize \> 100)  
eval (!($tibcoEMSObj.typeOfAlert != null))
then
$tibcoEMSObj.setSendEmail(true);
$tibcoEMSObj.setTypeOfAlert($tibcoEMSobj.getTypeOfAlert()+"pendingMsgSize###");
update($tibcoEMSObj)
end

It gives error at this location : $tibcoEMSobj.getTypeOfAlert()

Error : \[Error: unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.$tibcoEMSobj()\]
\[Near : {... SObj.setTypeOfAlert($tibcoEMSobj.getTypeOfAlert()+ ....}\]

I am expecting to use the getter method to get it and concatenate the string to set in another variable and save/update it



Solution 1:[1]

Your syntax is all over the place. And without actually seeing your model, it's hard to tell what you're even trying to do.

But based on what you said:

I am expecting to use the getter method to get it and concatenate the string to set in another variable and save/update it

And the code you did provide, I think this is what you're trying to do:

rule "pendingMsgSizeModified"
when
    $tibcoEMSObj : TibcoEMSData(
        deliveredMsgCnt.contains("MB"),
        pendingMsgSize > 100,
        $alertType: typeOfAlert != null
     )
then
    String newAlert = $alertType + "pendingMsgSize###";
    modify($tibcoEMSObj) {
        setSendEmail(true)
        setTypeOfAlert(newAlert)
    }
end

I presumed that your eval was either typo'd or just plain wrong since it was checking that the value was actually null. Don't use eval unless you have absolutely no other choice. (And I've never actually run into a situation where there was no other choice.)

I also presumed that the alert type will not change. I don't know what your sendEmail method does, or if it has side effects; if it does have side effects that change the typeOfAlert, then you'll of course need to call getTypeOfAlert() after calling setSendEmail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roddy of the Frozen Peas