BAEL-803: Backward Chaining with Drools - changefix (#3020)

This commit is contained in:
felipeazv
2017-11-13 19:41:00 +01:00
committed by Grzegorz Piwowarek
parent f984b4a07c
commit 503fd2d24f
15 changed files with 105 additions and 311 deletions
@@ -0,0 +1,30 @@
package com.baeldung.drools.backward_chaining;
import org.kie.api.runtime.KieSession;
import com.baeldung.drools.config.DroolsBeanFactory;
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();
KieSession ksession = new DroolsBeanFactory().getKieSession();
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;
}
}
@@ -3,7 +3,6 @@ package com.baeldung.drools.config;
import org.drools.decisiontable.DecisionTableProviderImpl;
import org.kie.api.KieServices;
import org.kie.api.builder.*;
import org.kie.api.io.KieResources;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
@@ -22,7 +21,7 @@ public class DroolsBeanFactory {
private KieFileSystem getKieFileSystem() throws IOException{
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){
kieFileSystem.write(ResourceFactory.newClassPathResource(rule));
}
@@ -56,9 +55,11 @@ public class DroolsBeanFactory {
getKieRepository();
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/Product_rules.xls"));
KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
kb.buildAll();
KieModule kieModule = kb.getKieModule();
@@ -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,27 @@
package com.baeldung.drools.rules
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 "print all facts"
when
belongsTo(element, place;)
then
result.addFact(element + " IS ELEMENT OF " + place);
end
@@ -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");
}
}