Search in sources :

Example 1 with LoggingInterceptor

use of org.drools.core.command.impl.LoggingInterceptor in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method interceptor.

private void interceptor(final boolean withOOPath) {
    final String str = getSimpleRule(withOOPath);
    final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    final PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    sscs.addInterceptor(new LoggingInterceptor());
    sscs.addInterceptor(new FireAllRulesInterceptor());
    sscs.addInterceptor(new LoggingInterceptor());
    final List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    insertIntRange(ksession, 1, 3);
    ksession.getWorkItemManager().completeWorkItem(0, null);
    assertThat(list).hasSize(3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 2 with LoggingInterceptor

use of org.drools.core.command.impl.LoggingInterceptor in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method interceptorOnRollback.

private void interceptorOnRollback(final boolean withOOPath) throws Exception {
    final String str = getSimpleRule(withOOPath);
    final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    final PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    sscs.addInterceptor(new LoggingInterceptor());
    sscs.addInterceptor(new FireAllRulesInterceptor());
    sscs.addInterceptor(new LoggingInterceptor());
    ChainableRunner runner = sscs.getChainableRunner();
    assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
    runner = (ChainableRunner) runner.getNext();
    assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
    runner = (ChainableRunner) runner.getNext();
    assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
    final UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    final List<?> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ut.rollback();
    ksession.insert(3);
    runner = sscs.getChainableRunner();
    assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
    runner = (ChainableRunner) runner.getNext();
    assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
    runner = (ChainableRunner) runner.getNext();
    assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
}
Also used : UserTransaction(javax.transaction.UserTransaction) ChainableRunner(org.drools.core.runtime.ChainableRunner) LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 3 with LoggingInterceptor

use of org.drools.core.command.impl.LoggingInterceptor in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testInterceptor.

@Test
public void testInterceptor() {
    String str = "";
    str += "package org.kie.test\n";
    str += "global java.util.List list\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "  Integer(intValue > 0)\n";
    str += "then\n";
    str += "  list.add( 1 );\n";
    str += "end\n";
    str += "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
    sscs.addInterceptor(new LoggingInterceptor());
    sscs.addInterceptor(new FireAllRulesInterceptor());
    sscs.addInterceptor(new LoggingInterceptor());
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);
    ksession.getWorkItemManager().completeWorkItem(0, null);
    assertEquals(3, list.size());
}
Also used : LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 FireAllRulesInterceptor (org.drools.core.command.impl.FireAllRulesInterceptor)3 LoggingInterceptor (org.drools.core.command.impl.LoggingInterceptor)3 PersistableRunner (org.drools.persistence.PersistableRunner)3 KieBase (org.kie.api.KieBase)3 KieSession (org.kie.api.runtime.KieSession)3 KieHelper (org.kie.internal.utils.KieHelper)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 UserTransaction (javax.transaction.UserTransaction)1 ChainableRunner (org.drools.core.runtime.ChainableRunner)1 Test (org.junit.Test)1 KieServices (org.kie.api.KieServices)1 KieFileSystem (org.kie.api.builder.KieFileSystem)1