Kamil
Kamil

Reputation: 1

Drools get object from map mvel cast exception

I am using jboss EAP 7.2 and Red Hat Decision Central 7.5.0

I have a custom objects like that

public class Model{
    private String id;
    private Map<String, Object> map;

    // ... getters and setters
}

public class ParameterModel{
   private String parameterName;
   private BigDecimal maxValue;
   private BigDecimal minValue;
   private Object value;

   // ... getters and setters
}

I have created new "Model" object that has custom "id" attribute and "map" attribute contains <parameterName, ParameterModel> pairs.

I sent it to decision manager and drools side ı want to achieve ParameterModel attributes but I could not.

My rule is below.

package com.rule.test;

import com.test.Model;
import com.test.ParameterModel;
    
rule "drools1"
    when
        Model(getId().equals("1"), Integer.parseInt(((ParameterModel)getMap().get("param1")).getValue().toString())>10)
    then
        System.out.println("Error on " + drools.getRule().getName());
end

The exception is below.

Caused by: [Error: null pointer: Integer.parseInt(((ParameterModel)getMap().get("param1")).getValue().toString())] [Near : {... Integer.parseInt(((ParameterMo ....}] In [Rule "drools1" in com/rule/test/test.drl]

Caused by: java.lang.NullPointerException at org.mvel2.DataConversion.convert(DataConversion.java:129) at org.mvel2.ast.TypeCast.getReducedValueAccelerated(TypeCast.java:74) at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38) at org.mvel2.ast.Substatement.getReducedValueAccelerated(Substatement.java:44) at org.mvel2.ast.Union.getReducedValueAccelerated(Union.java:44) at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38) at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:970) at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:396)

Thank you all.

Upvotes: 0

Views: 874

Answers (1)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15190

Drools has a lot of built-in null checking, but if you insist on bypassing it you're going to end up with a lot of errors.

The other kind of interesting Drools has is a special syntax for maps -- get("foo") can be written as this["foo"].

rule "drools1"
when
  Model( id == "1", // equivalent to getId().equals("1")
         $map: map != null )

  // Special syntax for getting stuff from maps:
  Map( $param1: this["param1"] != null ) from $map 

  ParameterModel( $value: value != null ) from $param1
  Integer( this > 10 ) from Integer.parseInt($value.toString())
then
  System.out.println("Error on " + drools.getRule().getName());
end

Why was your version giving a NPE? Not a clue, but it was also just about unreadable in its original form. All that really can be gleaned from the stack trace was that it was occurring at some implicit conversion step.

Of course this version will also fail if value isn't something whose toString() turns into a String representation of an integer.

Note that if value is actually an Integer type, then you don't need to waste time with the parse int and can just do:

Integer(this > 10) from $value

Upvotes: 0

Related Questions