Search in sources :

Example 1 with KieSessionsPool

use of org.kie.api.runtime.KieSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testStatelessSequential.

@Test
public void testStatelessSequential() {
    // DROOLS-3228
    String drl = "import " + AtomicInteger.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule R1 when\n" + "  String()\n" + "  Integer()\n" + "then\n" + "  list.add(\"OK\");\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    KieSessionsPool pool = kbase.newKieSessionsPool(1);
    StatelessKieSession ksession = pool.newStatelessKieSession();
    List<String> list = new ArrayList<>();
    List<Command> commands = new ArrayList<>(5);
    commands.add(CommandFactory.newSetGlobal("list", list));
    commands.add(CommandFactory.newInsert("test"));
    commands.add(CommandFactory.newInsert(1));
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    assertEquals(1, list.size());
    list.clear();
    ksession.execute(CommandFactory.newBatchExecution(commands));
    assertEquals(1, list.size());
    pool.shutdown();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieModule(org.kie.api.builder.KieModule) KieSessionsPool(org.kie.api.runtime.KieSessionsPool) Test(org.junit.Test)

Example 2 with KieSessionsPool

use of org.kie.api.runtime.KieSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testSegmentMemoriesResetWithNotNodeInTheMiddle.

@Test
public void testSegmentMemoriesResetWithNotNodeInTheMiddle() {
    String drl = "import " + FactA.class.getCanonicalName() + ";\n" + "import " + FactB.class.getCanonicalName() + ";\n" + "import " + FactC.class.getCanonicalName() + ";\n" + "rule R1\n" + "when\n" + "  $factA : FactA( field1 == \"code1\")\n" + "  not FactC( f2 == 1 && f1 == \"code1\")\n" + "  $factB : FactB( f2 == 1 )\n" + "then\n" + "end\n" + "rule R2\n" + "when\n" + "  $factA : FactA( field1 == \"code1\")\n" + "  not FactC( f2 == 1 && f1 == \"code1\")\n" + "  $factB : FactB( f2 == 3 )\n" + "then\n" + "end\n" + "rule R3\n" + "when\n" + "  $factA: FactA( field1 == \"code1\")\n" + "  $factC: FactC( f1 == \"code1\")\n" + "then\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    KieContainer kcontainer = KieServices.get().newKieContainer(kieModule.getReleaseId());
    KieSessionsPool pool = kcontainer.newKieSessionsPool(1);
    KieSession ksession = pool.newKieSession();
    try {
        createFactAndInsert(ksession);
        // R1 is fired
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
    ksession = pool.newKieSession();
    try {
        createFactAndInsert(ksession);
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
    pool.shutdown();
}
Also used : StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) FactB(org.drools.mvel.compiler.FactB) KieModule(org.kie.api.builder.KieModule) KieSessionsPool(org.kie.api.runtime.KieSessionsPool) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with KieSessionsPool

use of org.kie.api.runtime.KieSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testSegmentMemoriesResetWithNotNodeInTheMiddle2.

@Test
public void testSegmentMemoriesResetWithNotNodeInTheMiddle2() {
    // FactB constrains in R1 and R2 are different from testSegmentMemoriesResetWithNotNodeInTheMiddle()
    String drl = "import " + FactA.class.getCanonicalName() + ";\n" + "import " + FactB.class.getCanonicalName() + ";\n" + "import " + FactC.class.getCanonicalName() + ";\n" + "rule R1\n" + "when\n" + "  $factA : FactA( field1 == \"code1\")\n" + "  not FactC( f2 == 1 && f1 == \"code1\")\n" + "  $factB : FactB( f2 == 3 )\n" + "then\n" + "end\n" + "rule R2\n" + "when\n" + "  $factA : FactA( field1 == \"code1\")\n" + "  not FactC( f2 == 1 && f1 == \"code1\")\n" + "  $factB : FactB( f2 == 1 )\n" + "then\n" + "end\n" + "rule R3\n" + "when\n" + "  $factA: FactA( field1 == \"code1\")\n" + "  $factC: FactC( f1 == \"code1\")\n" + "then\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    KieContainer kcontainer = KieServices.get().newKieContainer(kieModule.getReleaseId());
    KieSessionsPool pool = kcontainer.newKieSessionsPool(1);
    KieSession ksession = pool.newKieSession();
    try {
        createFactAndInsert(ksession);
        // R2 is fired
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
    ksession = pool.newKieSession();
    try {
        createFactAndInsert(ksession);
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
    pool.shutdown();
}
Also used : StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) FactB(org.drools.mvel.compiler.FactB) KieModule(org.kie.api.builder.KieModule) KieSessionsPool(org.kie.api.runtime.KieSessionsPool) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with KieSessionsPool

use of org.kie.api.runtime.KieSessionsPool in project drools by kiegroup.

the class SessionsPoolTest method testSegmentMemoriesReset.

@Test
public void testSegmentMemoriesReset() {
    // DROOLS-3228
    String drl = "import " + AtomicInteger.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule R1 when\n" + "  String()\n" + "  $i : AtomicInteger()\n" + "  not Boolean()\n" + "then\n" + "  insert(true);\n" + "  insert($i.incrementAndGet());\n" + "end\n" + "\n" + "rule R2 when \n" + "  String()\n" + "  $i : AtomicInteger()\n" + "then\n" + "end\n" + "\n" + "rule R3 when\n" + "  Integer( this > 2 )\n" + "then\n" + "  list.add(\"OK\");\n" + "end";
    final KieModule kieModule = KieUtil.getKieModuleFromDrls("test", kieBaseTestConfiguration, drl);
    KieContainer kcontainer = KieServices.get().newKieContainer(kieModule.getReleaseId());
    KieSessionsPool pool = kcontainer.newKieSessionsPool(1);
    AtomicInteger i = new AtomicInteger(1);
    KieSession ksession = pool.newKieSession();
    List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(i);
    ksession.insert("test");
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(0, list.size());
    ksession = pool.newKieSession();
    ksession.setGlobal("list", list);
    ksession.insert(i);
    ksession.insert("test");
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(1, list.size());
    pool.shutdown();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) KieModule(org.kie.api.builder.KieModule) KieSessionsPool(org.kie.api.runtime.KieSessionsPool) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 KieModule (org.kie.api.builder.KieModule)4 KieSessionsPool (org.kie.api.runtime.KieSessionsPool)4 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)4 KieContainer (org.kie.api.runtime.KieContainer)3 KieSession (org.kie.api.runtime.KieSession)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 FactB (org.drools.mvel.compiler.FactB)2 KieBase (org.kie.api.KieBase)1 Command (org.kie.api.command.Command)1