j-easy / easy-rules

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

RuleListener is not triggered #369

Open manjuj123 opened 2 years ago

manjuj123 commented 2 years ago

Hello,

I am new to easy rules. I have written few rules that are working as expected. I have implemented a RuleListener in which I would like to add some extra context necessary for the next rule. However the rule listener is not getting triggered.

Below is the skeleton of this listener:

public class PositionalStrengthListener implements RuleListener {
    private static Logger logger = LoggerFactory.getLogger(PositionalStrengthListener.class);

    public void onSuccess(Rule rule, Facts facts) {
        logger.debug("OnSuccess: PositionalStrengthListener");
    }
}

I haven't added @Override annotation as I am running in to compiler error method does not override or implement a method from a supertype on eclipse. My understanding is that the OnSuccess method should still work from this subclass. I am not sure if the listener is not getting triggered because of this missing @Override.

Please let me know if I am missing anything else. Thanks.

chhil commented 2 years ago

If you are getting compiler error you should check your classpath to make sure the right jars are on it.

I used gradle to get the jars image

Compiles and runs fine, the breakpoints I set in the class get hit.

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;

public class MyRuleListener implements RuleListener {

    public MyRuleListener() {

    }

    @Override
    public void onSuccess(Rule rule, Facts facts) {

        RuleListener.super.onSuccess(rule, facts);
    }

    @Override
    public void beforeExecute(Rule rule, Facts facts) {

        RuleListener.super.beforeExecute(rule, facts);
    }
}
manjuj123 commented 2 years ago

Thank you @chhil. I compared your jars in my Maven dependencies and there are only few jars that are missing namely those related to MVEL and snakeyaml*. As I don't use MVEL, do I really need those jars? Which IDE do you use? I have seen posts on StackOverflow that this issue might be related to Eclipse and hence curiout.

chhil commented 2 years ago

My gradle file has

implementation group: 'org.jeasy', name: 'easy-rules-core', version: '4.1.0'
// https://mvnrepository.com/artifact/org.jeasy/easy-rules-support
implementation group: 'org.jeasy', name: 'easy-rules-support', version: '4.1.0'
// https://mvnrepository.com/artifact/org.jeasy/easy-rules-mvel
implementation group: 'org.jeasy', name: 'easy-rules-mvel', version: '4.1.0'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
//testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.32'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.32'
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.7'

Which IDE do you use?

I use eclipse.

As I don't use MVEL, do I really need those jars?

I would assume you don't need MVEL jars, but I am a couple of days old using jeasy so don't know. RuleListener that you are having problems with is part of easy-rules-core-4.1.0.jar snakeyaml is used for parsing yaml rules files, so its probably not needed.

When I remove the following from gradle, RuleListener does not show compilation errors.

implementation group: 'org.jeasy', name: 'easy-rules-mvel', version: '4.1.0'

The following brings in snakeyaml (for yaml rule file) jackson (for json rule file)

implementation group: 'org.jeasy', name: 'easy-rules-support', version: '4.1.0'
manjuj123 commented 2 years ago

Thanks again @chhil. I also use eclipse. I have snakeyaml now in my dependencies. Version: 2021-09 (4.21.0) Build id: 20210910-1417. I use JDK 10. I also don't use logback in my dependencies as I don't use it. I am still getting this compiler error. Would you mind letting me know which version of Eclipse do you use? When I use @Override annotation for OnSuccess, I get the below error. This error is not unique to OnSuccess. I get the same error for BeforeExecute/OnFailure as well.

The method beforeExecute(Rule, Facts) of type PositionalStrengthListener must override or implement a supertype method PositionalStrengthListener.java /Everest/src/main/java/com/blueprint/rules line 18 Java Problem

chhil commented 2 years ago

Logback is not needed.

Compiler error means your classpath is not setup correctly.

I tried java 8 and java 11, both worked fine.

Eclipse version image

In eclipse, see where the ctrl+click on your RuleListener takes you. See your imports, to check if its from the correct package.

manjuj123 commented 2 years ago

Thanks. What is interesting to me is that I am able to run rules before adding this listener class without any issues. Only when I introduced the listener, I am running into this issue. I did Cmd+Click (I am using Mac) and it opens the RuleListener from the jar just fine. I am even able to compile the MyRuleListener.java if I comment @Override annotation. If that's the case, I am not sure how it can be a classpath issue. I am unsure what is it that I am missing here.