use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.
@Test
public void testSerializationWithWindowLengthAndLiaSharing() throws Exception {
// DROOLS-953
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + " $e : StockTick() over window:length(1)\n" + "then\n" + " list.add($e.getCompany());\n" + "end\n" + "\n" + "rule ReportEventInserted when\n" + " $e : StockTick()\n" + "then\n" + " System.out.println(\"Event Insert : \" + $e);\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(1, "ACME", 50));
ksession.insert(new StockTick(2, "DROO", 50));
ksession.insert(new StockTick(3, "JBPM", 50));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("JBPM", list.get(0));
try {
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
System.out.println(list);
assertEquals(0, list.size());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class CepEspTest method testPseudoSchedulerRemoveJobTest.
@Test(timeout = 10000)
public void testPseudoSchedulerRemoveJobTest() {
String str = "import " + CepEspTest.class.getName() + ".A\n";
str += "declare A\n";
str += " @role( event )\n";
str += "end\n";
str += "rule A\n";
str += "when\n";
str += " $a : A()\n";
str += " not A(this after [1s,10s] $a)\n";
str += "then\n";
str += "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(str)), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = knowledgeBase.newKieSession(sessionConfig, KieServices.get().newEnvironment());
PseudoClockScheduler pseudoClock = ksession.<PseudoClockScheduler>getSessionClock();
FactHandle h = ksession.insert(new A());
ksession.retract(h);
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class EventDeserializationInPastTest method testSerializationWithEventInPastBZ1205666.
@Test
public void testSerializationWithEventInPastBZ1205666() {
// DROOLS-749
final String drl = "import " + Event1.class.getCanonicalName() + "\n" + "declare Event1\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 3h )\n" + "end\n" + "\n" + "rule R\n" + " when\n" + " $evt: Event1()\n" + " not Event1(this != $evt, this after[0, 1h] $evt)\n" + " then\n" + " System.out.println($evt.getCode());\n" + "end\n";
final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
final KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
final KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
ksession.insert(new Event1("id1", 0));
final PseudoClockScheduler clock = ksession.getSessionClock();
clock.advanceTime(2, TimeUnit.HOURS);
ksession.fireAllRules();
ksession = marshallAndUnmarshall(KieServices.Factory.get(), kbase, ksession, sessionConfig);
ksession.insert(new Event1("id2", 0));
ksession.fireAllRules();
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testBetaRightExpired.
@Test
public void testBetaRightExpired() {
// DROOLS-1329
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( id == $Aid )\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + " counter.incrementAndGet();\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler sessionClock = ksession.getSessionClock();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(new A(1));
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.insert(new B(1));
ksession.fireAllRules();
assertEquals(0, counter.get());
}
use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.
the class ExpirationTest method testBetaLeftExpired.
@Test
public void testBetaLeftExpired() {
// DROOLS-1329
String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + " $a: A( $Aid: id > 0 )\n" + " $b: B( id == $Aid )\n" + "then\n" + " System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + " counter.incrementAndGet();\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler sessionClock = ksession.getSessionClock();
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert(new B(1));
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.insert(new A(1));
ksession.fireAllRules();
assertEquals(0, counter.get());
}
Aggregations