Ric*_*cky 6 java groovy rule-engine easy-rules
我正在使用easy-rulesJSON descriptor加载规则,并且我想在 easy Rules 中使用变量。例如,我有一组规则,其中定义了规则 id、名称、描述,如下所示actions
[
{
"id": 1,
"name": "Task using Oracle DB",
"description": "Updated comments to update connector",
"priority": 1,
"condition": "user.getTaskData().getTargetConnectorType().contains(\"Oracle\") || user.getTaskData().getSourceConnectorType().contains(\"Oracle\")",
"actions": [
"user.setRuleDetail([\"impacted_feature\":\"Task using Oracle DB\", \"desc\": \"Updated comments to update connector\", \"impact\":\"low\", \"count\":\"Using \"+user.getTaskDetail().getConnBucketData().get(\"Oracle\")+\" connector type\",\"id\":1, \"extra_detail\":\"{\\\"impacted_connectors\\\":[\\\"Oracle\\\"]}\"]);"
]
}
]
Run Code Online (Sandbox Code Playgroud)
这里可能有以下两件事
-> 使用规则name,description内部操作?
...
[
{
"id": 1,
"name": "Task using Oracle DB",
"description": "Updated comments to update connector",
"priority": 1,
"condition": "user.getTaskData().getTargetConnectorType().contains(\"Oracle\") || user.getTaskData().getSourceConnectorType().contains(\"Oracle\")",
"actions": [
"user.setRuleDetail([\"impacted_feature\":\"+name+\", \"desc\": \"+description+\", \"impact\":\"low\", \"count\":\"Using \"+user.getTaskDetail().getConnBucketData().get(\"Oracle\")+\" connector type\",\"id\":1, \"extra_detail\":\"{\\\"impacted_connectors\\\":[\\\"Oracle\\\"]}\"]);"
]
}
]
...
Run Code Online (Sandbox Code Playgroud)
-> 在操作下使用变量?
...
"actions": [
"def name = \"Task using Oracle DB\"",
"def desc = \"Updated comments to update connector\"",
"def connector = \"Oracle\"",
"user.setRuleDetail([\"impacted_feature\":\"+name+\", \"desc\": \"+desc+\", \"impact\":\"low\", \"count\":\"Using \"+user.getTaskDetail().getConnBucketData().get(\"Oracle\")+\" connector type\",\"id\":1, \"extra_detail\":\"{\\\"impacted_connectors\\\":[\\\"Oracle\\\"]}\"]);"
]
...
Run Code Online (Sandbox Code Playgroud)
更新
这里我正在初始化MVELRuleFactory
def computeRules(UserData userData) {
try {
Facts facts = new Facts()
facts.put("user", userData)
MVELRuleFactory ruleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader())
Rules rules = ruleFactory.createRules(new FileReader("conf/rules.json"))
//create a default rules engine and fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine()
rulesEngine.fire(rules, facts)
} catch(FileNotFoundException fnfe) {
_errorLogger.error("Error in #computeRules {}", fnfe)
} catch(Exception e) {
_errorLogger.error("Error in #computeRules {}", e)
}
return userData.getRuleDetail()
}
//UserData POJO
@CompileStatic
class UserData {
String orgKey
TaskData taskData
List<Map> ruleDetail
UserData(String orgKey, TaskData taskData) {
this.orgKey = orgKey
this.taskData = taskData
}
String getOrgKey() {
return orgKey
}
void setOrgKey(String orgKey) {
this.orgKey = orgKey
}
TaskData getTaskData() {
return taskData
}
void setTaskData(TaskData taskData) {
this.taskData = taskData
}
List<Map> getRuleDetail() {
return ruleDetail
}
void setRuleDetail(Map ruleData) {
if (this.ruleDetail == null)
this.ruleDetail = []
this.ruleDetail.add(ruleData)
}
@Override
public String toString() {
return "UserData{" +
"orgKey='" + orgKey + '\'' +
", taskData=" + taskData +
", ruleDetail=" + ruleDetail +
'}';
}
}
Run Code Online (Sandbox Code Playgroud)
您基本上是在尝试访问条件/操作内的规则信息,或者换句话说,使条件/操作了解规则。您可以使用规则侦听器来执行此操作,该侦听器在规则执行之前将规则作为事实,然后将其删除。这是一个简单的例子:
public class MyListener implements RuleListener {
@Override
public void beforeExecute(Rule rule, Facts facts) {
facts.put("rule", rule);
}
@Override
public void onSuccess(Rule rule, Facts facts) {
facts.remove("rule");
}
@Override
public void onFailure(Rule rule, Facts facts, Exception exception) {
facts.remove("rule");
}
// implement other methods if needed
}
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它:给出以下规则描述文件rule-aware-action.json:
[
{
"name": "rule aware action",
"description": "a rule where the action is aware of the rule",
"condition": "true",
"actions": [
"System.out.println(rule.name);"
]
}
]
Run Code Online (Sandbox Code Playgroud)
以下示例打印rule aware action:
import java.io.FileReader;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.jeasy.rules.support.reader.JsonRuleDefinitionReader;
public class RuleAwareActionExample {
public static void main(String[] args) throws Exception {
MVELRuleFactory mvelRuleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader());
Rules rules = mvelRuleFactory.createRules(new FileReader("rule-aware-action.json"));
DefaultRulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.registerRuleListener(new MyListener());
Facts facts = new Facts();
rulesEngine.fire(rules, facts);
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着该操作能够访问它所属的规则的名称。该示例展示了如何使操作了解其规则,但它也应该适用于条件。
| 归档时间: |
|
| 查看次数: |
3535 次 |
| 最近记录: |