From 6a33e9ae9e0fecaa027d6924047e7c3ebc15332b Mon Sep 17 00:00:00 2001 From: Clayton Long Date: Mon, 11 Dec 2017 14:42:19 -0500 Subject: [PATCH 1/2] changed GoldenRule to allow for facts to be filtered, including null values --- gradle.properties | 2 +- .../rulebook/model/GoldenRule.java | 2 +- .../rulebook/model/AuditableRuleTest.java | 1 + .../rulebook/model/RuleBookAuditorTest.java | 27 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 7f4ae19..5e16ecf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # project specific settings project.group=com.deliveredtechnologies -project.version=0.9 +project.version=0.9.1 project.compatibility=1.8 project.url=http://www.deliveredtechnologies.com project.inceptionYear=2017 diff --git a/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java b/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java index de8afe1..8006a4e 100644 --- a/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java +++ b/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java @@ -134,7 +134,7 @@ public boolean invoke(NameValueReferableMap facts) { //only use facts of the specified type NameValueReferableMap typeFilteredFacts = new FactMap((Map>) facts.values().stream() - .filter((Object fact) -> _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass())) + .filter((Object fact) -> ((NameValueReferable)fact).getValue() == null || _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass())) .collect(Collectors.toMap(fact -> ((NameValueReferable) fact).getName(), fact -> (NameValueReferable) fact))); diff --git a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/AuditableRuleTest.java b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/AuditableRuleTest.java index f15f9f4..a7d2dea 100644 --- a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/AuditableRuleTest.java +++ b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/AuditableRuleTest.java @@ -7,6 +7,7 @@ import com.deliveredtechnologies.rulebook.RuleState; import com.deliveredtechnologies.rulebook.Result; +import com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; diff --git a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java index 04fbe7d..6d94b13 100644 --- a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java +++ b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java @@ -1,11 +1,16 @@ package com.deliveredtechnologies.rulebook.model; import com.deliveredtechnologies.rulebook.FactMap; +import com.deliveredtechnologies.rulebook.NameValueReferableMap; +import com.deliveredtechnologies.rulebook.Result; import com.deliveredtechnologies.rulebook.lang.RuleBookBuilder; import com.deliveredtechnologies.rulebook.lang.RuleBuilder; +import com.deliveredtechnologies.rulebook.model.rulechain.cor.CoRRuleBook; import org.junit.Assert; import org.junit.Test; +import java.util.function.BiConsumer; + /** * Tests for {@link RuleBookAuditor}. */ @@ -31,4 +36,26 @@ public void ruleBookAuditorCanAuditRules() { Assert.assertEquals(auditor.getRuleStatus("Rule2"), RuleStatus.SKIPPED); Assert.assertEquals(auditor.getRuleStatus("Rule3"), RuleStatus.EXECUTED); } + + @Test + @SuppressWarnings("unchecked") + public void rulesAreStillExecutedWithNullFacts() { + Rule rule = new GoldenRule(Object.class); + BiConsumer action = (facts, result) -> result.setValue("Rule was triggered with status=" + facts.get("status") + " and object=" + facts.get("object")); + rule.addAction(action); + + AuditableRule aRule = new AuditableRule(rule, "SimpleRule"); + + FactMap facts = new FactMap(); + facts.setValue("status", 1); + facts.setValue("object", null); + + RuleBook ruleBook = new CoRRuleBook(); + + RuleBookAuditor auditor = new RuleBookAuditor(ruleBook); + auditor.addRule(aRule); + ruleBook.run(facts); + + Assert.assertEquals(RuleStatus.EXECUTED, auditor.getRuleStatus("SimpleRule")); + } } From f2ad77be8bd2142ded7d6df7ae174707b4441d81 Mon Sep 17 00:00:00 2001 From: Clayton Long Date: Mon, 11 Dec 2017 14:46:09 -0500 Subject: [PATCH 2/2] fixed checkstyle issues --- .../deliveredtechnologies/rulebook/model/GoldenRule.java | 3 ++- .../rulebook/model/RuleBookAuditorTest.java | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java b/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java index 8006a4e..d44cd92 100644 --- a/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java +++ b/rulebook-core/src/main/java/com/deliveredtechnologies/rulebook/model/GoldenRule.java @@ -134,7 +134,8 @@ public boolean invoke(NameValueReferableMap facts) { //only use facts of the specified type NameValueReferableMap typeFilteredFacts = new FactMap((Map>) facts.values().stream() - .filter((Object fact) -> ((NameValueReferable)fact).getValue() == null || _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass())) + .filter((Object fact) -> ((NameValueReferable)fact).getValue() == null + || _factType.isAssignableFrom(((NameValueReferable) fact).getValue().getClass())) .collect(Collectors.toMap(fact -> ((NameValueReferable) fact).getName(), fact -> (NameValueReferable) fact))); diff --git a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java index 6d94b13..d143c34 100644 --- a/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java +++ b/rulebook-core/src/test/java/com/deliveredtechnologies/rulebook/model/RuleBookAuditorTest.java @@ -41,10 +41,12 @@ public void ruleBookAuditorCanAuditRules() { @SuppressWarnings("unchecked") public void rulesAreStillExecutedWithNullFacts() { Rule rule = new GoldenRule(Object.class); - BiConsumer action = (facts, result) -> result.setValue("Rule was triggered with status=" + facts.get("status") + " and object=" + facts.get("object")); + BiConsumer action = (facts, result) + -> result.setValue("Rule was triggered with status=" + facts.get("status") + + " and object=" + facts.get("object")); rule.addAction(action); - AuditableRule aRule = new AuditableRule(rule, "SimpleRule"); + AuditableRule auditableRule = new AuditableRule(rule, "SimpleRule"); FactMap facts = new FactMap(); facts.setValue("status", 1); @@ -53,7 +55,7 @@ public void rulesAreStillExecutedWithNullFacts() { RuleBook ruleBook = new CoRRuleBook(); RuleBookAuditor auditor = new RuleBookAuditor(ruleBook); - auditor.addRule(aRule); + auditor.addRule(auditableRule); ruleBook.run(facts); Assert.assertEquals(RuleStatus.EXECUTED, auditor.getRuleStatus("SimpleRule"));