由于需求缘故,笔者最近开发了一个IDEA的代码扫描插件,在这过程中,发现规则匹配虽通过设计模式实现,但发现规则的代码大多相似,可以说重复,除了配置不同。因此想抽离成配置方式扩展功能,而使用规则引擎感觉又太重,这就需要一个条件表达式匹配引擎,这个简单的表达式引擎由此而来。
实现思路参考自:https://www.codenong.com/20763189/。此文章实现的表达式匹配较为粗糙,但原理可借鉴。
笔者开源的jexpr借鉴这篇文章的实现原理,并优化了表达式的写法,扩展实现了sql where所支持的表达式,支持"()"优先级,虽然简单,但也算一个比较完善且轻量的条件表达式引擎。
GitHub:https://github.com/wujiuye/jexpr-engine (帮忙点颗小星星哟)
实现原理无非就是利用递归与回溯算法,再加上栈数据结构实现优先级。
表达式引擎支持的运算符:
支持组合与优先级:
表达式书写要求:
关系运算符简单使用:
a='jexpn'
a!='jexpn'
a>0
a>=0
a<1
a<=1
a in (1,2,3)
a in (1.0,2.0,3.0)
a in ('java','expr','engine')
a is null
a is not null
关系运算符与逻辑运算符混合使用:
a>0 and a<10
a<10 or a>100
not a=100
a>0 and not a=10
a>0 and a<100 and c=1 or d=0
加入优先级的复杂使用:
a='123' AND b!='123' OR (c='aa' AND d='ff')
(a>0 and a<10) or (b>10 or b<10) or not (c<0 and c>-100)
(a>0 and a<12) or (b>10 or b<10) or not (c<0 and c>-100)
(a>0 and a<10) or b=10 or not (c<0 and c>-100)
(a>0 and a<10) or (b>10 or b<10) or not (c<0 and c>-10)
使用方式:
/**
* @author wujiuye
*/
public class ExpressionParserTest {
public static void main(String[] args) {
// 解析表达式
Expression expr = ExpressionParser.fromString("(a>0 and a<10) or (b>10 or b<10) or not (c<0 and c>-100)");
// 设置变量值
Map<String,Object> bindings = new HashMap<>();
bindings.put("a", 11);
bindings.put("b", 10);
bindings.put("c", -10);
// 匹配
boolean triggered = expr.interpret(bindings);
System.out.println("匹配结果:" + triggered);
}
}