Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConditionalRuleGroup implementation #130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
* A composite rule is triggered if <strong>ALL</strong> conditions of its composing rules are satisfied.
* When a composite rule is applied, actions of <strong>ALL</strong> composing rules are performed.
*
* @deprecated use <code>UnitRuleGroup</code> instead. This class is deprecated in v3.2 and will be removed in v3.3 .
*
* @author Mahmoud Ben Hassine ([email protected])
*/
@Deprecated
public class CompositeRule extends BasicRule {

/**
Expand Down
83 changes: 83 additions & 0 deletions easy-rules-support/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules</artifactId>
<version>3.2.0-SNAPSHOT</version>
</parent>

<artifactId>easy-rules-support</artifactId>
<packaging>jar</packaging>
<name>Easy Rules Support module</name>
<description>Support classes module</description>

<scm>
<url>[email protected]:j-easy/easy-rules.git</url>
<connection>scm:git:[email protected]:j-easy/easy-rules.git</connection>
<developerConnection>scm:git:[email protected]:j-easy/easy-rules.git</developerConnection>
<tag>HEAD</tag>
</scm>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/j-easy/easy-rules/issues</url>
</issueManagement>

<ciManagement>
<system>Travis CI</system>
<url>https://travis-ci.org/j-easy/easy-rules</url>
</ciManagement>

<developers>
<developer>
<id>benas</id>
<name>Mahmoud Ben Hassine</name>
<url>http://benas.github.io</url>
<email>[email protected]</email>
<roles>
<role>Lead developer</role>
</roles>
</developer>
</developers>

<licenses>
<license>
<name>MIT License</name>
<url>http://opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<dependencies>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-core</artifactId>
<version>${parent.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* 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.support;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.core.BasicRule;
import org.jeasy.rules.core.RuleProxy;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
* Base class representing a composite rule composed of a set of rules.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public abstract class CompositeRule extends BasicRule {

/**
* The set of composing rules.
*/
protected Set<Rule> rules;

private Map<Object, Rule> proxyRules;

/**
* Create a new {@link CompositeRule}.
*/
public CompositeRule() {
this(Rule.DEFAULT_NAME, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
}

/**
* Create a new {@link CompositeRule}.
*
* @param name rule name
*/
public CompositeRule(final String name) {
this(name, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
}

/**
* Create a new {@link CompositeRule}.
*
* @param name rule name
* @param description rule description
*/
public CompositeRule(final String name, final String description) {
this(name, description, Rule.DEFAULT_PRIORITY);
}

/**
* Create a new {@link CompositeRule}.
*
* @param name rule name
* @param description rule description
* @param priority rule priority
*/
public CompositeRule(final String name, final String description, final int priority) {
super(name, description, priority);
rules = new TreeSet<>();
proxyRules = new HashMap<>();
}

@Override
public abstract boolean evaluate(Facts facts);

@Override
public abstract void execute(Facts facts) throws Exception;

/**
* Add a rule to the composite rule.
* @param rule the rule to add
*/
public void addRule(final Object rule) {
Rule proxy = RuleProxy.asRule(rule);
rules.add(proxy);
proxyRules.put(rule, proxy);
}

/**
* Remove a rule from the composite rule.
* @param rule the rule to remove
*/
public void removeRule(final Object rule) {
Rule proxy = proxyRules.get(rule);
if (proxy != null) {
rules.remove(proxy);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.jeasy.rules.support;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A conditional rule group is a composite rule where the rule with the highest priority acts as a condition:
* if the rule with the highest priority evaluates to true, then we try to evaluate the rest of the rules
* and execute the ones that evaulate to true.
*
* @author Dag Framstad ([email protected])
*/
public class ConditionalRuleGroup extends CompositeRule {

private Set<Rule> successfulEvaluations;
private Rule conditionalRule;

/**
* Create a conditional rule group.
*/
public ConditionalRuleGroup() {
}

/**
* Create a conditional rule group.
*
* @param name of the conditional rule
*/
public ConditionalRuleGroup(String name) {
super(name);
}

/**
* Create a conditional rule group.
*
* @param name of the conditional rule
* @param description of the conditional rule
*/
public ConditionalRuleGroup(String name, String description) {
super(name, description);
}

/**
* Create a conditional rule group.
*
* @param name of the conditional rule
* @param description of the conditional rule
* @param priority of the composite rule
*/
public ConditionalRuleGroup(String name, String description, int priority) {
super(name, description, priority);
}

/**
* A path rule will trigger all it's rules if the path rule's condition is true.
* @param facts The facts.
* @return true if the path rules condition is true.
*/
@Override
public boolean evaluate(Facts facts) {
successfulEvaluations = new HashSet<>();
conditionalRule = getRuleWithHighestPriority();
if (conditionalRule.evaluate(facts)) {
for (Rule rule : rules) {
if (rule != conditionalRule && rule.evaluate(facts)) {
successfulEvaluations.add(rule);
}
}
return true;
}
return false;
}

/**
* When a conditional rule group is applied, all rules that evaluated to true are performed
* in their natural order, but with the conditional rule (the one with the highest priority) first.
*
* @param facts The facts.
*
* @throws Exception thrown if an exception occurs during actions performing
*/
@Override
public void execute(Facts facts) throws Exception {
conditionalRule.execute(facts);
for (Rule rule : successfulEvaluations) {
rule.execute(facts);
}
}

private Rule getRuleWithHighestPriority() {
List<Rule> copy = sortRules();
// make sure that we only have one rule with the highest prority
Rule highest = copy.get(0);
if (copy.size() > 1 && copy.get(1).getPriority() == highest.getPriority()) {
throw new IllegalArgumentException("Only one rule can have highest priority");
}
return highest;
}

private List<Rule> sortRules() {
List<Rule> copy = new ArrayList<>(rules);
Collections.sort(copy, new Comparator<Rule>() {
@Override
public int compare(Rule o1, Rule o2) {
Integer i2 = o2.getPriority();
return i2.compareTo(o1.getPriority());
}
});
return copy;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.jeasy.rules.support;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;

/**
* A unit rule group is a composite rule that acts as a unit: Either all rules are applied or nothing is applied.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public class UnitRuleGroup extends CompositeRule {

/**
* Create a unit rule group.
*/
public UnitRuleGroup() {
}

/**
* Create a unit rule group.
* @param name of the composite rule
*/
public UnitRuleGroup(String name) {
super(name);
}

/**
* Create a unit rule group.
* @param name of the composite rule
* @param description of the composite rule
*/
public UnitRuleGroup(String name, String description) {
super(name, description);
}

/**
* Create a unit rule group.
* @param name of the composite rule
* @param description of the composite rule
* @param priority of the composite rule
*/
public UnitRuleGroup(String name, String description, int priority) {
super(name, description, priority);
}

@Override
public boolean evaluate(Facts facts) {
if (!rules.isEmpty()) {
for (Rule rule : rules) {
if (!rule.evaluate(facts)) {
return false;
}
}
return true;
}
return false;
}

@Override
public void execute(Facts facts) throws Exception {
for (Rule rule : rules) {
rule.execute(facts);
}
}
}
Loading