Search in sources :

Example 16 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class CepEspTest method testSerializationBeforeFireWithWindowLength.

@Test
public void testSerializationBeforeFireWithWindowLength() {
    // DROOLS-953
    final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + "    $e : StockTick() over window:length(1)\n" + "then\n" + "    list.add($e.getCompany());\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        List<String> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        ksession.insert(new StockTick(1, "ACME", 50));
        ksession.insert(new StockTick(2, "DROO", 50));
        ksession.insert(new StockTick(3, "JBPM", 50));
        try {
            ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        list = new ArrayList<>();
        ksession.setGlobal("list", list);
        ksession.fireAllRules();
        assertEquals(1, list.size());
        assertEquals("JBPM", list.get(0));
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class CepEspTest method testEventExpiration.

private void testEventExpiration(final String afterBoundary, final String windowTime) {
    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" + "    @timestamp( dateTimestamp )\n" + "    // this will override individual rule requirements\n" + "    @expires( 10m )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + "    $st1 : StockTick( company == \"ACME\" )\n" + "    $st2 : StockTick( company == \"RHT\", this after[0," + afterBoundary + "] $st1 )\n" + "then\n" + "    results.add( $st1 );\n" + "end\n" + "\n" + "rule \"Check event2\"\n" + "when\n" + "    $st1 : StockTick( company == \"RHT\" ) over window:time( " + windowTime + " )\n" + "then\n" + "    results.add( $st1 );\n" + "end\n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((RuleBase) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    final ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) StockTick(org.drools.testcoverage.common.model.StockTick) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode)

Example 18 with StockTick

use of org.drools.testcoverage.common.model.StockTick 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 19 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class CepEspNegativeCloudTest method testCloudModeExpiration.

@Test(timeout = 10000)
public void testCloudModeExpiration() throws InstantiationException, IllegalAccessException, InterruptedException {
    final String drl = "package org.drools.cloud\n" + "import " + StockTick.class.getCanonicalName() + "\n" + "declare Event\n" + "        @role ( event )\n" + "        name : String\n" + "        value : Object\n" + "end\n" + "declare AnotherEvent\n" + "        @role ( event )\n" + "        message : String\n" + "        type : String\n" + "end\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule \"two events\"\n" + "    when\n" + "        Event( value != null ) from entry-point X\n" + "        StockTick( company != null ) from entry-point X\n" + "    then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final EntryPoint ep = ksession.getEntryPoint("X");
        ep.insert(new StockTick(1, "RHT", 10, 1000));
        int rulesFired = ksession.fireAllRules();
        assertEquals(0, rulesFired);
        final FactType event = kbase.getFactType("org.drools.cloud", "Event");
        final Object e1 = event.newInstance();
        event.set(e1, "name", "someKey");
        event.set(e1, "value", "someValue");
        ep.insert(e1);
        rulesFired = ksession.fireAllRules();
        assertEquals(1, rulesFired);
        // let some time be spent
        Thread.sleep(1000);
        // check both events are still in memory as we are running in CLOUD mode
        assertEquals(2, ep.getFactCount());
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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 20 with StockTick

use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.

the class CepEspTest method testRetractFromWindow.

@Test
public void testRetractFromWindow() {
    // DROOLS-636
    final String drl = "import " + StockTick.class.getCanonicalName() + ";\n " + "declare StockTick\n" + " @role( event )\n" + "end\n" + "rule R1 when\n" + "    $i: Integer()\n" + "    $s: StockTick( price > 10 )\n" + "then\n" + "    modify($s) { setPrice(8) };\n" + "end\n" + "rule R2 when\n" + "    $s: StockTick( price > 15 ) over window:length(1)\n" + "then\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        ksession.insert(42);
        ksession.insert(new StockTick(1L, "DROOLS", 20));
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
}
Also used : StockTick(org.drools.testcoverage.common.model.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

StockTick (org.drools.testcoverage.common.model.StockTick)53 KieBase (org.kie.api.KieBase)52 Test (org.junit.Test)51 KieSession (org.kie.api.runtime.KieSession)51 ArrayList (java.util.ArrayList)31 EntryPoint (org.kie.api.runtime.rule.EntryPoint)19 List (java.util.List)15 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 InternalFactHandle (org.drools.core.common.InternalFactHandle)12 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)9 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)9 IOException (java.io.IOException)8 ParseException (java.text.ParseException)8 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)7 EventFactHandle (org.drools.core.common.EventFactHandle)5 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)4 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)3 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)3 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2