evaluate java_用Java进行动态公式处理

这篇博客介绍了如何使用Java进行动态公式处理。通过`ExpressionUtil`类,作者展示了如何根据实体类和JPA注解获取表名,进而从数据库查询公式,并对List中的每个对象计算公式值。文章还详细讲解了`workOutListBean`方法,该方法处理包含公式的bean列表,以及处理单个Map对象的辅助方法。此外,还包括了条件判断和表达式计算的相关逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*** Created by Teddy on 2017/10/23.*/

public classExpressionUtil {/*** 计算List配置了公式的值

*@parambeanList

*@return

*/

public static List workOutListBean(List extends BaseEntity>beanList){if(CollectionUtils.isEmpty(beanList)){returnCollections.EMPTY_LIST;

}else{

Class extends BaseEntity> beanClass = beanList.get(0).getClass();

Table table= beanClass.getAnnotation(Table.class);//在实体中通过JPA注解获取表名

String tableCode =table.name();if(StringUtil.isEmpty(tableCode)){

System.out.println("无法计算,该bean没有表名信息");

}

List mapList =BeanUtil.convertBeansToMaps(beanList);

FormulaService formulaService= SpringContext.getBean(FormulaService.class);

List formulas =formulaService.queryByTableCode(tableCode);for(Map map : mapList) {

JEP jep=getJEP(map);for(Formula formula : formulas) {

String conditionExpr=formula.getConditionExpr();

String formulaExpr=formula.getFormulaExpr();

String evaluate=formula.getEvaluate();

workOutKey(jep,map,conditionExpr,formulaExpr,evaluate);

}

}returnmapList;

}

}/*** 非动态配置公式方式

*@parammapList

*@paramconditionExpr

*@paramformulaExpr

*@paramevaluate*/

public static void workOutListMap(ListmapList,String conditionExpr,String formulaExpr, String evaluate){for(Map map : mapList) {

JEP jep=getJEP(map);

workOutKey(jep,map,conditionExpr,formulaExpr,evaluate);

}

}/*** 计算出表达式并填充

*@paramjep

*@parammap

*@paramconditionExpr

*@paramformulaExpr

*@paramevaluate*/

private static voidworkOutKey(JEP jep,Map map,String conditionExpr, String formulaExpr, String evaluate){//如果没有条件

if(StringUtil.isEmpty(conditionExpr)){

map.put(evaluate,workOutSingle(jep,formulaExpr));//如果有条件 且条件为true

}else if(workOutBool(jep,conditionExpr)){

map.put(evaluate,workOutSingle(jep,formulaExpr));

}

}/*** 判断条件表达式

*@paramjep

*@paramexpression

*@return

*/

private static booleanworkOutBool(JEP jep,String expression){return (Double)workOutSingle(jep,expression) > 0;

}/*** 计算表达式的值

*@paramjep

*@paramexpression

*@return

*/

private staticObject workOutSingle(JEP jep,String expression){

Object result= null;try { //执行

Node parse =jep.parse(expression);

result=jep.evaluate(parse);

}catch(ParseException e) {throw new BaseRunTimeException("公式表达式解析失败",e);

}if(result == null){throw new BaseRunTimeException("公式表达式解析失败");

}returnresult;

}/*** 获取填充好变量的JEP对象

*@paramparam

*@return

*/

private staticJEP getJEP(Map param){

JEP jep= newJEP();

Set set =param.entrySet();for(Map.Entry entry : set) {

Object entryValue=entry.getValue();

String entryKey=(String) entry.getKey();

jep.addVariable(entryKey, entryValue);

}returnjep;

}

}

Java 中可以使用数学表达式解析库来验证公式是否书写正确,例如使用 Apache Commons Math 库中的 ExpressionParser 类。下面是一个简单的示例: ```java import org.apache.commons.math3.analysis.function.Exp; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sqrt; import org.apache.commons.math3.analysis.function.Tan; import org.apache.commons.math3.analysis.parser.Expression; import org.apache.commons.math3.analysis.parser.ExpressionParser; public class FormulaValidator { public static boolean validate(String formula) { ExpressionParser parser = new ExpressionParser(); Expression expression = parser.parse(formula); double x = 1.0; // 可以设置变量的值进行计算验证 double result = expression.evaluate(x); return !Double.isNaN(result) && !Double.isInfinite(result); // 判断计算结果是否合法 } public static void main(String[] args) { String formula1 = "sin(x) + cos(x)"; // 正确的公式 String formula2 = "sqrt(x) - 2"; // 错误的公式,sqrt 函数参数不能为负数 String formula3 = "tan(x) / (1 - cos(x))"; // 错误的公式,除数不能为 0 System.out.println(validate(formula1)); // true System.out.println(validate(formula2)); // false System.out.println(validate(formula3)); // false } } ``` 在上面的示例中,我们使用了数学表达式解析库中的 ExpressionParser 类来解析公式,并且可以通过 evaluate 方法计算公式的值。在计算之前,我们可以设置变量的值进行验证,例如上面示例中的 x 变量。最后,我们还需要判断计算结果是否合法,例如不能为 NaN 或 Infinity。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值