vs777
vs777

Reputation: 634

How to populate Map in Drools ResponseObject

I have an object defined in .drl :

package drools.types

declare  ResponsibilityManager 
    @propertyReactive
    internal : java.lang.Long
    attr1 : java.lang.String  
    band : java.lang.Long
    resourceMap : java.util.HashMap

end

My Rule is also defined in DRL :

package drools.rules

import drools.types.*

rule "Third Test Rule"
    dialect "mvel"
    salience 0
    no-loop true

 when
   $var : ResponsibilityManager( band > 4  )
 then
     modify($var) { attr1 = "hidden";}
     modify($var) { getResourceMap().put("action","edit");}
end

Rule is getting compiled correctly, however when I execute the Rule from my Java application I am getting "NullPointerException" in that line :

modify($var) { getResourceMap().put("action","edit");}

I am able to update and return "attr1" property.

How do I update the Map in the Rule?

Upvotes: 0

Views: 333

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15190

Well, clearly the resourceMap was never instantiated.

When you create the ResponsibilityManager, you need to make sure to create the resourceMap inside of it as well.

rule "Whatever creates the ResponsibilityManager instance"
when
  // ...
then
  ResponsibilityManager r = new ResponsibilityManager();
  r.setResourceMap(new HashMap());
  // set other fields
  insert(r);
end

Then you can modify it normally without getting a null pointer.

Alternatively you could overwrite the value entirely, something like ...

rule "Example with overwrite"
when
  $var: ResponsibilityManager( band > 4, $resources: resourceMap != null )
then
  $resources.put("new value", "example");
  modify($var) {
    setAttr1("hidden"),
    setResourceMap($resources)
  }
end

(Of course you'll need to make sure your resourceMap is instantiated already because of that null-check.)

Upvotes: 1

Related Questions