import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 动态bool表达式解析器
*
*/
public class DynamicRuleParser {
private static final Map<String, String> operatorMap = new HashMap<>();
static {
operatorMap.put("AND", "&&");
operatorMap.put("OR", "||");
operatorMap.put("ge", ">=");
operatorMap.put("gt", ">");
operatorMap.put("eq", "==");
operatorMap.put("ne", "!=");
operatorMap.put("le", "<=");
operatorMap.put("lt", "<");
}
private static String parse(JSONObject jsonObject) {
if (jsonObject.containsKey("condition") && jsonObject.containsKey("rules")) {
String condition = jsonObject.getString("condition");
JSONArray array = JSONObject.parseArray(jsonObject.getString("rules"));
List<String> rules = new ArrayList<>();
for (Object object : array) {
JSONObject json = (JSONObject) object;
if (json.containsKey("operator")) {
String operator = json.getString("operator");
String rule = "(" + json.getString("leftValue") + " " + operator + " " + json.getString("rightValue") + ")";
rules.add(rule);
} else {
String rule = parse(json);
if (StrUtil.isNotBlank(rule)) {
rules.add(rule);
}
}
}
return "(" + String.join(operatorMap.get(condition), rules) + ")";
}
return StrUtil.EMPTY;
}
/**
* 解析规则字符串,转换成表达式形式
* 输入
* { "any": [
* { "all": [
* { "ge": ["A", 10] },
* { "eq": ["B", 20] }
* ]},
* { "lt": ["C", 30] },
* { "ne": ["D", 50] }
* ]}
*
* 输出:
* ( A >= 10 && B == 20 ) || ( C < 30 ) || ( D != 50 )
*
* @param rule 规则的json字符串形式
* @return 返回 bool 表达式
*/
public static String parse(String rule) {
JSONObject jsonpObject = JSONObject.parseObject(rule);
return parse(jsonpObject);
}
public static void main(String[] args) throws IOException {
String rule = "{\n" +
" \"condition\": \"AND\",\n" +
" \"rules\": [\n" +
" {\n" +
" \"leftValue\": \"rule1\",\n" +
" \"operator\": \">\",\n" +
" \"extraValue\": \"213\",\n" +
" \"rightValue\": \"11\",\n" +
" \"key\": \"7945e6adc28bc1952f04fdf7b78a1dda\"\n" +
" },\n" +
" {\n" +
" \"leftValue\": \"rule_group1_rule1\",\n" +
" \"operator\": \"==\",\n" +
" \"key\": \"de2d19a49893e2e2bffa2e310d2f0446\",\n" +
" \"rightValue\": \"1\",\n" +
" \"leftValueLabel\": \"测试Input组件字段11\"\n" +
" },\n" +
" {\n" +
" \"key\": \"301aa0670d52d581fc5deb9c8e0610d3\",\n" +
" \"condition\": \"OR\",\n" +
" \"rules\": [\n" +
" {\n" +
" \"key\": \"9f01e9a7c1fb9d5b888e995c8a0a265e\",\n" +
" \"ruleType\": \"options\",\n" +
" \"leftValue\": \"rule_group1_rule1\",\n" +
" \"leftValueLabel\": \"测试Input组件字段11\",\n" +
" \"rightValue\": \"12\",\n" +
" \"operator\": \"==\"\n" +
" },\n" +
" {\n" +
" \"key\": \"b6a9340ccab9998c2c760a3bd7b12eae\",\n" +
" \"ruleType\": \"options\",\n" +
" \"leftValue\": \"rule_group1_rule1\",\n" +
" \"leftValueLabel\": \"测试Input组件字段11\",\n" +
" \"rightValue\": \"222\",\n" +
" \"operator\": \"==\"\n" +
" },\n" +
" {\n" +
" \"key\": \"b32124b07fb082a49ef2a1248d6b0238\",\n" +
" \"condition\": \"AND\",\n" +
" \"rules\": [\n" +
" {\n" +
" \"key\": \"2ec556166844aaae58c4c369770d35f7\",\n" +
" \"ruleType\": \"options\",\n" +
" \"leftValue\": \"rule_group1_rule1\",\n" +
" \"leftValueLabel\": \"测试Input组件字段11\",\n" +
" \"rightValue\": \"33\",\n" +
" \"operator\": \"==\"\n" +
" },\n" +
" {\n" +
" \"key\": \"01b605ade1088f18a52258bdba9ea8a7\",\n" +
" \"ruleType\": \"options\",\n" +
" \"leftValue\": \"rule_group1_rule2\",\n" +
" \"leftValueLabel\": \"测试Input组件字段12\",\n" +
" \"rightValue\": \"3333\",\n" +
" \"operator\": \"==\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"key\": \"root\"\n" +
"}";
System.out.println(parse(rule));
}
}
动态规则表达式解析
最新推荐文章于 2024-04-30 17:28:01 发布