java可配置公式实现
表达式语言引擎:Apache Commons JEXL http://panyongzheng.iteye.com/blog/1811260
一种基于 JEP 和可配置公式实现用户自定义字段的解决方案 http://www.ibm.com/developerworks/cn/java/j-lo-jep/
java处理字符串公式运算 http://www.cnblogs.com/lmtoo/archive/2013/04/26/formula.html
java 使用eval.jar解析公式 http://blog.csdn.net/xuhuanchao/article/details/5536208
java实现字符串转换成可执行代码
1. http://wiselyman.iteye.com/blog/1677444
2. http://blog.5ibc.net/p/51238.html
使用commons的jexl可实现将字符串变成可执行代码的功能,我写了一个类来封装这个功能:
调用方式:
java 中使用jexl进行表达式判断http://hi.baidu.com/leezuu/item/2c98397843284a3c6e29f653
使用el在jsp中很方便,那么在java程序中如何实现表达式判断呢,jexl是个不错的选择
结果打印如下
年龄在30岁以上,并且身上有超过100元现金? false
左腿上有超过2500根汗毛? true
数学公式:
1 2 3 4 5 6 7 8 9 10 11 12 |
JexlEngine jexl = new JexlEngine(); jexl.setCache(512);//有何用 jexl.setLenient(false);//什么意思 jexl.setSilent(false); String calculate = "(G1 + G2)/2"; Expression e =jexl.createExpression(calculate); // populate the context JexlContext context = new MapContext(); context.set("G1", "3"); context.set("G2", "5"); Object res = e.evaluate(context);//silent为false时evaluate方法会抛异常,为true不抛异常但res为null |