JavaDemo——表达式处理Expression4j

  • Post author:
  • Post category:java


Expression4j网站:

http://expression4j.sourceforge.net/

下载jar包

1.简单Demo:

/**
 * 
 */
package test.expression4j;

import fr.expression4j.core.Expression;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;

/**
 * @author XWF
 *
 */
public class TestSimple {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Expression exp = ExpressionFactory.createExpression("f()=2.4e-2*1000");//不能有空格
			double val = exp.evaluate(null).getRealValue();
			System.out.println("exp name:" + exp.getName());
			System.out.println("exp:" + exp);
			System.out.println("result:" + val);
			
			Expression exp2 = ExpressionFactory.createExpression("func(x,y,z)=x+y*z");
			Parameters params = ExpressionFactory.createParameters();
			params.addParameter("x", NumberFactory.createReal(1));
			params.addParameter("y", NumberFactory.createReal(2));
			params.addParameter("z", NumberFactory.createReal(3));
			double val2 = exp2.evaluate(params).getRealValue();
			System.out.println("exp2 name:" + exp2.getName());
			System.out.println("exp2:" + exp2);
			System.out.println("exp2 result:" + val2);
		} catch (ParsingException e) {
			e.printStackTrace();
		} catch (EvalException e) {
			e.printStackTrace();
		}
	}

}

结果:

2.组合Demo:

/**
 * 
 */
package test.expression4j;

import fr.expression4j.core.Expression;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;

/**
 * @author XWF
 *
 */
public class TestAgregated {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Expression exp1 = ExpressionFactory.createExpression("f1(x)=x*x");
			Expression exp2 = ExpressionFactory.createExpression("f2(b,a,c)=f1(b)-4*a*c");
			Parameters params = ExpressionFactory.createParameters();
			params.addParameter("b", NumberFactory.createReal(4));
			params.addParameter("a", NumberFactory.createReal(2));
			params.addParameter("c", NumberFactory.createReal(2));
			double val = exp2.evaluate(params).getRealValue();
			System.out.println("exp2 name:" + exp2.getName());
			System.out.println("exp2:" + exp2);
			System.out.println("exp2 result:" + val);
		} catch (ParsingException e) {
			e.printStackTrace();
		} catch (EvalException e) {
			e.printStackTrace();
		}
	}

}

结果:

3.预定义函数Demo:

/**
 * 
 */
package test.expression4j;

import fr.expression4j.core.Expression;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.factory.ExpressionFactory;

/**
 * @author XWF
 *
 */
public class TestPredefined {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		abs(x)		绝对值
//		acos(x)		反余弦值
//		asin(x)		反正弦值
//		atan(x)		反正切
//		ceil(x)		向上取整
//		cos(x)		余弦
//		cosh(x)		双曲余弦
//		exp(x)		e的x次幂
//		floor(x)	向下取整
//		log(x)		对数
//		random()	随机数
//		sin(x)		正弦
//		sinh(x)		双曲正弦
//		sqrt(x)		平方根
//		tan(x)		正切
		try {
			Expression exp1 = ExpressionFactory.createExpression("f()=random()");
			double val = exp1.evaluate(null).getRealValue();
			System.out.println("result:" + val);
		} catch (ParsingException e) {
			e.printStackTrace();
		} catch (EvalException e) {
			e.printStackTrace();
		}
	}

}

结果:

4.用户定义函数Demo:

/**
 * 
 */
package test.expression4j;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import fr.expression4j.basic.MathematicalElement;
import fr.expression4j.basic.MathematicalException;
import fr.expression4j.basic.OperatorManager;
import fr.expression4j.core.Catalog;
import fr.expression4j.core.Expression;
import fr.expression4j.core.Parameters;
import fr.expression4j.core.exception.EvalException;
import fr.expression4j.core.exception.ParametersException;
import fr.expression4j.core.exception.ParsingException;
import fr.expression4j.core.predefine.AbstractFunction;
import fr.expression4j.factory.ExpressionFactory;
import fr.expression4j.factory.NumberFactory;
import fr.expression4j.util.ParameterUtil;

/**
 * @author XWF
 *
 */
public class TestUserDefine {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Catalog catalog = ExpressionFactory.getCatalog();
			catalog.addExpression(new MyFun());
			Expression exp = ExpressionFactory.createExpression("f()=abcd(13)");
			double val = exp.evaluate(null).getRealValue();
			System.out.println("result:" + val);
		} catch (ParsingException e) {
			e.printStackTrace();
		} catch (EvalException e) {
			e.printStackTrace();
		}
	}

}

class MyFun extends AbstractFunction {

	@Override
	public MathematicalElement evaluate(Parameters arg0) throws EvalException {
		try {
			double d = arg0.getParameter("x").getRealValue();//似乎只能是x一个
			return NumberFactory.createReal(d + 1);
		} catch (ParametersException e) {
			throw new EvalException("error");
		}
	}

	@Override
	public MathematicalElement evaluate(OperatorManager arg0, Parameters arg1) throws EvalException {
		return evaluate(arg1);
	}

	@Override
	public Catalog getCatalog() {
		return ExpressionFactory.getCatalog();
	}

	@Override
	public String getName() {
		return "abcd";//方法名
	}

	@Override
	public List getParameters() {
		return ParameterUtil.generateXParameters();
	}
	
}

结果:



版权声明:本文为FlyLikeButterfly原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。