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();
}
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();
}
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();
}
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();
}
Aggregations