[rules-users] do I have to use temporary variable to overcome data casting issue ?

Martin Minka martin.minka at gmail.com
Mon May 13 07:21:18 EDT 2013


I continue to play with the code attached to
https://issues.jboss.org/browse/DROOLS-136 in src.zip. Updated invoice.drl
is attached in this mail.

I changed the rule "calculate totalPrice per item" to MVEL and trying to
calculate and set totalPrice with one liner:

((DataItemPrice)$v).setTotalPrice(((DataItemPrice)$v).getQty()*((DataItemPrice)$v).getUnitPrice());

but that fails with message: java.lang.IllegalArgumentException: object is
not an instance of declaring class

On the other hand using temporary variable of type Double will work. Here
is the complete rule which works up to the comment "// THIS IS DOESN'T
WORK":

rule "calculate totalPrice per item"
salience -10
no-loop
when
    $q:Message(id=="item", answered==true,
        value instanceof DataItemPrice, $v:value
    )
then
    System.out.println("setting totalPrice on item: " +
((DataItemPrice)$v).getName());

    System.out.println("qty*unitPrice=" +
((DataItemPrice)$v).getQty()*((DataItemPrice)$v).getUnitPrice());

*    Double d =
((DataItemPrice)$v).getQty()*((DataItemPrice)$v).getUnitPrice();
    ((DataItemPrice)$v).setTotalPrice(d);
*    System.out.println("setting via temporary variable worked");

    // THIS IS DOESN'T WORK
    *
((DataItemPrice)$v).setTotalPrice(((DataItemPrice)$v).getQty()*((DataItemPrice)$v).getUnitPrice());
*
    System.out.println("setting directly works ???");

    update($q);
end

Am I again wrongly hoping that MVEL will simplify data types casting ? Or
should I fail a bug ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20130513/c1b3a1ef/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: invoice.drl
Type: application/octet-stream
Size: 1540 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20130513/c1b3a1ef/attachment.obj 


More information about the rules-users mailing list