首页 > 解决方案 > 基于 JSON 的 Java 规则评估器

问题描述

我想开发一个 JSON 规则模式和一个规则评估器,我可以在其中定义和/或/或用于属性比较的单个条件的条件。例如 - 如果 customerType 是 PRIMARY 并且 locale 是 en-US,则以下规则将评估为 true。我计划用 Java 编写我的自定义规则评估器,它将规则 JSON 作为输入以及请求参数,并根据其匹配与否评估为真或假。我保持我的模式通用并为扩展开放。就像如果你想在将来支持“OR”条件,或者如果你想在将来支持像 ">=" 这样的运算符,你可以通过扩展你的规则评估器来做到这一点。有没有更好的方法来做到这一点?我的架构足够灵活吗?

"rule": {
            "type": "CompositeRule",
            "allOf": [ #In future, we can also support "anyOf" if required
              {
                "type": "SimpleRule",
                "attribute": "customerType",
                "operator": "==", #In future, we can also support other operators like ">=" or "<=" etc.
                "value": "PRIMARY" #Value can be a number, string or boolean
              },
              {
                "type": "SimpleRule",
                "attribute": "locale",
                "operator": "==",
                "value": "en-US"
              }
            ]
        }

标签: javarulesrule-engine

解决方案


推荐阅读