我想写一个条件来评估事实中的任何设置值是否满足条件。
这是我的规则定义:
public void evaluateRule(Facts facts, String ruleNum) {
SpRuleDefinition spRuleDefinition = spRuleDefinitionService.findByRuleNum(ruleNum);
System.out.println("Invoke Action : " + spRuleDefinition.getActionNum());
facts.put("action", "ACTION1");
MVELRule rule = new MVELRule()
.name("Rule1")
.description("My Rule")
.priority(1)
.when("shipment.orgNum=='ORG1' && (shipment.fromAddress.country=='IN' || shipment.toAddress.country=='IN') && shipment.shipmentLines.itemDetail.active==false")
.then("shipment.setOutcome(action);");
Rules rules = new Rules();
rules.register(rule);
//Fire rules on known facts
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, facts);
}
我传递的输入可能是这样的:
{“ orgNum”:“ ORG1”,“ fromAddress”:{“ country”:“ SGP”},“ shipmentLines”:[{“ itemDetail”:{“ active”:true,“ countryOfOrigin”:“ IN” },“ itemNum”:“ I1”,“ quantity”:10},{“ itemDetail”:{“ active”:false,“ countryOfOrigin”:“ US”},“ itemNum”:“ I2”,“ quantity”: 1}],“ toAddress”:{“ country”:“ IN”}}
我想评估是否有任何一条装运线的itemDetail的活动标志设置为false。 上面的规则失败,但有以下例外:
org.mvel2.PropertyAccessException: [Error: could not access: itemDetail; in class: java.util.HashSet]
[Near : {... s.country=='IN') && shipment.shipmentLines.itemDet ....}
]
答案 0 :(得分:0)
这是MVEL问题,而不是EasyRules问题。
我想评估是否有任何一条装运线的itemDetail的活动标志设置为false。
您可以定义一个对集合进行迭代并检查条件的函数。这是一个示例:
@Test
public void testConditionOnSet() {
Set<ItemDetail> itemDetailSet = new HashSet<>();
itemDetailSet.add(new ItemDetail(false));
itemDetailSet.add(new ItemDetail(false));
itemDetailSet.add(new ItemDetail(true));
String condition = "active = false;\n" +
"foreach (itemDetail : itemDetails) {\n" +
" active = active && itemDetail.active;\n" +
"}\n" +
"System.out.println(\"Is there a non active itemDetail?: \" + !active);";
Map facts = new HashMap();
facts.put("itemDetails", itemDetailSet);
Object result = MVEL.eval(condition, facts);
}
static class ItemDetail {
private boolean active;
public ItemDetail(boolean active) {
this.active = active;
}
public boolean isActive() {
return active;
}
}
此示例打印:Is there a non active itemDetail? true
。
希望这会有所帮助。