From 503fd2d24f09b2b5fbc7759f71d0f69a1f79fdd3 Mon Sep 17 00:00:00 2001 From: felipeazv Date: Mon, 13 Nov 2017 19:41:00 +0100 Subject: [PATCH] BAEL-803: Backward Chaining with Drools - changefix (#3020) --- drools/backward-chaining/pom.xml | 35 ------ .../com/baeldung/BackwardChainingBeatles.java | 28 ----- .../main/java/com/baeldung/model/Beatle.java | 33 ----- .../src/main/resources/META-INF/kmodule.xml | 6 - .../resources/META-INF/maven/pom.properties | 3 - .../resources/backward_chaining/Beatles.drl | 44 ------- .../resources/backward_chaining/rules.drl | 34 ----- .../baeldung/test/BackwardChainingTest.java | 54 -------- drools/pom.xml | 119 +++++++++--------- .../backward_chaining}/BackwardChaining.java | 15 ++- .../drools/config/DroolsBeanFactory.java | 7 +- .../java/com/baeldung/drools/model/Fact.java | 0 .../com/baeldung/drools/model/Result.java | 0 .../drools/rules/BackwardChaining.drl} | 2 +- .../BackwardChainingTest.java | 36 ++++++ 15 files changed, 105 insertions(+), 311 deletions(-) delete mode 100644 drools/backward-chaining/pom.xml delete mode 100644 drools/backward-chaining/src/main/java/com/baeldung/BackwardChainingBeatles.java delete mode 100644 drools/backward-chaining/src/main/java/com/baeldung/model/Beatle.java delete mode 100644 drools/backward-chaining/src/main/resources/META-INF/kmodule.xml delete mode 100644 drools/backward-chaining/src/main/resources/META-INF/maven/pom.properties delete mode 100644 drools/backward-chaining/src/main/resources/backward_chaining/Beatles.drl delete mode 100644 drools/backward-chaining/src/main/resources/backward_chaining/rules.drl delete mode 100644 drools/backward-chaining/src/test/java/com/baeldung/test/BackwardChainingTest.java rename drools/{backward-chaining/src/main/java/com/baeldung/drools => src/main/java/com/baeldung/drools/backward_chaining}/BackwardChaining.java (59%) rename drools/{backward-chaining => }/src/main/java/com/baeldung/drools/model/Fact.java (100%) rename drools/{backward-chaining => }/src/main/java/com/baeldung/drools/model/Result.java (100%) rename drools/{backward-chaining/src/main/resources/com/baeldung/drools/backward_chaining/rules.drl => src/main/resources/com/baeldung/drools/rules/BackwardChaining.drl} (94%) create mode 100644 drools/src/test/java/com/baeldung/drools/backward_chaining/BackwardChainingTest.java diff --git a/drools/backward-chaining/pom.xml b/drools/backward-chaining/pom.xml deleted file mode 100644 index 1cf8f008af..0000000000 --- a/drools/backward-chaining/pom.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - 4.0.0 - - drools-backward-chaining - 1.0 - drools-backward-chaining - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - 6.4.0.Final - - - - org.kie - kie-api - ${runtime.version} - - - org.drools - ${runtime.version} - - - org.drools - drools-decisiontables - ${runtime.version} - - - \ No newline at end of file diff --git a/drools/backward-chaining/src/main/java/com/baeldung/BackwardChainingBeatles.java b/drools/backward-chaining/src/main/java/com/baeldung/BackwardChainingBeatles.java deleted file mode 100644 index 7fc5bd3685..0000000000 --- a/drools/backward-chaining/src/main/java/com/baeldung/BackwardChainingBeatles.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung; - -import org.kie.api.KieServices; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; - -import com.baeldung.model.Beatle; - -public class BackwardChainingBeatles { - public static void main(String[] args) { - - KieServices ks = KieServices.Factory.get(); - KieContainer kContainer = ks.getKieClasspathContainer(); - KieSession kSession = kContainer.newKieSession("ksession-backward-chaining"); - // drools session base on the xml configuration (kmodule.xml) - - // graph population - kSession.insert(new Beatle("Starr", "drums")); - kSession.insert(new Beatle("McCartney", "bass")); - kSession.insert(new Beatle("Lennon", "guitar")); - kSession.insert(new Beatle("Harrison", "guitar")); - - kSession.insert("Ringo"); // invoke the rule that calls the query implentation of backward chaining - kSession.fireAllRules(); // invoke all the rules - - } - -} \ No newline at end of file diff --git a/drools/backward-chaining/src/main/java/com/baeldung/model/Beatle.java b/drools/backward-chaining/src/main/java/com/baeldung/model/Beatle.java deleted file mode 100644 index 4b219b3a69..0000000000 --- a/drools/backward-chaining/src/main/java/com/baeldung/model/Beatle.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.model; - -import org.kie.api.definition.type.Position; - -public class Beatle { - - @Position(0) - private String lastName; - @Position(1) - private String instrument; - - public Beatle(String lastName, String instrument) { - this.lastName = lastName; - this.instrument = instrument; - } - - public String getInstrument() { - return instrument; - } - - public void setInstrument(String instrument) { - this.instrument = instrument; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - -} \ No newline at end of file diff --git a/drools/backward-chaining/src/main/resources/META-INF/kmodule.xml b/drools/backward-chaining/src/main/resources/META-INF/kmodule.xml deleted file mode 100644 index 6498e26343..0000000000 --- a/drools/backward-chaining/src/main/resources/META-INF/kmodule.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/drools/backward-chaining/src/main/resources/META-INF/maven/pom.properties b/drools/backward-chaining/src/main/resources/META-INF/maven/pom.properties deleted file mode 100644 index 26d8331732..0000000000 --- a/drools/backward-chaining/src/main/resources/META-INF/maven/pom.properties +++ /dev/null @@ -1,3 +0,0 @@ -groupId=com.baeldung.drools -artifactId=DroosBackwardChaining -version=1.0.0-SNAPSHOT diff --git a/drools/backward-chaining/src/main/resources/backward_chaining/Beatles.drl b/drools/backward-chaining/src/main/resources/backward_chaining/Beatles.drl deleted file mode 100644 index 8d0d06a79e..0000000000 --- a/drools/backward-chaining/src/main/resources/backward_chaining/Beatles.drl +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung - -import com.baeldung.model.Beatle; - - -query whichBeatle(String lastName, String instrument) - Beatle(lastName, instrument;) - or - (Beatle(lastName, null;) - and - whichBeatle(null, instrument;)) //recursive call to the function that allows to search in a derivation tree structure -end - -rule "Ringo" - when - String(this == "Ringo") - whichBeatle("Starr", "drums";) - then - System.out.println("The beatle is Ringo Starr"); -end - -rule "Paul" - when - String(this == "Paul") - whichBeatle("McCartney", "bass";) - then - System.out.println("The beatle is Paul McCartney"); -end - -rule "John" - when - String(this == "John") - whichBeatle("Lennon", "guitar";) - then - System.out.println("The beatle is John Lennon"); -end - -rule "George" - when - String(this == "George") - whichBeatle("Harrison", "guitar";) - then - System.out.println("The beatle is George Harrison"); -end diff --git a/drools/backward-chaining/src/main/resources/backward_chaining/rules.drl b/drools/backward-chaining/src/main/resources/backward_chaining/rules.drl deleted file mode 100644 index abcf95b9f1..0000000000 --- a/drools/backward-chaining/src/main/resources/backward_chaining/rules.drl +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung - -import com.baeldung.drools.model.Fact; - -global com.baeldung.drools.model.Result result; - -dialect "mvel" - -query belongsTo(String x, String y) - Fact(x, y;) - or - (Fact(z, y;) and belongsTo(x, z;)) -end - -rule "Great Wall of China BELONGS TO Planet Earth" -when - belongsTo("Great Wall of China", "Planet Earth";) -then - result.setValue("Decision one taken: Great Wall of China BELONGS TO Planet Earth"); -end - -rule "Great Wall of China DOES NOT BELONG TO of Planet Earth" -when - not belongsTo("Great Wall of China", "Planet Earth";) -then - result.setValue("Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth"); -end - -rule "print all facts" -when - belongsTo(element, place;) -then - result.addFact(element + " IS ELEMENT OF " + place); -end diff --git a/drools/backward-chaining/src/test/java/com/baeldung/test/BackwardChainingTest.java b/drools/backward-chaining/src/test/java/com/baeldung/test/BackwardChainingTest.java deleted file mode 100644 index 112188f01f..0000000000 --- a/drools/backward-chaining/src/test/java/com/baeldung/test/BackwardChainingTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.test; - -import org.junit.Before; -import org.junit.Test; -import org.kie.api.KieServices; -import org.kie.api.runtime.KieContainer; -import org.kie.api.runtime.KieSession; - -import com.baeldung.drools.model.Fact; -import com.baeldung.drools.model.Result; - -import static junit.framework.TestCase.assertEquals; - -public class BackwardChainingTest { - private Result result; - private KieServices ks; - private KieContainer kContainer; - private KieSession ksession; - - @Before - public void before() { - result = new Result(); - ks = KieServices.Factory.get(); - kContainer = ks.getKieClasspathContainer(); - ksession = kContainer.newKieSession("ksession-backward-chaining"); - ksession.setGlobal("result", result); - } - - @Test - public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() { - - ksession.setGlobal("result", result); - ksession.insert(new Fact("Asia", "Planet Earth")); - ksession.insert(new Fact("China", "Asia")); - ksession.insert(new Fact("Great Wall of China", "China")); - - ksession.fireAllRules(); - - // Assert Decision one - assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth"); - } - - @Test - public void whenChinaIsNotGiven_ThenWallOfChinaDoesNotBelongToPlanetEarth() { - ksession.insert(new Fact("Asia", "Planet Earth")); - // ksession.insert(new Location("China", "Asia")); // not provided to force Decision two - ksession.insert(new Fact("Great Wall of China", "China")); - - ksession.fireAllRules(); - - // Assert Decision two - assertEquals(result.getValue(), "Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth"); - } -} diff --git a/drools/pom.xml b/drools/pom.xml index ab894c6a1a..971bd5f4b8 100644 --- a/drools/pom.xml +++ b/drools/pom.xml @@ -1,73 +1,68 @@ - - 4.0.0 + + 4.0.0 - com.baeldung - drools - 1.0.0-SNAPSHOT + drools - pom + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + 4.4.6 + 7.1.0.Beta2 + 3.13 + - - 4.4.6 - 7.1.0.Beta2 - 3.13 - + + + org.apache.httpcomponents + httpcore + ${http-component-version} + + + + org.kie + kie-ci + ${drools-version} + + + org.drools + drools-decisiontables + ${drools-version} + - - - org.apache.httpcomponents - httpcore - ${http-component-version} - - - - org.kie - kie-ci - ${drools-version} - - - org.drools - drools-decisiontables - ${drools-version} - + + org.drools + drools-core + ${drools-version} + + + org.drools + drools-compiler + ${drools-version} + + + org.apache.poi + poi + ${apache-poi-version} + - - org.drools - drools-core - ${drools-version} - - - org.drools - drools-compiler - ${drools-version} - - - org.apache.poi - poi - ${apache-poi-version} - + + org.apache.poi + poi-ooxml + ${apache-poi-version} + - - org.apache.poi - poi-ooxml - ${apache-poi-version} - + + org.springframework + spring-core + 4.3.6.RELEASE + - - org.springframework - spring-core - 4.3.6.RELEASE - - - + \ No newline at end of file diff --git a/drools/backward-chaining/src/main/java/com/baeldung/drools/BackwardChaining.java b/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java similarity index 59% rename from drools/backward-chaining/src/main/java/com/baeldung/drools/BackwardChaining.java rename to drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java index bac144c32b..6f15ee510b 100644 --- a/drools/backward-chaining/src/main/java/com/baeldung/drools/BackwardChaining.java +++ b/drools/src/main/java/com/baeldung/drools/backward_chaining/BackwardChaining.java @@ -1,9 +1,8 @@ -package com.baeldung.drools; +package com.baeldung.drools.backward_chaining; -import org.kie.api.KieServices; -import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; +import com.baeldung.drools.config.DroolsBeanFactory; import com.baeldung.drools.model.Fact; import com.baeldung.drools.model.Result; @@ -11,17 +10,17 @@ public class BackwardChaining { public static void main(String[] args) { Result result = new BackwardChaining().backwardChaining(); System.out.println(result.getValue()); - result.getFacts().stream().forEach(System.out::println); + result.getFacts() + .stream() + .forEach(System.out::println); } public Result backwardChaining() { Result result = new Result(); - KieServices ks = KieServices.Factory.get(); - KieContainer kContainer = ks.getKieClasspathContainer(); - KieSession ksession = kContainer.newKieSession("ksession-backward-chaining"); + KieSession ksession = new DroolsBeanFactory().getKieSession(); ksession.setGlobal("result", result); ksession.insert(new Fact("Asia", "Planet Earth")); - // ksession.insert(new Fact("China", "Asia")); + ksession.insert(new Fact("China", "Asia")); ksession.insert(new Fact("Great Wall of China", "China")); ksession.fireAllRules(); diff --git a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java index e8841b05e2..cf5d56f246 100644 --- a/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java +++ b/drools/src/main/java/com/baeldung/drools/config/DroolsBeanFactory.java @@ -3,7 +3,6 @@ package com.baeldung.drools.config; import org.drools.decisiontable.DecisionTableProviderImpl; import org.kie.api.KieServices; import org.kie.api.builder.*; -import org.kie.api.io.KieResources; import org.kie.api.io.Resource; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; @@ -22,7 +21,7 @@ public class DroolsBeanFactory { private KieFileSystem getKieFileSystem() throws IOException{ KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); - List rules=Arrays.asList("SuggestApplicant.drl","Product_rules.xls"); + List rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls"); for(String rule:rules){ kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); } @@ -56,9 +55,11 @@ public class DroolsBeanFactory { getKieRepository(); KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); + kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/BackwardChaining.drl")); kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/SuggestApplicant.drl")); kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); - + + KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); kb.buildAll(); KieModule kieModule = kb.getKieModule(); diff --git a/drools/backward-chaining/src/main/java/com/baeldung/drools/model/Fact.java b/drools/src/main/java/com/baeldung/drools/model/Fact.java similarity index 100% rename from drools/backward-chaining/src/main/java/com/baeldung/drools/model/Fact.java rename to drools/src/main/java/com/baeldung/drools/model/Fact.java diff --git a/drools/backward-chaining/src/main/java/com/baeldung/drools/model/Result.java b/drools/src/main/java/com/baeldung/drools/model/Result.java similarity index 100% rename from drools/backward-chaining/src/main/java/com/baeldung/drools/model/Result.java rename to drools/src/main/java/com/baeldung/drools/model/Result.java diff --git a/drools/backward-chaining/src/main/resources/com/baeldung/drools/backward_chaining/rules.drl b/drools/src/main/resources/com/baeldung/drools/rules/BackwardChaining.drl similarity index 94% rename from drools/backward-chaining/src/main/resources/com/baeldung/drools/backward_chaining/rules.drl rename to drools/src/main/resources/com/baeldung/drools/rules/BackwardChaining.drl index bb5a8299e1..975be84fb4 100644 --- a/drools/backward-chaining/src/main/resources/com/baeldung/drools/backward_chaining/rules.drl +++ b/drools/src/main/resources/com/baeldung/drools/rules/BackwardChaining.drl @@ -1,4 +1,4 @@ -package com.baeldung +package com.baeldung.drools.rules import com.baeldung.drools.model.Fact; diff --git a/drools/src/test/java/com/baeldung/drools/backward_chaining/BackwardChainingTest.java b/drools/src/test/java/com/baeldung/drools/backward_chaining/BackwardChainingTest.java new file mode 100644 index 0000000000..f49d0b82de --- /dev/null +++ b/drools/src/test/java/com/baeldung/drools/backward_chaining/BackwardChainingTest.java @@ -0,0 +1,36 @@ +package com.baeldung.drools.backward_chaining; + +import org.junit.Before; +import org.junit.Test; +import org.kie.api.runtime.KieSession; + +import com.baeldung.drools.config.DroolsBeanFactory; +import com.baeldung.drools.model.Fact; +import com.baeldung.drools.model.Result; + +import static junit.framework.TestCase.assertEquals; + +public class BackwardChainingTest { + private Result result; + private KieSession ksession; + + @Before + public void before() { + result = new Result(); + ksession = new DroolsBeanFactory().getKieSession(); + } + + @Test + public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() { + + ksession.setGlobal("result", result); + ksession.insert(new Fact("Asia", "Planet Earth")); + ksession.insert(new Fact("China", "Asia")); + ksession.insert(new Fact("Great Wall of China", "China")); + + ksession.fireAllRules(); + + // Assert Decision one + assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth"); + } +}