Recently, our business requires the configurable calculation for delivery cost.I searched resolutions from net, the Easy Rule is recommended. I studied it, and chose MVELRule. But it executes unexpectedly.
mycode:
int price = 10;
Rule rule = new MVELRule()
.name("rule engine test")
.description("rule engine test")
.when("6 > 5")
.then("price = 100;");
Facts facts = new Facts();
facts.put("price", price);
Rules rules = new Rules();
rules.register(rule);
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, facts);
System.out.println(facts); // output: [Fact{name='price', value=10}]
The varible price
did not change.I changed the type from int to Integer, but it makes no differences.
I tried putting the varible in map or the concrete pojo, and it worked. The code goes:
Rule rule = new MVELRule()
.name("rule engine test")
.description("rule engine test")
.when("6 > 5")
.then("map.price = 100;");
Facts facts = new Facts();
HashMap<String, Integer> map = new HashMap<>();
map.put("price", 10);
facts.put("map", map);
Rules rules = new Rules();
rules.register(rule);
RulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.fire(rules, facts);
System.out.println(facts); // output: [Fact{name='map', value={price=100}}]
Although the latter worked fine, the expression is more complicated. So, I want to know why the former didn't work, and what should I do if I want to insist the former.