如何在易用规则中编写规则,该规则接受字符串数组并使用包含检查事实字符串?

时间:2019-05-09 17:45:48

标签: java groovy easy-rules

我正在使用expression-language-support使用easy-rules以json格式编写规则。

MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader())
Rules rules = ruleFactory.createRules(new FileReader("user-role-rule.json"))

2条规则

  1. 部门包含“ gcs”和&(标题包含“导演” ||标题包含“经理”)

  2. 部门包含[“ gcs”,“ ges”,“ ips”,“ csa”,“销售-云更新”]值中的任何一个

注意:dept =“销售-云更新”或“销售美国云更新”

user-role-rule.json

[
    {
        "name": "account",
        "description": "User in GCS department having either Director or Manager title",
        "priority": 1,
        "condition": "user.getDept().toLowerCase().contains(\"gcs\") && (user.getTitle().toLowerCase().contains(\"director\") || user.getTitle().toLowerCase().contains(\"manager\"))",
        "actions": [
          "user.setRole(\"account\");"
        ]
  },
  {
    "name": "account_common",
    "description": "User in CSM, IPS, CSA, SALES - CLOUD ENTERPRISE or GES department irrespective of any title",
    "priority": 1,
    "condition": "for (String dep in [\"gcs\",\"ges\",\"ips\",\"csa\",\"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}",
    "actions": [
      "user.setRole(\"account\");"
    ]
  }
]

用户pojo类

class User {
    String userId
    String dept
    String title
    List<String> role

    User(String userId, String dept, String title) {
        this.userId = userId
        this.dept = dept
        this.title = title
        this.role = new ArrayList<String>()
    }
    //..ommitting getter setters
}

这里名称为“ account”的第一条规则可以正常工作,但是在第二条规则中,我要使用字符串检查列表组成的列表,它们分别位于[“ gcs”,“ ges”,“ ips”,“ csa”, “销售-云续订”]值。

部门的示例值为“销售-云更新”或“销售美国云更新”

第二条规则中的异常

Exception in thread "main" [Error: expected : in foreach]
[Near : {... es - cloud renewal"]) {user.getDept().toLowerCase( ....}]
                                 ^

1 个答案:

答案 0 :(得分:1)

由于错误状态,您应该在:中使用冒号foreach而不是in。只需编写第二条规则的条件,如下所示:

"condition": "for (String dep : [\"gcs\", ..., \"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}"

您还可以选中the MVEL foreach documentation