Search in sources :

Example 26 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class CepEspTest method testComplexTimestamp.

@Test(timeout = 10000)
public void testComplexTimestamp() {
    final String drl = "package " + Message.class.getPackage().getName() + "\n" + "declare " + Message.class.getCanonicalName() + "\n" + "   @role( event ) \n" + "   @timestamp( getProperties().get( 'timestamp' ) - 1 ) \n" + "   @duration( getProperties().get( 'duration' ) + 1 ) \n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Message msg = new Message();
        final Properties props = new Properties();
        props.put("timestamp", 99);
        props.put("duration", 52);
        msg.setProperties(props);
        final EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
        assertEquals(98, efh.getStartTimestamp());
        assertEquals(53, efh.getDuration());
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) Test(org.junit.Test)

Example 27 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.

@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() {
    // DROOLS-1017
    final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\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();
        clock.setStartupTime(5000L);
        final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
        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 28 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class CepEspTest method testDisconnectedEventFactHandle.

@Test
public void testDisconnectedEventFactHandle() {
    // DROOLS-924
    final String drl = "declare String \n" + "  @role(event)\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
        final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
        FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
        assertTrue("FactHandle not deserialized as EventFactHandle", key instanceof EventFactHandle);
        assertEquals("hello", ksession.getObject(key));
        key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
        assertTrue("FactHandle not deserialized as EventFactHandle", key instanceof EventFactHandle);
        assertEquals("goodbye", ksession.getObject(key));
    } finally {
        ksession.dispose();
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 29 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class CepEspTest method testEventExpiration4.

@Test(timeout = 10000)
public void testEventExpiration4() {
    final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + "    @role( event )\n" + "    @expires( 10s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + "   $st1 : StockTick( company == \"ACME\" ) over window:time( 10s ) from entry-point \"Event Stream\"\n" + "then\n" + "   results.add( $st1 );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
        final SessionPseudoClock clock = ksession.getSessionClock();
        final List results = new ArrayList();
        ksession.setGlobal("results", results);
        final 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();
        assertEquals(1, results.size());
        assertTrue(handle1.isExpired());
        assertFalse(ksession.getFactHandles().contains(handle1));
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 30 with EventFactHandle

use of org.drools.core.common.EventFactHandle in project drools by kiegroup.

the class CepEspTest method testSimpleTimeWindow.

@Test(timeout = 10000)
public void testSimpleTimeWindow() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleTimeWindow.drl");
    final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        wm.setGlobal("results", results);
        final SessionPseudoClock clock = wm.getSessionClock();
        // 5 seconds
        clock.advanceTime(5, TimeUnit.SECONDS);
        final EventFactHandle handle1 = (EventFactHandle) wm.insert(new OrderEvent("1", "customer A", 70));
        assertEquals(5000, handle1.getStartTimestamp());
        assertEquals(0, handle1.getDuration());
        wm.fireAllRules();
        assertEquals(1, results.size());
        assertEquals(70, ((Number) results.get(0)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle2 = (EventFactHandle) wm.insert(new OrderEvent("2", "customer A", 60));
        assertEquals(15000, handle2.getStartTimestamp());
        assertEquals(0, handle2.getDuration());
        wm.fireAllRules();
        assertEquals(2, results.size());
        assertEquals(65, ((Number) results.get(1)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle3 = (EventFactHandle) wm.insert(new OrderEvent("3", "customer A", 50));
        assertEquals(25000, handle3.getStartTimestamp());
        assertEquals(0, handle3.getDuration());
        wm.fireAllRules();
        assertEquals(3, results.size());
        assertEquals(60, ((Number) results.get(2)).intValue());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle4 = (EventFactHandle) wm.insert(new OrderEvent("4", "customer A", 25));
        assertEquals(35000, handle4.getStartTimestamp());
        assertEquals(0, handle4.getDuration());
        wm.fireAllRules();
        // first event should have expired, making average under the rule threshold, so no additional rule fire
        assertEquals(3, results.size());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle5 = (EventFactHandle) wm.insert(new OrderEvent("5", "customer A", 70));
        assertEquals(45000, handle5.getStartTimestamp());
        assertEquals(0, handle5.getDuration());
        wm.fireAllRules();
        // still under the threshold, so no fire
        assertEquals(3, results.size());
        // advance clock and assert new data
        // 10 seconds
        clock.advanceTime(10, TimeUnit.SECONDS);
        final EventFactHandle handle6 = (EventFactHandle) wm.insert(new OrderEvent("6", "customer A", 115));
        assertEquals(55000, handle6.getStartTimestamp());
        assertEquals(0, handle6.getDuration());
        wm.fireAllRules();
        assertEquals(4, results.size());
        assertEquals(70, ((Number) results.get(3)).intValue());
    } finally {
        wm.dispose();
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.testcoverage.common.model.OrderEvent) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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