Skip to content

defining rules listener

Mahmoud Ben Hassine edited this page May 17, 2017 · 6 revisions

You can listen to rule execution events through the RuleListener API:

public interface RuleListener {

    /**
     * Triggered before the evaluation of a rule.
     *
     * @param rule being evaluated
     * @param facts known before evaluating the rule
     * @return true if the rule should be evaluated, false otherwise
     */
    boolean beforeEvaluate(Rule rule, Facts facts);

    /**
     * Triggered after the evaluation of a rule.
     *
     * @param rule that has been evaluated
     * @param facts known after evaluating the rule
     * @param evaluationResult true if the rule evaluated to true, false otherwise
     */
    void afterEvaluate(Rule rule, Facts facts, boolean evaluationResult);

    /**
     * Triggered before the execution of a rule.
     *
     * @param rule the current rule
     * @param rule known before executing the rule
     */
    void beforeExecute(Rule rule, Facts facts);

    /**
     * Triggered after a rule has been executed successfully.
     *
     * @param rule the current rule
     * @param facts known after executing the rule
     */
    void onSuccess(Rule rule, Facts facts);

    /**
     * Triggered after a rule has failed.
     *
     * @param rule      the current rule
     * @param facts known after executing the rule
     * @param exception the exception thrown when attempting to execute the rule
     */
    void onFailure(Rule rule, Facts facts, Exception exception);

}

You can implement this interface to provide custom behavior to execute before/after each rule. To register your listener, use the following snippet:

RulesEngine rulesEngine = aNewRulesEngine()
    .withRuleListener(myRuleListener)
    .build();

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