Skip to content

Commit 52eb138

Browse files
felipeazvadamd1985
authored andcommitted
BAEL-803: Backward Chaining with Drools - new example (eugenp#2921)
* 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 * BAEL-803: Backward Chaining with Drools - new example
1 parent 4599766 commit 52eb138

6 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.drools;
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.drools.model.Fact;
8+
import com.baeldung.drools.model.Result;
9+
10+
public class BackwardChaining {
11+
public static void main(String[] args) {
12+
Result result = new BackwardChaining().backwardChaining();
13+
System.out.println(result.getValue());
14+
result.getFacts().stream().forEach(System.out::println);
15+
}
16+
17+
public Result backwardChaining() {
18+
Result result = new Result();
19+
KieServices ks = KieServices.Factory.get();
20+
KieContainer kContainer = ks.getKieClasspathContainer();
21+
KieSession ksession = kContainer.newKieSession("ksession-backward-chaining");
22+
ksession.setGlobal("result", result);
23+
ksession.insert(new Fact("Asia", "Planet Earth"));
24+
// ksession.insert(new Fact("China", "Asia"));
25+
ksession.insert(new Fact("Great Wall of China", "China"));
26+
27+
ksession.fireAllRules();
28+
29+
return result;
30+
31+
}
32+
33+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.drools.model;
2+
3+
import org.kie.api.definition.type.Position;
4+
5+
public class Fact {
6+
7+
@Position(0)
8+
private String element;
9+
10+
@Position(1)
11+
private String place;
12+
13+
public Fact(String element, String place) {
14+
this.element = element;
15+
this.place = place;
16+
}
17+
18+
public String getElement() {
19+
return element;
20+
}
21+
22+
public void setElement(String element) {
23+
this.element = element;
24+
}
25+
26+
public String getPlace() {
27+
return place;
28+
}
29+
30+
public void setPlace(String place) {
31+
this.place = place;
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
final int prime = 31;
37+
int result = 1;
38+
result = prime * result + ((element == null) ? 0 : element.hashCode());
39+
result = prime * result + ((place == null) ? 0 : place.hashCode());
40+
return result;
41+
}
42+
43+
@Override
44+
public boolean equals(Object obj) {
45+
if (this == obj)
46+
return true;
47+
if (obj == null)
48+
return false;
49+
if (getClass() != obj.getClass())
50+
return false;
51+
Fact other = (Fact) obj;
52+
if (element == null) {
53+
if (other.element != null)
54+
return false;
55+
} else if (!element.equals(other.element))
56+
return false;
57+
if (place == null) {
58+
if (other.place != null)
59+
return false;
60+
} else if (!place.equals(other.place))
61+
return false;
62+
return true;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "Fact{" + "element='" + element + '\'' + ", place='" + place + '\'' + '}';
68+
}
69+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.drools.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Result {
7+
private String value;
8+
private List<String> facts = new ArrayList<>();
9+
10+
public String getValue() {
11+
return value;
12+
}
13+
14+
public void setValue(String value) {
15+
this.value = value;
16+
}
17+
18+
public List<String> getFacts() {
19+
return facts;
20+
}
21+
22+
public void setFacts(List<String> facts) {
23+
this.facts = facts;
24+
}
25+
26+
public void addFact(String fact) {
27+
this.facts.add(fact);
28+
}
29+
30+
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
groupId=com.baeldung.drools
2+
artifactId=DroosBackwardChaining
3+
version=1.0.0-SNAPSHOT
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung
2+
3+
import com.baeldung.drools.model.Fact;
4+
5+
global com.baeldung.drools.model.Result result;
6+
7+
dialect "mvel"
8+
9+
query belongsTo(String x, String y)
10+
Fact(x, y;)
11+
or
12+
(Fact(z, y;) and belongsTo(x, z;))
13+
end
14+
15+
rule "Great Wall of China BELONGS TO Planet Earth"
16+
when
17+
belongsTo("Great Wall of China", "Planet Earth";)
18+
then
19+
result.setValue("Decision one taken: Great Wall of China BELONGS TO Planet Earth");
20+
end
21+
22+
rule "Great Wall of China DOES NOT BELONG TO of Planet Earth"
23+
when
24+
not belongsTo("Great Wall of China", "Planet Earth";)
25+
then
26+
result.setValue("Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth");
27+
end
28+
29+
rule "print all facts"
30+
when
31+
belongsTo(element, place;)
32+
then
33+
result.addFact(element + " IS ELEMENT OF " + place);
34+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.test;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
import org.kie.api.KieServices;
8+
import org.kie.api.runtime.KieContainer;
9+
import org.kie.api.runtime.KieSession;
10+
11+
import com.baeldung.drools.model.Fact;
12+
import com.baeldung.drools.model.Result;
13+
14+
import static junit.framework.TestCase.assertEquals;
15+
16+
@RunWith(value = JUnit4.class)
17+
public class BackwardChainingTest {
18+
private Result result;
19+
private KieServices ks;
20+
private KieContainer kContainer;
21+
private KieSession ksession;
22+
23+
@Before
24+
public void before() {
25+
result = new Result();
26+
ks = KieServices.Factory.get();
27+
kContainer = ks.getKieClasspathContainer();
28+
ksession = kContainer.newKieSession("ksession-backward-chaining");
29+
ksession.setGlobal("result", result);
30+
}
31+
32+
@Test
33+
public void whenWallOfChinaIsGiven_ThenItBelongsToPlanetEarth() {
34+
35+
ksession.setGlobal("result", result);
36+
ksession.insert(new Fact("Asia", "Planet Earth"));
37+
ksession.insert(new Fact("China", "Asia"));
38+
ksession.insert(new Fact("Great Wall of China", "China"));
39+
40+
ksession.fireAllRules();
41+
42+
// Assert Decision one
43+
assertEquals(result.getValue(), "Decision one taken: Great Wall of China BELONGS TO Planet Earth");
44+
}
45+
46+
@Test
47+
public void whenChinaIsNotGiven_ThenWallOfChinaDoesNotBelongToPlanetEarth() {
48+
ksession.insert(new Fact("Asia", "Planet Earth"));
49+
// ksession.insert(new Location("China", "Asia")); // not provided to force Decision two
50+
ksession.insert(new Fact("Great Wall of China", "China"));
51+
52+
ksession.fireAllRules();
53+
54+
// Assert Decision two
55+
assertEquals(result.getValue(), "Decision two taken: Great Wall of China DOES NOT BELONG TO Planet Earth");
56+
}
57+
}

0 commit comments

Comments
 (0)