规则引擎VS使用Java自定义约束验证器

时间:2018-02-05 11:17:54

标签: java json hibernate validation

我正在评估是否为我的项目使用合适的架构和API,例如Java Custom Constraint Validator vs JRules,需要相当专业的帮助。 http://blog.athico.com/2007/08/jboss-drools-vs-ilog-jrules.html

业务要求是临床系统的Web表单基础提交系统,基本详情如下 i)1000个诊所

ii)共有20种诊所表格类型(登记,患者信息,药物信息,药物信息等)

iii)每个表格有大约10-30个中等到复杂的规则(即如果访客年龄超过12岁,身份证号码不能为空)

iv)每个表格大约有40个字段(名称,地址,位置等)

v)规则的条件一般在3-5

之内

vi)总共有300条规则,70%具有相同的相似性。请参阅下面的通用规则示例。

目前我使用简易规则概念和java自定义约束验证器来处理如下所示,我正在探索更多高级选项,如上面提到的规则引擎。

鉴于下面的Json格式有许多不同类型的规则场景,我应该考虑一个规则引擎来处理这个或java简单规则和java自定义约束验证器能够处理这个吗?

代码示例和更多信息如下

以下常见规则基于json格式,如下所示,否则将抛出错误

1)“visit.clientId”如果“visit.clientRevision”json字段不为null且“visit.clientInfo”json字段不为空,则json字段不能为空

2)“visitDetails”如果“visit”json对象不为null,则只能提交json对象

3)如果访客年龄超过12岁且“locationCode”在“10”到“1000”范围内,“visit.clientId”不能为空

4)如果访客年龄在12岁至21岁之间且“locationCode”为“1”和“2”

,则只允许提交“visit.clientId”值

5)如果给出“visitDetails.screeningInfo”.bmi,则“visitDetails.screeningInfo.height”不能为空

6)如果访客年龄在21岁至100岁之间且“locationCode”为“100”或“200”

,则只允许提交“visit.clientId”值
    {

        "refFormId": 3,
        "locationCode": "2",
        "visit": {
            "facilityCode": "2",
            "eventDate": "2017-11-01 15:07:06",
            "refServiceTypeId": 2,
            "clientRevision": 0,
            "clientId": 1,
            "serialNumber": "serial1"
        },
        "visitDetails": {
            "refDetailsID": 3,

            "screeningInfo": {
                "height": 100,
                "weight": 100,
                "bmi": 100,
                "screenLocation": 1,
                "screenLocationOther": "screenLocationOther",


                "healthMorbidityScope": [{
                        "refHealthMorbidifyScopeId": 1,
                        "otherDetails": "other1",
                        "refScopes": [2, 3, 4]
                    },
                    {
                        "refHealthMorbidifyScopeId": 2,
                        "otherDetails": "other2",
                        "refScopes": [1, 2, 3]
                    }
                ]

            }
        }
    }

以下关于Pojo类文件

的java自定义约束验证器的示例

示例Java代码1

规则要求 - 如果给出“visitDetails.screeningInfo.bmi”,则“visitDetails.screeningInfo.height”不能为空。

如果运行时json值未满足要求,则将抛出错误

@NotNullIfAnotherFieldHasValue.List({
        @NotNullIfAnotherFieldHasValue(conditionType = dictionary.REF_VALIDATOR_IF_DEPENDENT_FIELD_EQUAL_SOMETHING,    // checking type is whether notNullFieldName can be null
                conditionIfFieldNameA = "bmi", // if bmi given and not null then height is not allowed to be null
                conditionIfSelectedValueA = "dictionary.ANYTHING",
                notNullFieldName = "height", // field to be checked whether allowed to be null
                message = "{domain.constraint.notNullIfAnotherFieldHasValue.scenario1}") })     

示例java代码2

规则要求 - 仅当访客年龄介于12岁至21岁且“locationCode”为“1”和“2”时才允许提交visit.clientId值

如果运行时json值未满足要求,则将抛出错误

@AgeValidator.List({
        @AgeValidator(
                allowedFieldName = "clientId",     // checking for clientId
                conditionIfFieldName = "locationCode",   // locationCode must be range 1 and 2 as conditionIfSelectedValue
                conditionIfSelectedValue = [1,2],    
                checkingType = dictionary.REF_VALIDATOR_FIELD_IS_ALLOWED,   // checking type is whether value can be submitted
                allowedPatientAge = "12", allowedPatientAgeUpto = "21",   // age must be between 12 to 21 
                conditionType = LookupConstants.REF_AGE_IN_BETWEEN)     
        })              

0 个答案:

没有答案
相关问题