如何在 easy-rules 中编写一个规则,该规则采用字符串数组并使用包含检查事实字符串?
How do I write a rule in easy-rules which takes an array of string and checks a fact string using contains?
我正在使用 expression-language-support 使用 easy-rules 以 json 格式编写规则。
MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader())
Rules rules = ruleFactory.createRules(new FileReader("user-role-rule.json"))
2 条规则
部门包含"gas" &&(标题包含"director" || 标题包含"manager")
dept 包含任何 ["gas"、"ges"、"ips"、"csa"、"sales - cloud renewal"] 值
注:部门="SALES - CLOUD RENEWAL"或"SALES US CLOUD RENEWAL"
user-role-rule.json
[
{
"name": "account",
"description": "User in gas department having either Director or Manager title",
"priority": 1,
"condition": "user.getDept().toLowerCase().contains(\"gas\") && (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 [\"gas\",\"ges\",\"ips\",\"csa\",\"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}",
"actions": [
"user.setRole(\"account\");"
]
}
]
用户 pojo class
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" 的第一条规则工作正常,但在第二条规则中,我想使用字符串列表检查部门属于 ["gas"、"ges" 中的任何一个, "ips"、"csa"、"sales - cloud renewal"] 值。
示例部门值为 "SALES - CLOUD RENEWAL" 或 "SALES US CLOUD RENEWAL"
第二条规则中的异常
Exception in thread "main" [Error: expected : in foreach]
[Near : {... es - cloud renewal"]) {user.getDept().toLowerCase( ....}]
^
如错误所述,您应该在 foreach
中使用冒号字符 :
而不是 in
。只写第二条规则的条件如下:
"condition": "for (String dep : [\"gcs\", ..., \"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}"
我正在使用 expression-language-support 使用 easy-rules 以 json 格式编写规则。
MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader())
Rules rules = ruleFactory.createRules(new FileReader("user-role-rule.json"))
2 条规则
部门包含"gas" &&(标题包含"director" || 标题包含"manager")
dept 包含任何 ["gas"、"ges"、"ips"、"csa"、"sales - cloud renewal"] 值
注:部门="SALES - CLOUD RENEWAL"或"SALES US CLOUD RENEWAL"
user-role-rule.json
[
{
"name": "account",
"description": "User in gas department having either Director or Manager title",
"priority": 1,
"condition": "user.getDept().toLowerCase().contains(\"gas\") && (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 [\"gas\",\"ges\",\"ips\",\"csa\",\"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}",
"actions": [
"user.setRole(\"account\");"
]
}
]
用户 pojo class
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" 的第一条规则工作正常,但在第二条规则中,我想使用字符串列表检查部门属于 ["gas"、"ges" 中的任何一个, "ips"、"csa"、"sales - cloud renewal"] 值。
示例部门值为 "SALES - CLOUD RENEWAL" 或 "SALES US CLOUD RENEWAL"
第二条规则中的异常
Exception in thread "main" [Error: expected : in foreach]
[Near : {... es - cloud renewal"]) {user.getDept().toLowerCase( ....}]
^
如错误所述,您应该在 foreach
中使用冒号字符 :
而不是 in
。只写第二条规则的条件如下:
"condition": "for (String dep : [\"gcs\", ..., \"sales - cloud renewal\"]) {user.getDept().toLowerCase().contains(dep)}"