Spring 将双值设置为0
下面是我的fireRules()方法,其中orderresponseVO对象被插入到会话中,以基于totalorderprice计算收益Spring 将双值设置为0,spring,spring-mvc,drools,Spring,Spring Mvc,Drools,下面是我的fireRules()方法,其中orderresponseVO对象被插入到会话中,以基于totalorderprice计算收益 private void fireRules(ProductResponseVO orderresponseVO,OrderDetailsVO orderdetailsVO{ orderresponseVO.setDiscounts(null); FactHandle fact= vbDiscSession.insert(orderresponseVO); v
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);
}
这是.drl文件
编写了2条规则,根据totalordervalue和一条默认规则计算添加折扣,该规则每次打印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
根据totalordervalue添加新折扣VO
下面是addDiscount()方法
这里的问题是,无论何时使用drl文件中的addDiscount()添加DiscountVo,DiscountVo中的构造函数都会将第二个参数设置为0,即使实际计算不同。
我已交叉检查以验证折扣值。即使不为零,计算也设置为零
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
与其直接将乘法表达式传递给构造函数,不如将乘法常量0.35和0.10复制到一个变量$mulvalue,并将乘积存储在另一个变量$total中。这是可行的。但我不明白为什么直接乘法表达式不起作用。我重复了您报告的问题。这是一个由int乘以double引起的mvel错误,我会尽快修复它。请注意,使用java方言,它确实可以正常工作。相反,如果出于某种原因,您需要使用mvel方言,我建议的最快解决方法是首先使用双精度:在您的情况下,使用0.35*$responseVO.totalorderprice按预期工作。您如何得出结论,是DiscountTVo中的构造函数造成了损害?Java方法或构造函数无法“将参数设置为零”。如果折扣VO对象中的折扣值为零,则可能有其他原因,因为调用构造函数看起来没问题。是的,劳恩。问题不在构造函数中,而在.drl文件$responseVO.totalorderprice*0.35--->中的计算结果是0,而totalorderprice是408,但乘法结果是0。您使用的是哪个Drools版本?我的Drools版本是5.6.0.Final--launeWe也遇到了这个问题。您能告诉我们是否有问题跟踪参考?它是否在mvel 2.2.0最终版中固定?它在mvel 2.2.2.0最终版中固定。
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;
}
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