Search in sources :

Example 6 with TrackingAgendaEventListener

use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.

the class DecisionTableTest method testEvalTable.

/**
 * test for various evaluations, file sample_eval_dt.xls need to rewrite xls
 * table and maybe add some classes to be able to do the test
 */
@Test
public void testEvalTable() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("eval_dt.xls", getClass(), DecisionTableInputType.XLS);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    Assertions.assertThat(2).isEqualTo(kbase.getKiePackages().size());
    KieSession session = kbase.newKieSession();
    final TrackingAgendaEventListener rulesFired = new TrackingAgendaEventListener();
    session.addEventListener(rulesFired);
    rulesFired.clear();
    // eval test 1
    final Subject mary = new Subject("Mary");
    mary.setDummy(1);
    session.insert(mary);
    session.fireAllRules();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isTrue();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
    session.dispose();
    // eval test 2
    session = kbase.newKieSession();
    session.addEventListener(rulesFired);
    rulesFired.clear();
    final Subject inge = new Subject("Inge");
    inge.setAge(7);
    inge.setSex("F");
    final Subject jochen = new Subject("Jochen");
    jochen.setAge(9);
    jochen.setSex("M");
    session.insert(inge);
    session.insert(jochen);
    session.fireAllRules();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isTrue();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
    session.dispose();
    // eval test 3, will run four times, there are four combinations
    session = kbase.newKieSession();
    session.addEventListener(rulesFired);
    rulesFired.clear();
    final Subject karl = new Subject("Karl");
    karl.setSex("male");
    final Subject egon = new Subject("Egon");
    egon.setSex("male");
    session.insert(karl);
    session.insert(egon);
    session.fireAllRules();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isTrue();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
    session.dispose();
    // eval test 4
    session = kbase.newKieSession();
    session.addEventListener(rulesFired);
    rulesFired.clear();
    final Subject gerda = new Subject("Gerda");
    gerda.setSex("female");
    gerda.setAge(9);
    gerda.setDummy(-10);
    session.insert(gerda);
    session.fireAllRules();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isTrue();
    Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isFalse();
    session.dispose();
    // eval test 5 - simple binding
    session = kbase.newKieSession();
    session.addEventListener(rulesFired);
    rulesFired.clear();
    final List<Sample> results = new ArrayList<>();
    session.setGlobal("results", results);
    final Sample sample = new Sample();
    session.insert(sample);
    session.fireAllRules();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest1")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest2")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest3")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("evalTest4")).isFalse();
    Assertions.assertThat(rulesFired.isRuleFired("simpleBindingTest")).isTrue();
    session.dispose();
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) Sample(org.drools.testcoverage.common.model.Sample) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Subject(org.drools.testcoverage.common.model.Subject) Test(org.junit.Test)

Example 7 with TrackingAgendaEventListener

use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.

the class DecisionTableTest method testPushQueryWithFactDeclaration.

@Test
public void testPushQueryWithFactDeclaration() throws IllegalAccessException, InstantiationException {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("queries.xls", getClass(), DecisionTableInputType.XLS);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Location");
    final KieSession ksession = kbase.newKieSession();
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final Person peter = new Person("Peter");
    peter.setLikes("steak");
    final Object steakLocation = locationType.newInstance();
    locationType.set(steakLocation, "thing", "steak");
    locationType.set(steakLocation, "location", "table");
    final Object tableLocation = locationType.newInstance();
    locationType.set(tableLocation, "thing", "table");
    locationType.set(tableLocation, "location", "office");
    ksession.insert(peter);
    final FactHandle steakHandle = ksession.insert(steakLocation);
    final FactHandle tableHandle = ksession.insert(tableLocation);
    ksession.insert("push");
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
    listener.clear();
    // when location is changed of what Peter likes, push query should fire
    // rule
    final Object steakLocation2 = locationType.newInstance();
    locationType.set(steakLocation2, "thing", "steak");
    locationType.set(steakLocation2, "location", "desk");
    final Object deskLocation = locationType.newInstance();
    locationType.set(deskLocation, "thing", "desk");
    locationType.set(deskLocation, "location", "office");
    ksession.insert(steakLocation2);
    ksession.insert(deskLocation);
    ksession.delete(steakHandle);
    ksession.delete(tableHandle);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
    listener.clear();
    final Person paul = new Person("Paul");
    paul.setLikes("steak");
    ksession.insert(paul);
    ksession.fireAllRules();
    Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isTrue();
    Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
    listener.clear();
    ksession.dispose();
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 8 with TrackingAgendaEventListener

use of org.drools.testcoverage.common.listener.TrackingAgendaEventListener in project drools by kiegroup.

the class GuidedDecisionTableTest method initKieSession.

private void initKieSession(String... resourceNames) {
    KieResources kieResources = KieServices.get().getResources();
    final Resource[] resources = Stream.of(resourceNames).map(resource -> kieResources.newClassPathResource(resource, GuidedDecisionTableTest.class)).toArray(Resource[]::new);
    final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resources);
    kSession = kBase.newKieSession();
    rulesFired = new TrackingAgendaEventListener();
    kSession.addEventListener(rulesFired);
    rulesFired.clear();
}
Also used : Address(org.drools.testcoverage.common.model.Address) Collection(java.util.Collection) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieResources(org.kie.api.io.KieResources) FactHandle(org.kie.api.runtime.rule.FactHandle) Resource(org.kie.api.io.Resource) Stream(java.util.stream.Stream) Person(org.drools.testcoverage.common.model.Person) KieServices(org.kie.api.KieServices) Assertions(org.assertj.core.api.Assertions) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieResources(org.kie.api.io.KieResources)

Aggregations

TrackingAgendaEventListener (org.drools.testcoverage.common.listener.TrackingAgendaEventListener)8 Resource (org.kie.api.io.Resource)8 Test (org.junit.Test)7 KieBase (org.kie.api.KieBase)7 KieSession (org.kie.api.runtime.KieSession)7 Person (org.drools.testcoverage.common.model.Person)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 Before (org.junit.Before)2 FactType (org.kie.api.definition.type.FactType)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Stream (java.util.stream.Stream)1 Assertions (org.assertj.core.api.Assertions)1 Address (org.drools.testcoverage.common.model.Address)1 Message (org.drools.testcoverage.common.model.Message)1 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)1 Sample (org.drools.testcoverage.common.model.Sample)1 Subject (org.drools.testcoverage.common.model.Subject)1 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)1 KieBaseUtil (org.drools.testcoverage.common.util.KieBaseUtil)1