<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><div>Hi,<br><br>I'm getting unexpected results when when using BigDecimal objects for matching in rules. <br>I'm generating DRL in my code, using freemaker.<br><br>I have a RuleEngineNumericFact object which has two attributes:<br>String propertyName<br>BigDecimal propertyValue<br><br>Below is an example of a rule where I try to match a RuleEngineNumericFact object based on the two attributes:<br>
<br>rule "aaa"<br>&nbsp;&nbsp;&nbsp; agenda-group "r1"<br>&nbsp;&nbsp;&nbsp; dialect "java"<br>&nbsp;&nbsp;&nbsp; when<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (RuleEngineNumericFact( propertyName == "somename" , <span style="font-weight: bold;">propertyValue == 10.8 </span>))<br>&nbsp;&nbsp;&nbsp; then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String ruleId = drools.getRule().getName();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; firedRuleIDs.add(ruleId);<br>end<br><br><br>Then I create a new RuleEngineNumericFact object and set its propertyValue as following:<br><br>String <span style="font-weight: bold;">stringValue = "10.8"</span>;<br>
BigDecimal propertyValue = new BigDecimal(stringValue, new MathContext(10));<br><br>(I also tried to use default precision settings by creating the BigDecimal value as:<br>BigDecimal propertyValue = new BigDecimal(stringValue);<br>)<br>
<br>When I insert this fact into the rule engine - the rule does not get fired, so the fact does not match.<br><br>One note: if the values are integeres (like, propertyValue == 100) - the rule gets fired. So, it is only the floating point numbers that do not match.<br><br>I generate the DRL in my Java code, using freemaker, and I specify the
value that will be used in the rule for matching by calling toString()
method on a BigDecimal object that I get from my business code. The toString() representation ends up to be the "10.8" value (for example).<br>According to the BigDecimal documentation, marshalling from/to the same string representation shoudl still produce BigDecimal objects that will return TRUE from the equals() method when compared.<br><br><br>any ideas why this is not working here?<br>How does Drools compare values of objects like BigDecimal?<br><br>thanks,<br>Marina<br><br><br><br><br>
<br><br></div></div></body></html>