Rule When for BigDecimal without using Eval

925 views
Skip to first unread message

murali mohan

unread,
Apr 12, 2016, 10:50:44 AM4/12/16
to Drools Usage
I want to write a condition for when a BigDecimal Object is greater than another BigDecimal Object. How can I do this? The following attempt, does not work. Please suggest.

For example:
rule "testRule"
when
   
BigDecimal( this.compareTo($anotherBigDecimal) >0 )

then
...
...

end

Thanks.

Mario Fusco

unread,
Apr 12, 2016, 11:44:24 AM4/12/16
to Drools Usage
Hi,

where is $anotherBigDecimal declared? Can you be a bit more precise? Why do you say that this doesn't work?

Thanks,
Mario

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/680822a8-2079-447b-a5cc-4590d5da6723%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

murali mohan

unread,
Apr 12, 2016, 12:05:49 PM4/12/16
to Drools Usage
We are using Drools 6.3.0. Improved the Rule example to show where $anotherBigDecimal comes from.

The error I get, on validation, from Drools WorkBench is:
[ERR 102] Line 18:30 mismatched input '>' in rule "<Rulename>"


rule "testRule"
when
    $mortgage
: test.Mortgage()
    $anotherBigDecimal
: BigDecimal() from $mortgage.amount
   
BigDecimal( this.compareTo($anotherBigDecimal) > 0 )
then
...
...

end


Mario Fusco

unread,
Apr 13, 2016, 3:29:06 AM4/13/16
to Drools Usage
Hi again,

I developed this test case

@Test
public void testBigDecimalCompareTo() {

String drl =
"import " + BigDecimal.class.getCanonicalName() + ";\n" +
"rule R1 when\n" +
" $bd1 : BigDecimal()\n" +
" $bd2 : BigDecimal( this.compareTo($bd1) > 0 )\n" +
"then \n" +
"end\n";

KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL )
.build().newKieSession();

kieSession.insert(new BigDecimal( 1.0 ));
kieSession.insert(new BigDecimal( 2.0 ));

assertEquals( 1, kieSession.fireAllRules() );
}
and also this one that is more similar to your use case

@Test
public void testBigDecimalCompareTo2() {

String drl =
"import " + Mortgage.class.getCanonicalName() + ";\n" +
"import " + BigDecimal.class.getCanonicalName() + ";\n" +
"rule R1 when\n" +
" $mortgage: Mortgage()\n" +
" $anotherBigDecimal: BigDecimal() from $mortgage.amount\n" +
" BigDecimal( this.compareTo($anotherBigDecimal) > 0 )\n" +
"then \n" +
"end\n";

KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL )
.build().newKieSession();

kieSession.insert(new Mortgage(new BigDecimal( 1.0 )));
kieSession.insert(new BigDecimal( 2.0 ));

assertEquals( 1, kieSession.fireAllRules() );
}

public static class Mortgage {
private final BigDecimal amount;

public Mortgage( BigDecimal amount ) {
this.amount = amount;
}

public BigDecimal getAmount() {
return amount;
}
}
and they both works for me.

Which version of Drools are you using?
If you still have this problem, please provide a complete reproducer so I could investigate it.

Thanks,
Mario


murali mohan

unread,
Apr 15, 2016, 8:33:36 AM4/15/16
to Drools Usage
Sorry, my mistake. It is indeed working. The rule was complaining because of some other error.

Very sorry for making you write these test cases.

Thanks.
Reply all
Reply to author
Forward
0 new messages