j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.83k stars 1.04k forks source link

Migration From version 3 to Version 4 breaking the already working relavance rules #400

Open junaidwarsivd opened 1 year ago

junaidwarsivd commented 1 year ago

hi I've Followed the instruction mentioned in the migration guide but seems like the relevance rules are breaking. my question is do we need to change anything in the yaml files as well or this is just a bug in the library ? i've attached Example rule below which is working in version 3 but not in version 4

---
name: step1_danger_signs
description: danger_signs
priority: 1
condition: "!step1_contact_reason.isEmpty()"
actions:
  - "isRelevant = true"
ikoesun commented 1 year ago

3.2.0升级到4.1.0有BUG,替换使用RuleBuilder生成Rule。 Demo:

// Build a rule.
Rule rule = new RuleBuilder()
                .name("rule name")
                .when(new MVELCondition("condition", new ParserContext()))// "condition" like "x == 1" or "x >= 10 && x <= 100" or custom by your business.
                .then(facts -> facts.put(key, value))// add or update fact's key-value in facts.
                .build();

// Fire rules.
DefaultRulesEngine rulesEngine = new DefaultRulesEngine();// Only create once in program runtime.

Facts facts = new Facts();
facts.put("x", value);// "x" is define by rule .when() param "condition".
rulesEngine.fire(rules, facts);
Object obj = facts.get(key);
if (obj == null) {
    // not fire.
} else {
    // fire! obj = value which be define by rule .then().
}

Because of in org.jeasy.rules.mvel.MVELAction.class:

public void execute(Facts facts) {
    try {
          // Facts.asMap() retuen a new Map,but not update facts's fact after MVEL.executeExpression(). That is why you can got a result in version 3.2.0, but null in version 4.1.0. 目前只能使用@Rule注解自定义Rule或通过RuleBuilder生成Rule解决。
        MVEL.executeExpression(this.compiledExpression, facts.asMap());
    } catch (Exception var3) {
        LOGGER.error("Unable to evaluate expression: '" + this.expression + "' on facts: " + facts, var3);
        throw var3;
    }
}

Hoping this helps.