I reproduced the issue you reported. It is a mvel bug caused by the multiplication of an int by a double and I'll try to fix this asap. Note indeed that using the java dialect it works as expected. Conversely, if for some reason you need to use mvel dialect, the quickest workaround I can suggest is putting the double first: in your case doing 0.35*$responseVO.totalorderprice works as expected.
Double value getting set to 0
-
20-06-2023 - |
문제
Here is my fireRules() method within which orderresponseVO object is inserted in to the session to calculate earnings based on totalorderprice.
private void fireRules(ProductResponseVO orderresponseVO,OrderDetailsVO orderdetailsVO{
orderresponseVO.setDiscounts(null);
FactHandle fact= vbDiscSession.insert(orderresponseVO);
vbDiscSession.fireAllRules();
calculateDiscounts(orderresponseVO);
orderdetailsVO.setEarnings(orderresponseVO.getEarnings());
orderdetailsVO.setInvoiceAmount(orderresponseVO.getInvoice());
vbDiscSession.retract(fact);
}
Here is the .drl file 2 rules are written to calculate add discounts based on totalordervalue and a default rule which get fired everytime to print the totalorderprice
//created on: Mar 21, 2014
package com.mit.rules.vb
import com.mit.vb.admin.order.bean.ProductResponseVO
import com.mit.vb.admin.order.bean.DiscountVO
import com.mit.vb.admin.order.bean.OrderResponseVO
//list any import classes here.
dialect "mvel"
//declare any global variables here
rule "Discount"
salience 100
no-loop true
when
$responseVO: ProductResponseVO(totalorderprice > 250)
//conditions
then
//actions
$responseVO.addDiscount(new DiscountVO("test",$responseVO.totalorderprice*0.35));
end
rule "Discount special"
salience 50
no-loop true
//include attributes such as "salience" here...
when
$responseVO: ProductResponseVO(totalorderprice >= 500)
//conditions
then
$responseVO.addDiscount(new DiscountVO(" You made it ",$responseVO.totalorderprice*0.10));
//actions
end
rule "Print before every rule" salience 150
when
$responseVO: ProductResponseVO()
then
// System.out.println( " -------------- " + $cpSellerDetails.cpInfoBean.name);
System.out.println( " -------------- " + $responseVO.totalorderprice);
end
Based on the totalordervalue new DiscountVO are added.
Here is the addDiscount() method
public void addDiscount(DiscountVO discount) {
if(this.discounts ==null)
this.discounts = new ArrayList<DiscountVO>();
discounts.add(discount);
}
and DiscountVO constructor to set the discountname and discountvalue that is calculated based on totalordervalue
public class DiscountVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3440977389992293711L;
private int orderid;
private String discountName;
private Double discountValue;
private boolean isPercent=false;
private Double discountPercent;
public DiscountVO(String discountName,Double discountValue){
this.discountName= discountName;
this.discountValue=discountValue;
}
The problem here is whenever a DiscountVo is added using addDiscount() in drl file the constructor in DiscountVO is setting the 2nd argument to 0 even though the actual calculation differs. I have cross checked to verify the discountValue.The calculation even not being zero is set to zero
해결책
다른 팁
package com.mit.rules.vb
import com.mit.vb.admin.order.bean.ProductResponseVO
import com.mit.vb.admin.order.bean.DiscountVO
//list any import classes here.
dialect "mvel"
//declare any global variables here
rule "Discount"
salience 100
no-loop true
when
$responseVO: ProductResponseVO(totalorderprice > 250)
//conditions
then
//actions
$mulvalue=0.35;
$total=$responseVO.totalorderprice*$mulvalue;
System.out.println( " Discount " + $total);
$responseVO.addDiscount(new DiscountVO("test", $total));
end
rule "Discount special"
salience 50
no-loop true
//include attributes such as "salience" here...
when
$responseVO: ProductResponseVO(totalorderprice >= 500)
//conditions
then
$mulvalue=0.10;
$total=$responseVO.totalorderprice*$mulvalue;
System.out.println( " Discount special " + $total);
$responseVO.addDiscount(new DiscountVO(" You made it ",$total));
//actions
end
rule "Print before every rule" salience 150
when
$responseVO: ProductResponseVO()
then
// System.out.println( " -------------- " + $cpSellerDetails.cpInfoBean.name);
System.out.println( " -------------- " + $responseVO.totalorderprice);
end
Instead of directly passing the multiplication expression to the constructor ,copy the multiplication constant 0.35 and 0.10 to a variable $mulvalue and store the product in other variable $total.This works.But i cannot figure out why direct multiplication expression doesn't work