Search in sources :

Example 6 with OrderListener

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

the class DeclarativeAgendaTest method testCancelWithUpdatingFacts.

// very complicated
// rule firing and canceling depends also on order of inserting facts into
// working memory
// but activationListener('direct') annotation should guarantee priority to
// fire
@Test(timeout = 10000L)
public void testCancelWithUpdatingFacts() {
    final KieBase kbase = buildKieBase("declarative-agenda-cancel.drl");
    final KieSession ksession = kbase.newKieSession();
    OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // first run - with cancelling rule, it should cancel activation of
    // sales1
    final FactHandle fireRules = ksession.insert("fireRules");
    final FactHandle fireCancelRule = ksession.insert("fireCancelRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected = { "salesCancel", "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    // second run
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.update(fireCancelRule, "fireCancelRule");
    ksession.update(fireRules, "fireRules");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected2 = { "salesCancel", "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected2[i]);
    }
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 7 with OrderListener

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

the class DeclarativeAgendaTest method testSimpleCancel.

@Test(timeout = 10000L)
public void testSimpleCancel() {
    final KieBase kbase = buildKieBase("declarative-agenda-cancel.drl");
    final KieSession ksession = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // fires only sales1 rule, sales2 rule activation is canceled by
    // salesCancel rule
    ksession.insert("fireRules");
    ksession.insert("fireCancelRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected = { "salesCancel", "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 8 with OrderListener

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

the class DeclarativeAgendaTest method testSimpleActivationBlock.

// rule activation is blocked and after several iterations (fireAllRules) it
// is unblocked and the rule fires
@Test(timeout = 10000L)
public void testSimpleActivationBlock() {
    final KieBase kbase = buildKieBase("declarative-agenda-simple-block.drl");
    final KieSession ksession = kbase.newKieSession();
    OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // first run - just run rules without any blocking
    final FactHandle fireRules = ksession.insert("fireRules");
    final FactHandle fireBlockerRule = ksession.insert("fireBlockerRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected = { "blocker", "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    // second run - add blocker rule
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(0);
    // third run
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.delete(fireBlockerRule);
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(1);
    final String[] expected3 = { "sales1" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected3[i]);
    }
    // fourth run
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(0);
    // fifth run
    ksession.removeEventListener(listener);
    listener = new OrderListener();
    ksession.addEventListener(listener);
    ksession.update(fireRules, "fireRules");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected5 = { "sales1", "sales2" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected5[i]);
    }
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 9 with OrderListener

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

the class DeclarativeAgendaTest method testUnblockAll.

// testing unblockall command
@Test(timeout = 10000L)
public void testUnblockAll() {
    final KieBase kbase = buildKieBase("declarative-agenda-unblockall.drl");
    final KieSession ksession = kbase.newKieSession();
    final OrderListener listener = new OrderListener();
    ksession.addEventListener(listener);
    // first run
    ksession.insert("fireRules");
    ksession.insert("fireBlockerRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(2);
    final String[] expected = { "salesBlocker", "salesBlocker" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected[i]);
    }
    // second run
    ksession.insert("fireUnblockerRule");
    ksession.fireAllRules();
    Assertions.assertThat(listener.size()).isEqualTo(8);
    final String[] expected2 = { "salesBlocker", "salesBlocker", "salesUnblocker", "sales1", "salesBlocker", "salesUnblocker", "sales2", "salesBlocker" };
    for (int i = 0; i < listener.size(); i++) {
        Assertions.assertThat(listener.get(i)).isEqualTo(expected2[i]);
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) OrderListener(org.drools.testcoverage.common.listener.OrderListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

OrderListener (org.drools.testcoverage.common.listener.OrderListener)9 Test (org.junit.Test)9 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 Resource (org.kie.api.io.Resource)4 FactHandle (org.kie.api.runtime.rule.FactHandle)4 Sample (org.drools.testcoverage.common.model.Sample)1 Subject (org.drools.testcoverage.common.model.Subject)1