-
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.
Merge branch 'inference-rules-engine'
- Loading branch information
Showing
9 changed files
with
405 additions
and
3 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
113 changes: 113 additions & 0 deletions
113
easy-rules-core/src/main/java/org/jeasy/rules/core/InferenceRulesEngine.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,113 @@ | ||
/** | ||
* 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.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* Inference {@link RulesEngine} implementation. | ||
* | ||
* Rules are selected based on given facts and fired according to their natural order which is priority by default. | ||
* | ||
* The engine continuously select and fire rules until no more rules are applicable. | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public final class InferenceRulesEngine implements RulesEngine { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(InferenceRulesEngine.class); | ||
|
||
private RulesEngineParameters parameters; | ||
private List<RuleListener> ruleListeners; | ||
private DefaultRulesEngine delegate; | ||
|
||
/** | ||
* Create a new inference rules engine with default parameters. | ||
*/ | ||
public InferenceRulesEngine() { | ||
this(new RulesEngineParameters()); | ||
} | ||
|
||
/** | ||
* Create a new inference rules engine. | ||
* @param parameters of the engine | ||
*/ | ||
public InferenceRulesEngine(RulesEngineParameters parameters) { | ||
this(parameters, new ArrayList<RuleListener>()); | ||
} | ||
|
||
/** | ||
* Create a new inference rules engine. | ||
* @param parameters of the engine | ||
* @param ruleListeners to apply for each rule | ||
*/ | ||
public InferenceRulesEngine(RulesEngineParameters parameters, List<RuleListener> ruleListeners) { | ||
this.parameters = parameters; | ||
this.ruleListeners = ruleListeners; | ||
delegate = new DefaultRulesEngine(parameters, ruleListeners); | ||
} | ||
|
||
@Override | ||
public RulesEngineParameters getParameters() { | ||
return parameters; | ||
} | ||
|
||
@Override | ||
public List<RuleListener> getRuleListeners() { | ||
return ruleListeners; | ||
} | ||
|
||
@Override | ||
public void fire(Rules rules, Facts facts) { | ||
Set<Rule> selectedRules; | ||
do { | ||
LOGGER.info("Selecting candidate rules based on the following {}", facts); | ||
selectedRules = selectCandidates(rules, facts); | ||
if(!selectedRules.isEmpty()) { | ||
delegate.apply(new Rules(selectedRules), facts); | ||
} else { | ||
LOGGER.info("No candidate rules found for {}", facts); | ||
} | ||
} while (!selectedRules.isEmpty()); | ||
} | ||
|
||
private Set<Rule> selectCandidates(Rules rules, Facts facts) { | ||
Set<Rule> candidates = new TreeSet<>(); | ||
for (Rule rule : rules) { | ||
if (rule.evaluate(facts)) { | ||
candidates.add(rule); | ||
} | ||
} | ||
return candidates; | ||
} | ||
|
||
@Override | ||
public Map<Rule, Boolean> check(Rules rules, Facts facts) { | ||
return delegate.check(rules, facts); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -24,8 +24,13 @@ | |
package org.jeasy.rules.core; | ||
|
||
/** | ||
* Parameters of the rules engine. | ||
* | ||
* Parameters of a rules engine. | ||
* | ||
* <ul> | ||
* <li>When parameters are used with a {@link DefaultRulesEngine}, they are applied on all registered rules.</li> | ||
* <li>When parameters are used with a {@link InferenceRulesEngine}, they are applied on candidate rules in each iteration.</li> | ||
* </ul> | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public class RulesEngineParameters { | ||
|
138 changes: 138 additions & 0 deletions
138
easy-rules-core/src/test/java/org/jeasy/rules/core/InferenceRulesEngineTest.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,138 @@ | ||
/** | ||
* 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.annotation.*; | ||
import org.jeasy.rules.api.Facts; | ||
import org.jeasy.rules.api.Rules; | ||
import org.jeasy.rules.api.RulesEngine; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class InferenceRulesEngineTest { | ||
|
||
@Test | ||
public void testCandidateSelection() throws Exception { | ||
// Given | ||
Facts facts = new Facts(); | ||
facts.put("foo", true); | ||
DummyRule dummyRule = new DummyRule(); | ||
AnotherDummyRule anotherDummyRule = new AnotherDummyRule(); | ||
Rules rules = new Rules(dummyRule, anotherDummyRule); | ||
RulesEngine rulesEngine = new InferenceRulesEngine(); | ||
|
||
// When | ||
rulesEngine.fire(rules, facts); | ||
|
||
// Then | ||
assertThat(dummyRule.isExecuted()).isTrue(); | ||
assertThat(anotherDummyRule.isExecuted()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testCandidateOrdering() throws Exception { | ||
// Given | ||
Facts facts = new Facts(); | ||
facts.put("foo", true); | ||
facts.put("bar", true); | ||
DummyRule dummyRule = new DummyRule(); | ||
AnotherDummyRule anotherDummyRule = new AnotherDummyRule(); | ||
Rules rules = new Rules(dummyRule, anotherDummyRule); | ||
RulesEngine rulesEngine = new InferenceRulesEngine(); | ||
|
||
// When | ||
rulesEngine.fire(rules, facts); | ||
|
||
// Then | ||
assertThat(dummyRule.isExecuted()).isTrue(); | ||
assertThat(anotherDummyRule.isExecuted()).isTrue(); | ||
assertThat(dummyRule.getTimestamp()).isLessThanOrEqualTo(anotherDummyRule.getTimestamp()); | ||
} | ||
|
||
@Rule | ||
class DummyRule { | ||
|
||
private boolean isExecuted; | ||
private long timestamp; | ||
|
||
@Condition | ||
public boolean when(@Fact("foo") boolean foo) { | ||
return foo; | ||
} | ||
|
||
@Action | ||
public void then(Facts facts) { | ||
isExecuted = true; | ||
timestamp = System.currentTimeMillis(); | ||
facts.remove("foo"); | ||
} | ||
|
||
@Priority | ||
public int priority() { | ||
return 1; | ||
} | ||
|
||
public boolean isExecuted() { | ||
return isExecuted; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
} | ||
|
||
@Rule | ||
class AnotherDummyRule { | ||
|
||
private boolean isExecuted; | ||
private long timestamp; | ||
|
||
@Condition | ||
public boolean when(@Fact("bar") boolean bar) { | ||
return bar; | ||
} | ||
|
||
@Action | ||
public void then(Facts facts) { | ||
isExecuted = true; | ||
timestamp = System.currentTimeMillis(); | ||
facts.remove("bar"); | ||
} | ||
|
||
@Priority | ||
public int priority() { | ||
return 2; | ||
} | ||
|
||
public boolean isExecuted() { | ||
return isExecuted; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
} | ||
|
||
} |
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
24 changes: 24 additions & 0 deletions
24
easy-rules-tutorials/src/main/java/org/jeasy/rules/tutorials/airco/AirConditioningRule.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,24 @@ | ||
package org.jeasy.rules.tutorials.airco; | ||
|
||
import org.jeasy.rules.annotation.Action; | ||
import org.jeasy.rules.annotation.Condition; | ||
import org.jeasy.rules.annotation.Fact; | ||
import org.jeasy.rules.annotation.Rule; | ||
import org.jeasy.rules.api.Facts; | ||
|
||
@Rule(name = "air conditioning rule", description = "if it is hot, decrease temperature" ) | ||
public class AirConditioningRule { | ||
|
||
@Condition | ||
public boolean isItHot(@Fact("temperature") int temperature) { | ||
return temperature > 25; | ||
} | ||
|
||
@Action | ||
public void coolAir(Facts facts) { | ||
System.out.println("It is hot! cooling air.."); | ||
Integer temperature = (Integer) facts.get("temperature"); | ||
facts.put("temperature", temperature - 1); | ||
} | ||
|
||
} |
Oops, something went wrong.