Search in sources :

Example 86 with EventFactHandle

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

the class CepEspTest method testEventAssertionWithDuration.

@Test(timeout = 10000)
public void testEventAssertionWithDuration() {
    final String drl = "package org.drools.compiler\n" + "\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + "    @role( event )\n" + "    @duration( duration )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st : StockTick( company == \"ACME\" )\n" + "then\n" + "    results.add( $st );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List results = new ArrayList();
        session.setGlobal("results", results);
        final StockTick tick1 = new StockTick(1, "DROO", 50, 10000, 5);
        final StockTick tick2 = new StockTick(2, "ACME", 10, 11000, 10);
        final StockTick tick3 = new StockTick(3, "ACME", 10, 12000, 8);
        final StockTick tick4 = new StockTick(4, "DROO", 50, 13000, 7);
        final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
        final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
        final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
        final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
        assertNotNull(handle1);
        assertNotNull(handle2);
        assertNotNull(handle3);
        assertNotNull(handle4);
        assertTrue(handle1.isEvent());
        assertTrue(handle2.isEvent());
        assertTrue(handle3.isEvent());
        assertTrue(handle4.isEvent());
        final EventFactHandle eh1 = (EventFactHandle) handle1;
        final EventFactHandle eh2 = (EventFactHandle) handle2;
        final EventFactHandle eh3 = (EventFactHandle) handle3;
        final EventFactHandle eh4 = (EventFactHandle) handle4;
        assertEquals(tick1.getTime(), eh1.getStartTimestamp());
        assertEquals(tick2.getTime(), eh2.getStartTimestamp());
        assertEquals(tick3.getTime(), eh3.getStartTimestamp());
        assertEquals(tick4.getTime(), eh4.getStartTimestamp());
        assertEquals(tick1.getDuration(), eh1.getDuration());
        assertEquals(tick2.getDuration(), eh2.getDuration());
        assertEquals(tick3.getDuration(), eh3.getDuration());
        assertEquals(tick4.getDuration(), eh4.getDuration());
        session.fireAllRules();
        assertEquals(2, results.size());
    } finally {
        session.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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