2014-03-24 113 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