Skip to content

Commit 0b19939

Browse files
felipeazvadamd1985
authored andcommitted
BAEL-803: Backward Chaining with Drools (eugenp#2741)
* spring beans DI examples * fix-1: shortening examples * List of Rules Engines in Java * BAEL-812: Openl-Tablets example added * BAEL-812: artifacts names changed * BAEL-812: moving rule-engines examples to rule-engines folder * BAEL-812: removing evaluation article files * BAEL-812: folder renamed * BAEL-812: folder renamed * BAEL-812: pom.xml - parent added * BAEL-1027: Introduction to GraphQL - initial commit * BAEL-781: Explore the new Spring Cloud Gateway * BAEL-781: Fix UserService.java * BAEL-781: Fix user-service pom.xml * BAEL-781: remove eureka-server from the example * BAEL-781: modifying example * BAEL-803: Backward Chaining wih Drools * BAEL-803: pom.xml
1 parent d0862d2 commit 0b19939

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

drools/backward-chaining/pom.xml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>drools-backward-chaining</artifactId>
8+
<version>1.0</version>
9+
<name>drools-backward-chaining</name>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<properties>
18+
<runtime.version>6.4.0.Final</runtime.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.kie</groupId>
24+
<artifactId>kie-api</artifactId>
25+
<version>${runtime.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.drools</groupId>
29+
<artifactId>drools-core</artifactId>
30+
<version>${runtime.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.drools</groupId>
34+
<artifactId>drools-decisiontables</artifactId>
35+
<version>${runtime.version}</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung;
2+
3+
import org.kie.api.KieServices;
4+
import org.kie.api.runtime.KieContainer;
5+
import org.kie.api.runtime.KieSession;
6+
7+
import com.baeldung.model.Beatle;
8+
9+
public class BackwardChainingBeatles {
10+
public static void main(String[] args) {
11+
12+
KieServices ks = KieServices.Factory.get();
13+
KieContainer kContainer = ks.getKieClasspathContainer();
14+
KieSession kSession = kContainer.newKieSession("ksession-backward-chaining");
15+
// drools session base on the xml configuration (<strong>kmodule.xml</strong>)
16+
17+
// graph population
18+
kSession.insert(new Beatle("Starr", "drums"));
19+
kSession.insert(new Beatle("McCartney", "bass"));
20+
kSession.insert(new Beatle("Lennon", "guitar"));
21+
kSession.insert(new Beatle("Harrison", "guitar"));
22+
23+
kSession.insert("Ringo"); // invoke the rule that calls the query implentation of backward chaining
24+
kSession.fireAllRules(); // invoke all the rules
25+
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.model;
2+
3+
import org.kie.api.definition.type.Position;
4+
5+
public class Beatle {
6+
7+
@Position(0)
8+
private String lastName;
9+
@Position(1)
10+
private String instrument;
11+
12+
public Beatle(String lastName, String instrument) {
13+
this.lastName = lastName;
14+
this.instrument = instrument;
15+
}
16+
17+
public String getInstrument() {
18+
return instrument;
19+
}
20+
21+
public void setInstrument(String instrument) {
22+
this.instrument = instrument;
23+
}
24+
25+
public String getLastName() {
26+
return lastName;
27+
}
28+
29+
public void setLastName(String lastName) {
30+
this.lastName = lastName;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
3+
<kbase name="backward_chaining" packages="backward_chaining">
4+
<ksession name="ksession-backward-chaining"/>
5+
</kbase>
6+
</kmodule>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung
2+
3+
import com.baeldung.model.Beatle;
4+
5+
6+
query whichBeatle(String lastName, String instrument)
7+
Beatle(lastName, instrument;)
8+
or
9+
(Beatle(lastName, null;)
10+
and
11+
whichBeatle(null, instrument;)) //recursive call to the function that allows to search in a derivation tree structure
12+
end
13+
14+
rule "Ringo"
15+
when
16+
String(this == "Ringo")
17+
whichBeatle("Starr", "drums";)
18+
then
19+
System.out.println("The beatle is Ringo Starr");
20+
end
21+
22+
rule "Paul"
23+
when
24+
String(this == "Paul")
25+
whichBeatle("McCartney", "bass";)
26+
then
27+
System.out.println("The beatle is Paul McCartney");
28+
end
29+
30+
rule "John"
31+
when
32+
String(this == "John")
33+
whichBeatle("Lennon", "guitar";)
34+
then
35+
System.out.println("The beatle is John Lennon");
36+
end
37+
38+
rule "George"
39+
when
40+
String(this == "George")
41+
whichBeatle("Harrison", "guitar";)
42+
then
43+
System.out.println("The beatle is George Harrison");
44+
end

0 commit comments

Comments
 (0)