Skip to content

Commit

Permalink
issue #121 : extract logging in a default RulesEngineListener
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Dec 11, 2017
1 parent 40b456b commit 6b726e0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public DefaultRulesEngine(final RulesEngineParameters parameters, final List<Rul
this.ruleListeners.add(new DefaultRuleListener());
this.ruleListeners.addAll(ruleListeners);
this.rulesEngineListeners = new ArrayList<>();
this.rulesEngineListeners.add(new DefaultRulesEngineListener(parameters));
this.rulesEngineListeners.addAll(rulesEngineListeners);
}

Expand All @@ -113,33 +114,11 @@ public List<RulesEngineListener> getRulesEngineListeners() {
@Override
public void fire(Rules rules, Facts facts) {
triggerListenersBeforeRules(rules, facts);
if (rules.isEmpty()) {
LOGGER.warn("No rules registered! Nothing to apply");
return;
}
logEngineParameters();
log(rules);
log(facts);
apply(rules, facts);
triggerListenersAfterRules(rules, facts);
}

@Override
public Map<Rule, Boolean> check(Rules rules, Facts facts) {
triggerListenersBeforeRules(rules, facts);
LOGGER.info("Checking rules");
Map<Rule, Boolean> result = new HashMap<>();
for (Rule rule : rules) {
if (shouldBeEvaluated(rule, facts)) {
result.put(rule, rule.evaluate(facts));
}
}
triggerListenersAfterRules(rules, facts);
return result;
}

void apply(Rules rules, Facts facts) {
LOGGER.info("Rules evaluation started");
for (Rule rule : rules) {
final String name = rule.getName();
final int priority = rule.getPriority();
Expand Down Expand Up @@ -180,6 +159,25 @@ void apply(Rules rules, Facts facts) {
}
}

@Override
public Map<Rule, Boolean> check(Rules rules, Facts facts) {
triggerListenersBeforeRules(rules, facts);
Map<Rule, Boolean> result = doCheck(rules, facts);
triggerListenersAfterRules(rules, facts);
return result;
}

private Map<Rule, Boolean> doCheck(Rules rules, Facts facts) {
LOGGER.info("Checking rules");
Map<Rule, Boolean> result = new HashMap<>();
for (Rule rule : rules) {
if (shouldBeEvaluated(rule, facts)) {
result.put(rule, rule.evaluate(facts));
}
}
return result;
}

private void triggerListenersOnFailure(final Rule rule, final Exception exception, Facts facts) {
for (RuleListener ruleListener : ruleListeners) {
ruleListener.onFailure(rule, facts, exception);
Expand Down Expand Up @@ -229,27 +227,4 @@ private boolean shouldBeEvaluated(Rule rule, Facts facts) {
return triggerListenersBeforeEvaluate(rule, facts);
}

private void logEngineParameters() {
LOGGER.info(parameters.toString());
}

private void log(Rules rules) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Registered rules:");
for (Rule rule : rules) {
LOGGER.info("Rule { name = '{}', description = '{}', priority = '{}'}",
rule.getName(), rule.getDescription(), rule.getPriority());
}
}
}

private void log(Facts facts) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Known facts:");
for (Map.Entry<String, Object> fact : facts) {
LOGGER.info("Fact { {} : {} }",
fact.getKey(), String.valueOf(fact.getValue()));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* The MIT License
*
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.rules.core;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngineListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

class DefaultRulesEngineListener implements RulesEngineListener {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRuleListener.class);

private RulesEngineParameters parameters;

DefaultRulesEngineListener(RulesEngineParameters parameters) {
this.parameters = parameters;
}

@Override
public void beforeEvaluate(Rules rules, Facts facts) {
if (!rules.isEmpty()) {
logEngineParameters();
log(rules);
log(facts);
LOGGER.info("Rules evaluation started");
} else {
LOGGER.warn("No rules registered! Nothing to apply");
}
}

@Override
public void afterExecute(Rules rules, Facts facts) {

}

private void logEngineParameters() {
LOGGER.info(parameters.toString());
}

private void log(Rules rules) {
LOGGER.info("Registered rules:");
for (Rule rule : rules) {
LOGGER.info("Rule { name = '{}', description = '{}', priority = '{}'}",
rule.getName(), rule.getDescription(), rule.getPriority());
}
}

private void log(Facts facts) {
LOGGER.info("Known facts:");
for (Map.Entry<String, Object> fact : facts) {
LOGGER.info("Fact { {} : {} }",
fact.getKey(), String.valueOf(fact.getValue()));
}
}
}

4 comments on commit 6b726e0

@cemo
Copy link

@cemo cemo commented on 6b726e0 Dec 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benas would you cut a release soon?

@fmbenhassine
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, planned for christmas 😄

#119 and #36 and I'm ready to go

@fmbenhassine
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cemo v3.1 is out! Many improvements are there thanks to your suggestions 👍 Thank you!
You will notice 2 small breaking changes from the snapshot version in the RuleBuilder (see 001dd0c ).

In the end, with #119 and #36, we have some flexibility in defining rules (as shown in the readme). I hope this version will attract users that were looking for defining rules in external resources.

@cemo
Copy link

@cemo cemo commented on 6b726e0 Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benas Thanks for your kind words. We had already starting to use immediately after your release. We have not observed any problem. Such little enhancements are really no problem. Thanks for your time and understanding.

Please sign in to comment.