BAEL-803: Backward Chaining with Drools (#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
This commit is contained in:
parent
d0862d2804
commit
0b19939bcd
|
@ -0,0 +1,38 @@
|
|||
<?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>
|
||||
<artifactId>drools-core</artifactId>
|
||||
<version>${runtime.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.drools</groupId>
|
||||
<artifactId>drools-decisiontables</artifactId>
|
||||
<version>${runtime.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,28 @@
|
|||
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
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
<?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>
|
|
@ -0,0 +1,44 @@
|
|||
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
|
Loading…
Reference in New Issue