use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testTimerWithMillisPrecision.
@Test
public void testTimerWithMillisPrecision() {
// RHBRMS-2627
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R\n" + " timer (int: 0 1; start=$startTime, repeat-limit=0 )\n" + " when\n" + " $event: MyEvent ($startTime : timestamp)\n" + " $counter : AtomicInteger(get() > 0)\n" + " then\n" + " System.out.println(\"RG_TEST_TIMER WITH \" + $event + \" AND \" + $counter);\n" + " modify($counter){\n" + " decrementAndGet()\n" + " }\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);
// System.currentTimeMillis();
long now = 1000;
PseudoClockScheduler sessionClock = ksession.getSessionClock();
sessionClock.setStartupTime(now - 10);
AtomicInteger counter = new AtomicInteger(1);
MyEvent event1 = new MyEvent(now - 8);
MyEvent event2 = new MyEvent(now - 7);
MyEvent event3 = new MyEvent(now - 6);
ksession.insert(counter);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
// Nothing Happens
ksession.fireAllRules();
assertEquals(1, counter.get());
sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, counter.get());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testPackageSerializationWithEvents.
@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() throws Exception {
// read in the source
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
// get the package
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(2, pkgs.size());
// serialize the package
byte[] serializedPkg = DroolsStreamUtils.streamOut(pkgs);
pkgs = (Collection<KiePackage>) DroolsStreamUtils.streamIn(serializedPkg);
// create the kbase
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
// create the session
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
KieSession session = createKnowledgeSession(kbase, conf);
final List<StockTick> results = new ArrayList<StockTick>();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(tick2, results.get(0));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testRightTupleExpiration.
@Test
public void testRightTupleExpiration() {
// RHBRMS-2463
String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "global AtomicInteger counter;\n" + "declare MyEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + " String()\n" + " MyEvent()\n" + " Boolean()\n" + " Integer()\n" + " then\n" + " 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();
sessionClock.setStartupTime(0);
AtomicInteger counter = new AtomicInteger(0);
ksession.setGlobal("counter", counter);
ksession.insert("test");
ksession.insert(true);
ksession.insert(new MyEvent(0));
ksession.insert(new MyEvent(15));
ksession.fireAllRules();
assertEquals(0, counter.get());
sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
ksession.insert(1);
// MyEvent is expired
ksession.fireAllRules();
assertEquals(1, counter.get());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testDeleteOfDeserializedJob.
@Test
public void testDeleteOfDeserializedJob() throws Exception {
// DROOLS-1660
String drl = "import " + EventA.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "global java.util.List list\n" + "declare EventA\n" + " @role(event)\n" + " @timestamp(timestamp)\n" + "end\n" + "rule test\n" + " when\n" + " $event : EventA(value == 1)\n" + " not(EventA(value == 1, this after [1ms,4m] $event))\n" + " then\n" + " list.add(\"Fired \"+ $event);\n" + "end\n";
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kieBase.newKieSession(sessionConfig, null);
List<String> list = new ArrayList<>();
List<EventA> events = new ArrayList<>();
events.add(new EventA("2010-01-01 02:00:00", 0));
events.add(new EventA("2010-01-01 03:00:00", 1));
events.add(new EventA("2010-01-01 03:01:00", 0));
events.add(new EventA("2010-01-01 03:02:00", 1));
events.add(new EventA("2010-01-01 03:03:00", 0));
events.add(new EventA("2010-01-01 03:04:00", 0));
events.add(new EventA("2010-01-01 03:05:00", 0));
events.add(new EventA("2010-01-01 03:06:00", 0));
events.add(new EventA("2010-01-01 03:07:00", 0));
// set clock reference
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(events.get(0).getTimestamp().getTime(), TimeUnit.MILLISECONDS);
byte[] serializedSession = null;
try {
Marshaller marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kieBase);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession);
serializedSession = baos.toByteArray();
} catch (IOException e2) {
e2.printStackTrace();
}
for (EventA current : events) {
KieSession ksession2 = null;
Marshaller marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kieBase);
try {
ByteArrayInputStream bais = new ByteArrayInputStream(serializedSession);
ksession2 = marshaller.unmarshall(bais, sessionConfig, null);
ksession2.setGlobal("list", list);
clock = ksession2.getSessionClock();
bais.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
long currTime = clock.getCurrentTime();
long nextTime = current.getTimestamp().getTime();
while (currTime <= (nextTime - 1000)) {
clock.advanceTime(1000, TimeUnit.MILLISECONDS);
ksession2.fireAllRules();
currTime += 1000;
}
long diff = nextTime - currTime;
if (diff > 0) {
clock.advanceTime(diff, TimeUnit.MILLISECONDS);
}
ksession2.insert(current);
ksession2.fireAllRules();
// serialize knowledge session
try {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshall(baos, ksession2);
serializedSession = baos.toByteArray();
} catch (IOException e2) {
e2.printStackTrace();
}
ksession2.dispose();
}
assertEquals(1, list.size());
assertEquals("Fired EventA at 2010-01-01 03:02:00", list.get(0));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testEventExpiration4.
@Test(timeout = 10000)
public void testEventExpiration4() throws Exception {
// read in the source
final KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration4.drl");
final KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = createKnowledgeSession(kbase, sconf);
EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
final List results = new ArrayList();
ksession.setGlobal("results", results);
EventFactHandle handle1 = (EventFactHandle) eventStream.insert(new StockTick(1, "ACME", 50, System.currentTimeMillis(), 3));
ksession.fireAllRules();
clock.advanceTime(11, TimeUnit.SECONDS);
/**
* clock.advance() will put the event expiration in the queue to be executed,
* but it has to wait for a "thread" to do that
* so we fire rules again here to get that
* alternative could run fireUntilHalt() *
*/
ksession.fireAllRules();
assertTrue(results.size() == 1);
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
}
Aggregations