双值设置为0

时间:2014-03-24 13:08:47

标签: spring spring-mvc drools

这是我的fireRules()方法,其中orderresponseVO对象被插入到会话中,以根据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);
}

这是.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构造函数,用于设置基于totalordervalue计算的discountname和discountvalue

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;
    }

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

2 个答案:

答案 0 :(得分:1)

我转载了你报道的问题。这是一个由int乘以double引起的错误,我会尝试尽快解决这个问题。请注意,使用java方言它按预期工作。相反,如果由于某种原因你需要使用mvel方言,我建议的最快的解决方法是先放双:在你的情况下做0.35 * $ responseVO.totalorderprice按预期工作。

答案 1 :(得分: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中。这样可行。但是我无法弄清楚为什么直接乘法表达式没有工作