-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #121 : extract logging in a default RulesEngineListener
- Loading branch information
1 parent
40b456b
commit 6b726e0
Showing
2 changed files
with
101 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
easy-rules-core/src/main/java/org/jeasy/rules/core/DefaultRulesEngineListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
6b726e0
There was a problem hiding this comment.
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?
6b726e0
There was a problem hiding this comment.
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
6b726e0
There was a problem hiding this comment.
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.
6b726e0
There was a problem hiding this comment.
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.