动态规则表达式解析

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));
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值