Search in sources :

Example 21 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class CepEspTest method testTimedRuleWithAccumulate.

@Test
@Ignore("Cannot reproduce with pseudoclock and takes too long with system clock")
public void testTimedRuleWithAccumulate() {
    // BZ-1083103
    String drl = "import " + SynthEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare SynthEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "end\n" + "\n" + "declare EventCounter\n" + "      @role( event )\n" + "      @timestamp( timestamp )\n" + "      id          : long\n" + "      key         : String\n" + "      timestamp   : Date\n" + "end\n" + "\n" + "rule \"Create counter\"\n" + "when\n" + "$e : SynthEvent() from entry-point \"synth\"\n" + "then\n" + "    entryPoints[\"counters\"].insert(new EventCounter( $e.getId(), \"event\", $e.getTimestamp() ) );\n" + "end\n" + "\n" + "rule \"Count epm\"\n" + "timer ( cron: 0/10 * * * * ? )\n" + // "timer ( int: 2s 1s )\n" +
    "when\n" + "    Number( $count : intValue ) from accumulate(\n" + "       EventCounter( key == \"event\" ) over window:time( 60s ) from entry-point \"counters\", count(1) )\n" + "then\n" + "    System.out.println(\"[\" + new Date() + \"] epm = \" + $count );\n" + "end\n " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = kbase.newKieSession();
    EntryPoint synthEP = ksession.getEntryPoint("synth");
    new Thread() {

        public void run() {
            System.out.println("[" + new Date() + "] start!");
            ksession.fireUntilHalt();
        }
    }.start();
    long counter = 0;
    while (true) {
        counter++;
        synthEP.insert(new SynthEvent(counter));
        try {
            Thread.sleep(20L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if ((counter % 1000) == 0) {
            System.out.println("Total events: " + counter);
        }
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Date(java.util.Date) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class CepEspTest method testEventExpiration4.

@Test(timeout = 10000)
public void testEventExpiration4() throws Exception {
    // read in the source
    final KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration4.drl");
    final KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    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();
    assertTrue(results.size() == 1);
    assertTrue(handle1.isExpired());
    assertFalse(ksession.getFactHandles().contains(handle1));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 23 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class FusionAfterBeforeTest method testAfterBeforeOperators.

@Test
public void testAfterBeforeOperators() {
    final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("fusionAfterBeforeTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    final KieSession ksession = kieBase.newKieSession(ksconf, null);
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final EntryPoint stream = ksession.getEntryPoint("EventStream");
    SessionPseudoClock clock = ksession.getSessionClock();
    try {
        for (int i = 0; i < 3; i++) {
            final MessageEvent tc = new MessageEvent(MessageEvent.Type.received, new Message());
            stream.insert(tc);
            ksession.fireAllRules();
            clock.advanceTime(8, TimeUnit.SECONDS);
        }
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
    Assertions.assertThat(listener.isRuleFired("AfterMessageEvent")).as("Rule 'AfterMessageEvent' was no fired!").isTrue();
    Assertions.assertThat(listener.isRuleFired("BeforeMessageEvent")).as("Rule 'BeforeMessageEvent' was no fired!").isTrue();
    // each rules should be fired 2 times
    int firedCount = 2;
    int actuallyFired = listener.ruleFiredCount("AfterMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'AfterMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
    firedCount = listener.ruleFiredCount("BeforeMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'BeforeMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
}
Also used : Message(org.drools.testcoverage.common.model.Message) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Resource(org.kie.api.io.Resource) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 24 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class DataDictionaryTest method testEntryPoints.

@Test
public void testEntryPoints() throws Exception {
    setKSession(getModelSession(source, VERBOSE));
    setKbase(getKSession().getKieBase());
    FactType gender = getKbase().getFactType(packageName, "Gender");
    FactType noclaims = getKbase().getFactType(packageName, "NoOfClaims");
    FactType scrambled = getKbase().getFactType(packageName, "Scrambled");
    FactType domicile = getKbase().getFactType(packageName, "Domicile");
    FactType agecar = getKbase().getFactType(packageName, "AgeOfCar");
    FactType amklaims = getKbase().getFactType(packageName, "AmountOfClaims");
    assertEquals(7, getKSession().getEntryPoints().size());
    EntryPoint gender_ep = getKSession().getEntryPoint("in_Gender");
    Assert.assertNotNull(gender_ep);
    EntryPoint noclaims_ep = getKSession().getEntryPoint("in_NoOfClaims");
    Assert.assertNotNull(noclaims_ep);
    EntryPoint scrambled_ep = getKSession().getEntryPoint("in_Scrambled");
    Assert.assertNotNull(scrambled_ep);
    EntryPoint domicile_ep = getKSession().getEntryPoint("in_Domicile");
    Assert.assertNotNull(domicile_ep);
    EntryPoint agecar_ep = getKSession().getEntryPoint("in_AgeOfCar");
    Assert.assertNotNull(agecar_ep);
    EntryPoint amklaims_ep = getKSession().getEntryPoint("in_AmountOfClaims");
    Assert.assertNotNull(amklaims_ep);
    gender_ep.insert("M");
    noclaims_ep.insert("> 4");
    scrambled_ep.insert(4);
    domicile_ep.insert("way out");
    agecar_ep.insert(new Double("3.4"));
    amklaims_ep.insert(9);
    getKSession().fireAllRules();
    assertEquals(6, getKSession().getObjects().size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(gender.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(noclaims.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(scrambled.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(domicile.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(agecar.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(amklaims.getFactClass())).size());
    checkFirstDataFieldOfTypeStatus(amklaims, true, false, null, 9);
    checkFirstDataFieldOfTypeStatus(domicile, false, false, null, "way out");
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) EntryPoint(org.kie.api.runtime.rule.EntryPoint) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 25 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class LifecycleTest method testExpires.

@Test
public void testExpires() throws Exception {
    EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
    StockTick event = new StockTick();
    FactHandle handle = entryPoint.insert(event);
    assertTrue(entryPoint.getFactHandles().contains(handle));
    kieSession.fireAllRules();
    assertTrue(entryPoint.getFactHandles().contains(handle));
    advanceTime(5, TimeUnit.SECONDS);
    kieSession.fireAllRules();
    assertFalse(entryPoint.getFactHandles().contains(handle));
}
Also used : StockTick(org.drools.compiler.StockTick) FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

EntryPoint (org.kie.api.runtime.rule.EntryPoint)70 Test (org.junit.Test)50 KieSession (org.kie.api.runtime.KieSession)46 ArrayList (java.util.ArrayList)26 KieBase (org.kie.api.KieBase)26 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)19 StockTick (org.drools.compiler.StockTick)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)16 FactHandle (org.kie.api.runtime.rule.FactHandle)15 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)13 RegistryContext (org.drools.core.command.impl.RegistryContext)12 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)8 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)6 EventFactHandle (org.drools.core.common.EventFactHandle)6 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)6 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)6