Search in sources :

Example 1 with Subject

use of org.drools.testcoverage.common.model.Subject in project drools by kiegroup.

the class DecisionTableTest method testAdvancedTable.

/**
 * test for advanced rule settings (no-loop, saliences, ...), file
 * sample_advanced_dt.xls
 *
 * covers also bugfix for Bug724257 (agenda group not added from dtable to
 * .drl)
 */
@Test
public void testAdvancedTable() {
    final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("advanced_dt.xls", getClass(), DecisionTableInputType.XLS);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    KieSession session = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    session.addEventListener(listener);
    final Subject lili = new Subject("Lili");
    lili.setAge(100);
    final Sample sample = new Sample();
    session.insert(lili);
    session.insert(sample);
    session.fireAllRules();
    // just 4 rules should fire
    Assertions.assertThat(listener.size()).isEqualTo(4);
    // rules have to be fired in expected order
    final String[] expected = new String[] { "HelloWorld_11", "namedRule", "b1", "another rule" };
    for (int i = 0; i < 4; i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    session.dispose();
}
Also used : KieBase(org.kie.api.KieBase) Sample(org.drools.testcoverage.common.model.Sample) Resource(org.kie.api.io.Resource) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Subject(org.drools.testcoverage.common.model.Subject) Test(org.junit.Test)

Example 2 with Subject

use of org.drools.testcoverage.common.model.Subject 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)

Aggregations

Sample (org.drools.testcoverage.common.model.Sample)2 Subject (org.drools.testcoverage.common.model.Subject)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 Resource (org.kie.api.io.Resource)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 OrderListener (org.drools.testcoverage.common.listener.OrderListener)1 TrackingAgendaEventListener (org.drools.testcoverage.common.listener.TrackingAgendaEventListener)1