2014-03-24 207 views
1

以下是我的fireRules()方法,其中orderresponseVO对象将插入到会话中,以基于totalorderprice计算收益。将双精度值设置为0

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新DiscountVO默认规则被添加。

这里是addDiscount()方法

public void addDiscount(DiscountVO discount) { 
     if(this.discounts ==null) 
      this.discounts = new ArrayList<DiscountVO>(); 

     discounts.add(discount); 
    } 

和DiscountVO构造设置discountname和discountvalue是基于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; 
    } 

计算这里的问题是每当使用添加DiscountVo drl文件中的addDiscount()DiscountVO中的构造函数将第二个参数设置为0,即使实际计算不同。 我已经交叉检查,以验证discountValue.The计算甚至不为零设置为零

+0

你如何得出结论:DiscoutVO中的构造函数会造成损害? Java方法或构造函数无法“将参数设置为零”。如果DiscountVO对象中的discountValue为零,则可能有其他原因,因为调用构造函数看起来确定。 – laune

+0

是laune.The问题不是在构造函数中,而是在.drl文件中的计算$ responseVO.totalorderprice * 0.35 --->这个计算结果为0,而总的订购价格是408,但这个乘法给出结果0 – sai

回答

1

我转载了你报告的问题。它是一个int乘以double的倍增造成的一个mvel错误,我会尽快解决这个问题。请注意,使用java方言,它按预期工作。相反,如果出于某种原因需要使用mvel方言,我可以建议的最快的解决方法是将双重优先:在您的情况下,0.35 * $ responseVO.totalorderprice按预期工作。

+0

我们也遇到过这个问题。你能否告诉我们是否有问题跟踪参考?它是否被固定在mvel 2.2.0 Final? –

+0

它被固定在碎石2.2.2.Final。 –

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.This产品的作品。但我不明白为什么直接乘法表达式不起作用

+0

哪个Drools版本你正在用吗? – laune

+0

我的drools版本是5.6.0.Final - laden – sai