Jexl的长到两倍

时间:2011-11-26 12:58:38

标签: java jexl

我从apache使用Jexl lib,并且在使用Expression类的evaluate()方法时遇到一些问题。这是NelderMead类的代码:

import org.apache.commons.jexl2.*;

public class NelderMead {
    // контсанты
    private static int      M = 3;
    private static double   E = 0.005;
    private static double   A = 1.000;
    private static double   B = 0.500;
    private static double   Y = 2.000;

    // переменные
    private JexlEngine jexl;
    private Expression func;
    private String funcString = "";
    private MapContext[] iterations;

    public NelderMead(){
        this.jexl = new JexlEngine();
    }

    public NelderMead(String funcString){
        this.jexl = new JexlEngine();
        this.setFunc(funcString);
    }


    public void setFunc(String funcString){
        this.funcString = funcString;
        this.func = this.jexl.createExpression(funcString);
    }

    public double funcEval(MapContext args){
    return ((Double) this.func.evaluate(args)).doubleValue();

    }

    public boolean checkCriterian(){
        return true;
    }
}

测试用例的代码是:

import org.apache.commons.jexl2.MapContext;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        NelderMead nm = new NelderMead("(x1-2)^4+(x1-2*x2)^2");
        MapContext mc = new MapContext();
        mc.set("x1", 2);
        mc.set("x2", 1);
        System.out.println(nm.funcEval(mc));

    }

}

当我运行测试用例时,会导致以下错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
    at NelderMead.funcEval(NelderMead.java:33)
    at Test.main(Test.java:14)

我无法理解为什么它不能转为Double?

PS

Here是evaluate()函数的javadoc。

2 个答案:

答案 0 :(得分:1)

此测试用例应模仿您的问题

package com.sg2net.test;

public class TestCast {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestCast tc= new TestCast();
        tc.funcEval();
    }

    public double funcEval(){
        return ((Long) eval()).doubleValue();
    }   

    private Object eval() {
        return new Long(1);
    }

}

它运行没有问题。您发布的代码是否为您提供例外的代码?

这里是您使用Long修改的代码

import org.apache.commons.jexl2.*;

public class NelderMead {
    // контсанты
    private static int      M = 3;
    private static double   E = 0.005;
    private static double   A = 1.000;
    private static double   B = 0.500;
    private static double   Y = 2.000;

    // переменные
    private JexlEngine jexl;
    private Expression func;
    private String funcString = "";
    private MapContext[] iterations;

    public NelderMead(){
        this.jexl = new JexlEngine();
    }

    public NelderMead(String funcString){
        this.jexl = new JexlEngine();
        this.setFunc(funcString);
    }


    public void setFunc(String funcString){
        this.funcString = funcString;
        this.func = this.jexl.createExpression(funcString);
    }

    public double funcEval(MapContext args){
    return ((Long) this.func.evaluate(args)).doubleValue();

    }

    public boolean checkCriterian(){
        return true;
    }
}

它运行没有问题。 evaluate函数返回一个Long,它是一个Object。 evaluate函数可以返回任何类,因为Object是Java中的根类。

答案 1 :(得分:0)

方法的返回类型为double为什么要将return语句中的结果转换为Long