I am using JSON descriptor
to load rules using easy-rules and I want to use variables in easy rules actions
. For example I have a set of rules where I define rule id, name, description as given below
[
{
"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\\\"]}\"]);"
]
}
]
Are the following 2 things possible here
-> Use rule name
, description
inside 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\":\"+name+\", \"desc\": \"+description+\", \"impact\":\"low\", \"count\":\"Using \"+user.getTaskDetail().getConnBucketData().get(\"Oracle\")+\" connector type\",\"id\":1, \"extra_detail\":\"{\\\"impacted_connectors\\\":[\\\"Oracle\\\"]}\"]);"
]
}
]
...
-> Use a variable under actions?
...
"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\\\"]}\"]);"
]
...
Update
Here I am initializing 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 +
'}';
}
}
MVELRuleFactory
class – Halley