增加QLExpress支持

This commit is contained in:
Looly 2022-10-12 14:14:42 +08:00
parent c12102e89b
commit 896ef4f701
6 changed files with 78 additions and 11 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 AnnotationUtil类支持Lambda获取某注解属性值pr#827@Gitee
* 【core 】 CharUtil.isBlank添加Hangul Filler字符issue#I5UGSQ@Gitee
* 【poi 】 优化合并单元格读取issue#I5UJZ1@Gitee
* 【extra 】 增加QLExpress支持issue#2653@Github
### 🐞Bug修复
* 【poi 】 修复ExcelReader读取只有标题行报错问题issue#I5U1JA@Gitee

View File

@ -453,7 +453,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.20</version>
<version>5.3.23</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
@ -464,6 +464,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.3.0</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- 表达式引擎可选依赖 end -->
<dependency>
<groupId>org.apache.commons</groupId>

View File

@ -0,0 +1,38 @@
package cn.hutool.extra.expression.engine.qlexpress;
import cn.hutool.extra.expression.ExpressionEngine;
import cn.hutool.extra.expression.ExpressionException;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import java.util.Map;
/**
* QLExpress引擎封装<br>
* https://github.com/alibaba/QLExpress
*
* @author looly
* @since 5.8.9
*/
public class QLExpressEngine implements ExpressionEngine {
private final ExpressRunner engine;
/**
* 构造
*/
public QLExpressEngine() {
engine = new ExpressRunner();
}
@Override
public Object eval(final String expression, final Map<String, Object> context) {
final DefaultContext<String, Object> defaultContext = new DefaultContext<>();
defaultContext.putAll(context);
try {
return engine.execute(expression, defaultContext, null, true, false);
} catch (final Exception e) {
throw new ExpressionException(e);
}
}
}

View File

@ -0,0 +1,7 @@
/**
* QLExpress引擎封装<br>
* https://github.com/alibaba/QLExpress
*
* @author looly
*/
package cn.hutool.extra.expression.engine.qlexpress;

View File

@ -3,4 +3,5 @@ cn.hutool.extra.expression.engine.jexl.JexlEngine
cn.hutool.extra.expression.engine.mvel.MvelEngine
cn.hutool.extra.expression.engine.jfireel.JfireELEngine
cn.hutool.extra.expression.engine.spel.SpELEngine
cn.hutool.extra.expression.engine.rhino.RhinoEngine
cn.hutool.extra.expression.engine.rhino.RhinoEngine
cn.hutool.extra.expression.engine.qlexpress.QLExpressEngine

View File

@ -4,6 +4,7 @@ import cn.hutool.core.lang.Dict;
import cn.hutool.extra.expression.engine.jexl.JexlEngine;
import cn.hutool.extra.expression.engine.jfireel.JfireELEngine;
import cn.hutool.extra.expression.engine.mvel.MvelEngine;
import cn.hutool.extra.expression.engine.qlexpress.QLExpressEngine;
import cn.hutool.extra.expression.engine.rhino.RhinoEngine;
import cn.hutool.extra.expression.engine.spel.SpELEngine;
import org.junit.Assert;
@ -26,7 +27,7 @@ public class ExpressionUtilTest {
@Test
public void jexlTest(){
ExpressionEngine engine = new JexlEngine();
final ExpressionEngine engine = new JexlEngine();
final Dict dict = Dict.create()
.set("a", 100.3)
@ -38,18 +39,18 @@ public class ExpressionUtilTest {
@Test
public void jexlScriptTest(){
ExpressionEngine engine = new JexlEngine();
final ExpressionEngine engine = new JexlEngine();
String exps2="if(a>0){return 100;}";
Map<String,Object> map2=new HashMap<>();
final String exps2="if(a>0){return 100;}";
final Map<String,Object> map2=new HashMap<>();
map2.put("a", 1);
Object eval1 = engine.eval(exps2, map2);
final Object eval1 = engine.eval(exps2, map2);
Assert.assertEquals(100, eval1);
}
@Test
public void mvelTest(){
ExpressionEngine engine = new MvelEngine();
final ExpressionEngine engine = new MvelEngine();
final Dict dict = Dict.create()
.set("a", 100.3)
@ -61,7 +62,7 @@ public class ExpressionUtilTest {
@Test
public void jfireELTest(){
ExpressionEngine engine = new JfireELEngine();
final ExpressionEngine engine = new JfireELEngine();
final Dict dict = Dict.create()
.set("a", 100.3)
@ -73,7 +74,7 @@ public class ExpressionUtilTest {
@Test
public void spELTest(){
ExpressionEngine engine = new SpELEngine();
final ExpressionEngine engine = new SpELEngine();
final Dict dict = Dict.create()
.set("a", 100.3)
@ -85,7 +86,7 @@ public class ExpressionUtilTest {
@Test
public void rhinoTest(){
ExpressionEngine engine = new RhinoEngine();
final ExpressionEngine engine = new RhinoEngine();
final Dict dict = Dict.create()
.set("a", 100.3)
@ -95,4 +96,16 @@ public class ExpressionUtilTest {
Assert.assertEquals(-143.8, (double)eval, 0);
}
@Test
public void qlExpressTest(){
final ExpressionEngine engine = new QLExpressEngine();
final Dict dict = Dict.of()
.set("a", 100.3)
.set("b", 45)
.set("c", -199.100);
final Object eval = engine.eval("a-(b-c)", dict);
Assert.assertEquals(-143.8, (double)eval, 0);
}
}