Search code examples
javadrools

Java Drools : Cast object with possible null values to double & then compare


I am trying to trigger a condition using Java Drools & here is the code snippet-

rule "ABC Pass"
when
    form: Form_ABC()
    $m: Map()
    $ABC: Object() from $m["ABC_ID"]
    $a: Object() from $m["ABC_26_a"]
    $b: Object() from $m["ABC_26_b"]
    Map (Double.valueOf($a.toString()) < 0 && Double.valueOf($b.toString()) < 0)
then
    System.out.println("ABC Rule Pass \t\t" 
                        + $ABC.toString() 
                        + "\tField 1 is: " + $a.toString()
                        + "\tField 2 is: " + $b.toString());
end

So, the rule should get triggered when data in columns ABC_26_a & ABC_26_b are less than zero. It is to be noted that the columns have null/blank or special characters like $, (, ).

I am getting the below errors -

Caused by: java.lang.NumberFormatException: For input string: "-5,099.00"
Caused by: java.lang.NumberFormatException: empty String

Can you please guide me the best way to handle them? The data-set I am handling is pretty small. Thanks.


Solution

  • The String value "-5,099.00" does not represent a floating point literal according to the Java Language specification, hence Double.valueOf( "-5,099.00" ) throws an exception.

    You can fix your data or use java.text.DecimalFormat for converting your current format to double, using the parse method available there. Design the format according your knowledge of the full range of possible input formats.