deliveredtechnologies / rulebook

100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
http://www.deliveredtechnologies.com
Apache License 2.0
721 stars 124 forks source link

Why not scanning subpackages #150

Closed Panthro closed 5 years ago

Panthro commented 5 years ago

Currently (v0.11) the code of RuleBookRunner has this code:

  protected List<Class<?>> getPojoRules() {
    Reflections reflections = new Reflections(_package);

    List<Class<?>> rules = reflections
        .getTypesAnnotatedWith(com.deliveredtechnologies.rulebook.annotation.Rule.class).stream()
        .filter(rule -> _package.equals(rule.getPackage().getName())) // Search only within package, not subpackages
        .filter(rule -> rule.getAnnotatedSuperclass() != null) // Include classes only, exclude interfaces, etc.
        .collect(Collectors.toList());

    rules.sort(comparingInt(aClass ->
        getAnnotation(com.deliveredtechnologies.rulebook.annotation.Rule.class, aClass).order()));

    return rules;
  }
}

Why are the scanning limited to the exact same package?

That's not an expected behaviour, and it takes a lot of time (debugging) to understand why your rule rule is not being loaded.

I know is probably for performance, not to load all classes at startup time, but wouldn't it be better just to recommend people to have rules in a separate package?

Thanks

Clayton7510 commented 5 years ago

I think the thought was that subpackage would be considered a separate package, and, therefore would not be loaded. No reason why subpackages couldn't be scanned, tho. Perhaps default behavior could be to scan only the package specified with an option to scan subpackages?

Panthro commented 5 years ago

I would expect the default behaviour to scan sub packages, just like a spring @ComponentScan would do, with the option to disable subpackage scanning.

I believe is harder to find the issue of NOT SCANNING then scanning too much :-)

Clayton7510 commented 5 years ago

Fair point. Agreed. Subpackages should be scanned by default with an option to disable.

Clayton7510 commented 5 years ago

Updated in 0.12-SNAPSHOT.