Skip to content

defining rules engine listener

Mahmoud Ben Hassine edited this page May 17, 2020 · 4 revisions

You can listen to rules engine execution events through the RulesEngineListener API:

public interface RulesEngineListener {

    /**
     * Triggered before evaluating the rule set.
     * <strong>When this listener is used with a {@link InferenceRulesEngine},
     * this method will be triggered before the evaluation of each candidate rule
     * set in each iteration.</strong>
     *
     * @param rules to fire
     * @param facts present before firing rules
     */
    default void beforeEvaluate(Rules rules, Facts facts) { }

    /**
     * Triggered after executing the rule set
     * <strong>When this listener is used with a {@link InferenceRulesEngine},
     * this method will be triggered after the execution of each candidate rule
     * set in each iteration.</strong>
     *
     * @param rules fired
     * @param facts present after firing rules
     */
    default void afterExecute(Rules rules, Facts facts) { }
}

Unlike the RuleListener that is executed for each rule, the RulesEngineListener allows you to provide custom behavior before/after firing the entire rule set. To register your listener, use the following snippet:

DefaultRulesEngine rulesEngine = new DefaultRulesEngine();
rulesEngine.registerRulesEngineListener(myRulesEngineListener);

You can register as many listeners as you want, they will be executed in their registration order.