BAEL-803: Backward Chaining with Drools - changefix (#3020)
This commit is contained in:
		
							parent
							
								
									f984b4a07c
								
							
						
					
					
						commit
						503fd2d24f
					
				| @ -1,35 +0,0 @@ | |||||||
| <?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> |  | ||||||
| 
 |  | ||||||
| 	<artifactId>drools-backward-chaining</artifactId> |  | ||||||
| 	<version>1.0</version> |  | ||||||
| 	<name>drools-backward-chaining</name> |  | ||||||
| 
 |  | ||||||
| 	<parent> |  | ||||||
| 		<groupId>com.baeldung</groupId> |  | ||||||
| 		<artifactId>parent-modules</artifactId> |  | ||||||
| 		<version>1.0.0-SNAPSHOT</version> |  | ||||||
| 	</parent> |  | ||||||
| 
 |  | ||||||
| 	<properties> |  | ||||||
| 		<runtime.version>6.4.0.Final</runtime.version> |  | ||||||
| 	</properties> |  | ||||||
| 	<dependencies> |  | ||||||
| 		<dependency> |  | ||||||
| 			<groupId>org.kie</groupId> |  | ||||||
| 			<artifactId>kie-api</artifactId> |  | ||||||
| 			<version>${runtime.version}</version> |  | ||||||
| 		</dependency> |  | ||||||
| 		<dependency> |  | ||||||
| 			<groupId>org.drools</groupId> |  | ||||||
| 			<version>${runtime.version}</version> |  | ||||||
| 		</dependency> |  | ||||||
| 		<dependency> |  | ||||||
| 			<groupId>org.drools</groupId> |  | ||||||
| 			<artifactId>drools-decisiontables</artifactId> |  | ||||||
| 			<version>${runtime.version}</version> |  | ||||||
| 		</dependency> |  | ||||||
| 	</dependencies> |  | ||||||
| </project> |  | ||||||
| @ -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 (<strong>kmodule.xml</strong>) |  | ||||||
| 
 |  | ||||||
|             // 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 |  | ||||||
|          |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -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; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,6 +0,0 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> |  | ||||||
| <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule"> |  | ||||||
|     <kbase name="backward_chaining" packages="backward_chaining"> |  | ||||||
|         <ksession name="ksession-backward-chaining"/> |  | ||||||
|     </kbase> |  | ||||||
| </kmodule> |  | ||||||
| @ -1,3 +0,0 @@ | |||||||
| groupId=com.baeldung.drools |  | ||||||
| artifactId=DroosBackwardChaining |  | ||||||
| version=1.0.0-SNAPSHOT |  | ||||||
| @ -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 |  | ||||||
| @ -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 |  | ||||||
| @ -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"); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,14 +1,9 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|          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"> | 	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> | 	<modelVersion>4.0.0</modelVersion> | ||||||
| 
 | 
 | ||||||
|     <groupId>com.baeldung</groupId> |  | ||||||
| 	<artifactId>drools</artifactId> | 	<artifactId>drools</artifactId> | ||||||
|     <version>1.0.0-SNAPSHOT</version> |  | ||||||
| 	 |  | ||||||
| 	<packaging>pom</packaging> |  | ||||||
| 	 | 	 | ||||||
| 	<parent> | 	<parent> | ||||||
| 		<groupId>com.baeldung</groupId> | 		<groupId>com.baeldung</groupId> | ||||||
|  | |||||||
| @ -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 org.kie.api.runtime.KieSession; | ||||||
| 
 | 
 | ||||||
|  | import com.baeldung.drools.config.DroolsBeanFactory; | ||||||
| import com.baeldung.drools.model.Fact; | import com.baeldung.drools.model.Fact; | ||||||
| import com.baeldung.drools.model.Result; | import com.baeldung.drools.model.Result; | ||||||
| 
 | 
 | ||||||
| @ -11,17 +10,17 @@ public class BackwardChaining { | |||||||
|     public static void main(String[] args) { |     public static void main(String[] args) { | ||||||
|         Result result = new BackwardChaining().backwardChaining(); |         Result result = new BackwardChaining().backwardChaining(); | ||||||
|         System.out.println(result.getValue()); |         System.out.println(result.getValue()); | ||||||
|         result.getFacts().stream().forEach(System.out::println); |         result.getFacts() | ||||||
|  |             .stream() | ||||||
|  |             .forEach(System.out::println); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public Result backwardChaining() { |     public Result backwardChaining() { | ||||||
|         Result result = new Result(); |         Result result = new Result(); | ||||||
|         KieServices ks = KieServices.Factory.get(); |         KieSession ksession = new DroolsBeanFactory().getKieSession(); | ||||||
|         KieContainer kContainer = ks.getKieClasspathContainer(); |  | ||||||
|         KieSession ksession = kContainer.newKieSession("ksession-backward-chaining"); |  | ||||||
|         ksession.setGlobal("result", result); |         ksession.setGlobal("result", result); | ||||||
|         ksession.insert(new Fact("Asia", "Planet Earth")); |         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.insert(new Fact("Great Wall of China", "China")); | ||||||
| 
 | 
 | ||||||
|         ksession.fireAllRules(); |         ksession.fireAllRules(); | ||||||
| @ -3,7 +3,6 @@ package com.baeldung.drools.config; | |||||||
| import org.drools.decisiontable.DecisionTableProviderImpl; | import org.drools.decisiontable.DecisionTableProviderImpl; | ||||||
| import org.kie.api.KieServices; | import org.kie.api.KieServices; | ||||||
| import org.kie.api.builder.*; | import org.kie.api.builder.*; | ||||||
| import org.kie.api.io.KieResources; |  | ||||||
| import org.kie.api.io.Resource; | import org.kie.api.io.Resource; | ||||||
| import org.kie.api.runtime.KieContainer; | import org.kie.api.runtime.KieContainer; | ||||||
| import org.kie.api.runtime.KieSession; | import org.kie.api.runtime.KieSession; | ||||||
| @ -22,7 +21,7 @@ public class DroolsBeanFactory { | |||||||
| 
 | 
 | ||||||
|     private  KieFileSystem getKieFileSystem() throws IOException{ |     private  KieFileSystem getKieFileSystem() throws IOException{ | ||||||
|         KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); |         KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); | ||||||
|         List<String> rules=Arrays.asList("SuggestApplicant.drl","Product_rules.xls"); |         List<String> rules=Arrays.asList("BackwardChaining.drl","SuggestApplicant.drl","Product_rules.xls"); | ||||||
|         for(String rule:rules){ |         for(String rule:rules){ | ||||||
|             kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); |             kieFileSystem.write(ResourceFactory.newClassPathResource(rule)); | ||||||
|         } |         } | ||||||
| @ -56,9 +55,11 @@ public class DroolsBeanFactory { | |||||||
|         getKieRepository(); |         getKieRepository(); | ||||||
|         KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); |         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/SuggestApplicant.drl")); | ||||||
|         kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); |         kieFileSystem.write(ResourceFactory.newClassPathResource("com/baeldung/drools/rules/Product_rules.xls")); | ||||||
|          |          | ||||||
|  |          | ||||||
|         KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); |         KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); | ||||||
|         kb.buildAll(); |         kb.buildAll(); | ||||||
|         KieModule kieModule = kb.getKieModule(); |         KieModule kieModule = kb.getKieModule(); | ||||||
|  | |||||||
| @ -1,4 +1,4 @@ | |||||||
| package com.baeldung | package com.baeldung.drools.rules | ||||||
| 
 | 
 | ||||||
| import com.baeldung.drools.model.Fact; | import com.baeldung.drools.model.Fact; | ||||||
| 
 | 
 | ||||||
| @ -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"); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user