use of org.drools.core.common.EventFactHandle in project drools by kiegroup.
the class CepEspTest method testExpiredEventWithPendingActivations.
@Test
public void testExpiredEventWithPendingActivations() throws Exception {
final String drl = "package org.drools.drools_usage_pZB7GRxZp64;\n" + "\n" + "declare time_Var\n" + " @role( event )\n" + " @expires( 1s )\n" + " value : Long\n" + "end\n" + "\n" + "declare ExpiringEvent_Var\n" + " @role( event )\n" + " @expires( 10s )\n" + " value : Double\n" + "end\n" + "\n" + "declare window ExpiringEvent_Window1 ExpiringEvent_Var() over window:length(1) end\n" + "\n" + "rule \"Expring variable - Init\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " not ExpiringEvent_Var()\n" + "\n" + " then\n" + " System.out.println($now + \" : Init\");\n" + " insert(new ExpiringEvent_Var(0.0));\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 1\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value < 1.0) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 1\");\n" + " insert(new ExpiringEvent_Var(1.0));\n" + "\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 2\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 2\");\n" + " insert(new ExpiringEvent_Var($previousValue));\n" + "\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final SessionPseudoClock clock = session.getSessionClock();
final FactType time_VarType = kbase.getFactType("org.drools.drools_usage_pZB7GRxZp64", "time_Var");
clock.advanceTime(1472057509000L, TimeUnit.MILLISECONDS);
final Object time_Var = time_VarType.newInstance();
time_VarType.set(time_Var, "value", 1472057509000L);
session.insert(time_Var);
session.fireAllRules();
for (int i = 0; i < 10; i++) {
clock.advanceTime(1, TimeUnit.SECONDS);
final Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
}
clock.advanceTime(1, TimeUnit.SECONDS);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
final Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
final Object time_VarP2 = time_VarType.newInstance();
time_VarType.set(time_VarP2, "value", clock.getCurrentTime());
session.insert(time_VarP2);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
session.fireAllRules();
// actually should be empty..
for (final Object o : session.getFactHandles()) {
if (o instanceof EventFactHandle) {
final EventFactHandle eventFactHandle = (EventFactHandle) o;
assertFalse(eventFactHandle.isExpired());
}
}
} finally {
session.dispose();
}
}
use of org.drools.core.common.EventFactHandle in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEvent.
@Test
public void testDeleteExpiredEvent() {
// BZ-1274696
final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final PseudoClockScheduler clock = ksession.getSessionClock();
final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50));
ksession.fireAllRules();
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
} finally {
ksession.dispose();
}
}
use of org.drools.core.common.EventFactHandle in project drools by kiegroup.
the class SlidingTimeWindow method assertFact.
@Override
public boolean assertFact(final Object context, final InternalFactHandle fact, final PropagationContext pctx, final ReteEvaluator reteEvaluator) {
final SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
final EventFactHandle handle = (EventFactHandle) fact;
long currentTime = reteEvaluator.getTimerService().getCurrentTime();
if (isExpired(currentTime, handle)) {
return false;
}
queue.add(handle);
if (handle.equals(queue.peek())) {
// update next expiration time
updateNextExpiration(handle, reteEvaluator, queue, nodeId);
}
return true;
}
use of org.drools.core.common.EventFactHandle in project drools by kiegroup.
the class SlidingTimeWindow method expireFacts.
@Override
public void expireFacts(final Object context, final PropagationContext pctx, final ReteEvaluator reteEvaluator) {
TimerService clock = reteEvaluator.getTimerService();
long currentTime = clock.getCurrentTime();
SlidingTimeWindowContext queue = (SlidingTimeWindowContext) context;
EventFactHandle handle = queue.peek();
while (handle != null && isExpired(currentTime, handle)) {
queue.setExpiringHandle(handle);
queue.remove();
if (handle.isValid()) {
// if not expired yet, expire it
final PropagationContext expiresPctx = createPropagationContextForFact(reteEvaluator, handle, PropagationContext.Type.EXPIRATION);
ObjectTypeNode.doRetractObject(handle, expiresPctx, reteEvaluator);
}
queue.setExpiringHandle(null);
handle = queue.peek();
}
// update next expiration time
updateNextExpiration(handle, reteEvaluator, queue, nodeId);
}
use of org.drools.core.common.EventFactHandle in project drools by kiegroup.
the class EventListDataStream method insertAndAdvanceClock.
private void insertAndAdvanceClock(T t, DataProcessor subscriber) {
EventFactHandle fh = (EventFactHandle) subscriber.insert(null, t);
long timestamp = fh.getStartTimestamp();
WorkingMemoryEntryPoint ep = fh.getEntryPoint(null);
SessionPseudoClock clock = (SessionPseudoClock) ep.getReteEvaluator().getSessionClock();
long advanceTime = timestamp - clock.getCurrentTime();
if (advanceTime > 0) {
clock.advanceTime(advanceTime, TimeUnit.MILLISECONDS);
} else if (advanceTime < 0) {
LOGGER.warn("Received an event with a timestamp that is " + (-advanceTime) + " milliseconds in the past. " + "Evaluation of out of order events could lead to unpredictable results.");
}
}
Aggregations