Is there any way to let Drools handle a String, Integer comparison without having to manually do type conversions?
I am having trouble comparing a string to an integer in a rule. The rule can be simplified to the following where fields is just a Map<String, String>.
rule "Test Rule"
when
MapTest(fields["a"] != null, fields["a"] == 10)
then
System.out.println( "test succeeded" );
end
The problem is that given this rule, even when a = “10”, the consequence is never fired. According to the documentation at section 4.8.3.3.3 “Coercion is always in favor of the field type and not the value type”. So this rule seems like the 10 should be coerced to “10” and then this comparison should return true, but it doesn’t.
If a change the 10 to “10” the rule succeeds. Also if I do Integer.parseInt(fields[“a”]) the rule succeeds.
The more interesting part is if I change the operator to > or < and change the value of fields[“a”] appropriately the rule still succeeds! Is this because > and < aren’t technically valid operators on Strings so the two operands are converted to numeric values and compared?
Drools appears to handle type coercion differently between versions 5.3 and 5.4. Wolfgang gave a good example of the differences on the Drools User Forum.
To me this seems like a bug in version 5.3 because the documentation says coercion should be attempted but according to the example it is not (or at least it is not done properly). In the end I still do not know how the > and < operators work but at least I gathered enough information to know I have to parse the string to a numeric value in my case.