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

Read Rules from multiple Json or Yaml Files #425

Open TheMohitShrivastava opened 3 months ago

TheMohitShrivastava commented 3 months ago

Hi,

How can MVELRuleFactory read rules from multiple files. Since we have a large ruleset we want to split them into multiple files so it can be managed easily.

I tried something like this

    MVELRuleFactory mvelRuleFactory = new MVELRuleFactory(new JsonRuleDefinitionReader());
    File dir = new File("./batch/src/main/resources/rules");
    log.info(dir.getAbsolutePath());
    File[] files = dir.listFiles((d, f) -> f.endsWith(".json"));
    Rules rules = null;
    for (File file : files) {
      if (rules == null) {
        rules = mvelRuleFactory.createRules(new FileReader(file));
      } else {
        Rules rules1 = mvelRuleFactory.createRules(new FileReader(file));
        rules.register(rules1);
      }
    }

but this fails with the below error

Exception in thread "main" java.lang.IllegalArgumentException: Rule 'org.jeasy.rules.api.Rules' is not annotated with 'org.jeasy.rules.annotation.Rule'
    at org.jeasy.rules.core.RuleDefinitionValidator.checkRuleClass(RuleDefinitionValidator.java:57)
    at org.jeasy.rules.core.RuleDefinitionValidator.validateRuleDefinition(RuleDefinitionValidator.java:49)
    at org.jeasy.rules.core.RuleProxy.asRule(RuleProxy.java:81)
    at org.jeasy.rules.api.Rules.register(Rules.java:84)

It seems rules.register() can only accept Java Rules & not MVEL Rules. Is there a way to read rules from multiple files.