# expr-engine **Repository Path**: zhg101/expr-engine ## Basic Information - **Project Name**: expr-engine - **Description**: 基于类sql的表达式引擎,可判断where后的逻辑 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-06-02 - **Last Updated**: 2024-06-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: 规则引擎, sql表达式引擎 ## README # expr-engine #### 介绍 基于类sql的表达式引擎,可判断where后的逻辑 #### 使用说明 表达式引擎支持的运算符: - 关系运算符:=,!=,>,>=,<,<=,in,is null,is not null,startWith,endWith,contains,notContains - 逻辑运算符:and ,or,not(逻辑与或非) 支持组合与优先级: - not in可通过逻辑运算符not+关系运算符in实现 - is not null也可通过逻辑运算符not+关系运算符is null实现 - 支持(),优先级从左到右,()里面的优先,()与()之间,也是从左到右,符合正常数学层面理解的优先级 表达式书写要求: - 字符串必要使用"'"开始,"'"结束 - startWith,endWith,contains,notContains不区分大小写 - in后面必须带括号 - and、or、not、in、is null、is not null左右两边至少要有一个空格,is null、is not null中的空格至少一个,多空格无影响 - and、or、not、in、is null、is not null不区分大小写 **使用示例:** ``` // 测试 startWith endWith Map map= new HashMap<>(); map.put("a", "123456"); map.put("b", "5"); map.put("c", "2"); map.put("d", null); Expression exp= ExpressionParser.fromString("c endWith '2' and (a startWith '123' OR b != '5')"); boolean b = exp.interpret(map); System.out.println("exp match result [true]:" + b); // 测试 in map= new HashMap<>(); map.put("a", 1); Expression exp2 = ExpressionParser.fromString("a in (1,2,3)"); b = exp2.interpret(map); System.out.println("exp2 match result [true]:" + b); // 测试 contains map= new HashMap<>(); map.put("a", 1); map.put("b", "1zhanf21jkd21"); Expression exp3 = ExpressionParser.fromString("(a in (6,2,3) and b contains '214j') OR (a = 3 OR b != '1')"); b = exp3.interpret(map); System.out.println("exp3 match result [true]:" + b); ``` #### 参与贡献 1. 所有代码均来自于 https://github.com/wujiuye/jexpr-engine (感谢),本代码只做了部分调整,增加了startWith,endWith,contains,notContains的支持。 2. 做记录提交gitee,希望可以帮到更多的人或者欢迎指教。