gpt4 book ai didi

java - 如何在 easy-rules 中编写规则,该规则接受字符串数组并使用 contains 检查事实字符串?

转载 作者:行者123 更新时间:2023-12-02 09:54:55 25 4
gpt4 key购买 nike

我正在使用表达式语言支持使用 easy-rules 以 json 格式编写规则。

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

2条规则

  1. 部门包含“gas”&&(标题包含“主管”|| 标题包含“经理”)

  2. dept 包含任何 ["gas"、"ges"、"ips"、"csa"、"sales - 云续订"] 值

注意:dept =“SALES - CLOUD RENEWAL”或“SALES US CLOUD RENEWAL”

用户角色规则.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 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", “销售-云续约”]值(value)观。

示例部门值为“SALES - CLOUD RENEWAL”或“SALES US CLOUD RENEWAL”

第二条规则的异常(exception)

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

您还可以查看the MVEL foreach documentation

关于java - 如何在 easy-rules 中编写规则,该规则接受字符串数组并使用 contains 检查事实字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com