Search in sources :

Example 31 with EventFactHandle

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();
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) EntryPoint(org.kie.api.runtime.rule.EntryPoint) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 32 with EventFactHandle

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();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 33 with EventFactHandle

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;
}
Also used : EventFactHandle(org.drools.core.common.EventFactHandle)

Example 34 with EventFactHandle

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);
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) EventFactHandle(org.drools.core.common.EventFactHandle) TimerService(org.drools.core.time.TimerService)

Example 35 with EventFactHandle

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.");
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) EventFactHandle(org.drools.core.common.EventFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint)

Aggregations

EventFactHandle (org.drools.core.common.EventFactHandle)86 Test (org.junit.Test)51 KieSession (org.kie.api.runtime.KieSession)23 KieBase (org.kie.api.KieBase)18 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 DisconnectedWorkingMemoryEntryPoint (org.drools.core.common.DisconnectedWorkingMemoryEntryPoint)13 DisconnectedWorkingMemoryEntryPoint (org.drools.kiesession.entrypoints.DisconnectedWorkingMemoryEntryPoint)13 FactHandle (org.kie.api.runtime.rule.FactHandle)11 DuringEvaluatorDefinition (org.drools.core.base.evaluators.DuringEvaluatorDefinition)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)10 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)6 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)5 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)5 PropagationContext (org.drools.core.spi.PropagationContext)5 StockTick (org.drools.testcoverage.common.model.StockTick)5 EntryPointId (org.drools.core.rule.EntryPointId)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4