You may be better asking on the MVEL mailing list for this. My best guess is that it is a
combination from the compiler knowing that the result of the + is being assigned to a
string and because all your variables are strings it uses string operations. You’d have to
check but I’d also imagine that the expression string1 * string2 probably compiles to
something like (String)( (number)string1 * (number)string2 ) ie while within the
calculation the strings are converted to numbers the result may still be a string.
Either way I think the expression is vary ambiguous and putting casts in there don’t make
it messy they make it obvious what exactly your wish is.
Thomas
From: rules-users-bounces(a)lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On
Behalf Of Kinshuk Adhikary
Sent: 21 December 2009 10:12
To: Rules Users List
Subject: Re: [rules-users] Re-sending : Unexpected MVEL plus
operatorbehavior(string-to-double), in consequence block
Hi,
It does work if cast, but the logic of the + operator is what I am trying to get at.
1. Having converted them (as you said, multiplication will try to convert and it does),
they ought to be now numbers. So the + operator should not concatenate, should just add
the numbers.
Actually leaving messy casts all around is what is "not nice" about it.
Regards and thanks
Kinshuk
--- On Mon, 12/21/09, Swindells, Thomas <TSwindells(a)nds.com> wrote:
From: Swindells, Thomas <TSwindells(a)nds.com>
Subject: Re: [rules-users] Re-sending : Unexpected MVEL plus operator
behavior(string-to-double), in consequence block
To: "Rules Users List" <rules-users(a)lists.jboss.org>
Date: Monday, December 21, 2009, 2:54 PM
My gut instinct would say that this is possibly correct.
I’ve not looked at the mvel code but if all your types are strings then it makes sense for
it to pick the most type specific operator first. Multiplication only works on numbers so
it attempts to convert them but the + operator is defined on strings and therefore is a
closer match for strings as you are assigning the result to a string.
I haven’t tried it but you could liberally sprinkling in brackets and type casts to get
what you want, eg:
t.out = (double)(t.in2*t.in3) + (double)(t.in3*t.in4) + (double)(t.in2*t.in4);
Thomas
From: rules-users-bounces(a)lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On
Behalf Of Kinshuk Adhikary
Sent: 21 December 2009 02:48
To: rules-users(a)lists.jboss.org
Subject: [rules-users] Re-sending : Unexpected MVEL plus operator
behavior(string-to-double), in consequence block
Using
drools 5.1
mvel 2-2.0.14
In consequence (then) block : (dialect strict or not does not make a difference)
Trying to sum up some numbers after a multiplication operation. For example :
t.out = t.in2*t.in3 + t.in3*t.in4 + t.in2*t.in4;
(the object t has all its attributes of the type String), all the t.in parameters are
actually numbers-as-strings.
So the multiplication is going on fine, as expected, but the addition (+) sign, is
concatenating instead of adding.
So the for inputs "1", "2" and "3", the result looks
something like : 2.06.03.0 (i.e concetenated) instead of the expected 11.0.
What is going worng How does the + operator decide to do concat instead of doing
addition.
Any documentation on this + operator logic ?
Note : The above works fine when I use MVEL without Drools.
Thanks
________________________________
**************************************************************************************
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes. To protect the environment please
do not print this e-mail unless necessary.
NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United
Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603
8808 40-00
**************************************************************************************
________________________________
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes.
To protect the environment please do not print this e-mail unless necessary.
An NDS Group Limited company.
www.nds.com
-----Inline Attachment Follows-----
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org</mc/compose?to=rules-users(a)lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/rules-users
________________________________
**************************************************************************************
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes. To protect the environment please
do not print this e-mail unless necessary.
NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United
Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603
8808 40-00
**************************************************************************************
________________________________
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes.
To protect the environment please do not print this e-mail unless necessary.
An NDS Group Limited company.
www.nds.com