|
| 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