BAEL-803: Backward Chaining with Drools - new example (#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
This commit is contained in:
parent
45997664ad
commit
52eb1380b6
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.drools;
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public Result backwardChaining() {
|
||||
Result result = new Result();
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
KieSession ksession = kContainer.newKieSession("ksession-backward-chaining");
|
||||
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();
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.drools.model;
|
||||
|
||||
import org.kie.api.definition.type.Position;
|
||||
|
||||
public class Fact {
|
||||
|
||||
@Position(0)
|
||||
private String element;
|
||||
|
||||
@Position(1)
|
||||
private String place;
|
||||
|
||||
public Fact(String element, String place) {
|
||||
this.element = element;
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
public String getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(String element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public String getPlace() {
|
||||
return place;
|
||||
}
|
||||
|
||||
public void setPlace(String place) {
|
||||
this.place = place;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((element == null) ? 0 : element.hashCode());
|
||||
result = prime * result + ((place == null) ? 0 : place.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Fact other = (Fact) obj;
|
||||
if (element == null) {
|
||||
if (other.element != null)
|
||||
return false;
|
||||
} else if (!element.equals(other.element))
|
||||
return false;
|
||||
if (place == null) {
|
||||
if (other.place != null)
|
||||
return false;
|
||||
} else if (!place.equals(other.place))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Fact{" + "element='" + element + '\'' + ", place='" + place + '\'' + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.drools.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Result {
|
||||
private String value;
|
||||
private List<String> facts = new ArrayList<>();
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public List<String> getFacts() {
|
||||
return facts;
|
||||
}
|
||||
|
||||
public void setFacts(List<String> facts) {
|
||||
this.facts = facts;
|
||||
}
|
||||
|
||||
public void addFact(String fact) {
|
||||
this.facts.add(fact);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
groupId=com.baeldung.drools
|
||||
artifactId=DroosBackwardChaining
|
||||
version=1.0.0-SNAPSHOT
|
|
@ -0,0 +1,34 @@
|
|||
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
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.test;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
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;
|
||||
|
||||
@RunWith(value = JUnit4.class)
|
||||
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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue