use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testAnnotatedEventAssertion.
@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession session = createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CommandsTest method testSessionTimeCommands.
@Test
public void testSessionTimeCommands() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
KieServices ks = KieServices.get();
KieCommands kieCommands = ks.getCommands();
KieSessionConfiguration sessionConfig = ks.newKieSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession kSession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(sessionConfig, null);
assertEquals(0L, (long) kSession.execute(kieCommands.newGetSessionTime()));
assertEquals(2000L, (long) kSession.execute(kieCommands.newAdvanceSessionTime(2, TimeUnit.SECONDS)));
assertEquals(2000L, (long) kSession.execute(kieCommands.newGetSessionTime()));
kSession.dispose();
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2Paths.
@Test
public void testEagerEvaluationWith2Paths() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
@Override
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
list.add("cancelled");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
FactHandle fh = ksession.insert(1);
assertEquals(2, list.size());
assertEquals("activated", list.get(0));
assertEquals("activated", list.get(1));
list.clear();
ksession.delete(fh);
assertEquals(2, list.size());
assertEquals("cancelled", list.get(0));
assertEquals("cancelled", list.get(1));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testSegMemInitializationWithForceEagerActivation.
@Test(timeout = 10000L)
public void testSegMemInitializationWithForceEagerActivation() throws InterruptedException {
// DROOLS-1247
String drl = "global java.util.List list\n" + "declare SimpleFact end\n" + "declare AnotherFact end\n" + "\n" + "rule Init when\n" + " not (SimpleFact())\n" + " not (AnotherFact())\n" + "then\n" + " insert(new SimpleFact());\n" + " insert(new AnotherFact());\n" + "end\n" + "\n" + "rule R1 no-loop when\n" + " $f : SimpleFact() \n" + " $h : AnotherFact() \n" + " $s : String() \n" + " eval(true)\n" + "then\n" + " list.add(\"1\");\n" + "end\n" + "\n" + "rule R2 no-loop when\n" + " $f : SimpleFact() \n" + " $h : AnotherFact() \n" + " $s : String() \n" + "then\n" + " list.add(\"2\");\n" + "end";
KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
config.setOption(ForceEagerActivationOption.YES);
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(config, null);
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("1", "2")));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2SubPaths.
@Test
public void testEagerEvaluationWith2SubPaths() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " exists( Integer() or Long() )\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(2, list.size());
}
Aggregations