Search in sources :

Example 16 with EntryPoint

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

the class MTEntryPointsTest method testTwoEntryPoints.

/**
 * Inserts events using multiple threads into two EntryPoints. The insert
 * operation is synchronized on corresponding SessionEntryPoint instance.
 */
@Test
public void testTwoEntryPoints() throws Exception {
    final EntryPoint firstThreadEntryPoint = kieSession.getEntryPoint("FirstStream");
    final EntryPoint secondThreadEntryPoint = kieSession.getEntryPoint("SecondStream");
    int numInsertersInEachEntryPoint = 10;
    int numThreadPoolCapacity = numInsertersInEachEntryPoint * 2;
    ExecutorService executorService = Executors.newFixedThreadPool(numThreadPoolCapacity);
    List<Future<?>> futures = new ArrayList<Future<?>>();
    for (int i = 0; i < numInsertersInEachEntryPoint; i++) {
        // working only with first stream, future for exception watching
        Future<?> futureForFirstThread = executorService.submit(new TestInserter(kieSession, firstThreadEntryPoint));
        futures.add(futureForFirstThread);
        // working only with second stream, future for exception watching
        Future<?> futureForSecondThread = executorService.submit(new TestInserter(kieSession, secondThreadEntryPoint));
        futures.add(futureForSecondThread);
    }
    try {
        for (Future<?> f : futures) {
            f.get(30, TimeUnit.SECONDS);
        }
    } catch (ExecutionException ex) {
        throw ex;
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 17 with EntryPoint

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

the class CepEspTest method testTemporalOperatorsInfinity.

@Test(timeout = 10000)
public void testTemporalOperatorsInfinity() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators3.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(sconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    EntryPoint ep = ksession.getEntryPoint("X");
    clock.advanceTime(1000, TimeUnit.SECONDS);
    int rules = 0;
    ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    // int rules = ksession.fireAllRules();
    System.out.println(list);
    ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    // rules = ksession.fireAllRules();
    System.out.println(list);
    ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    rules = ksession.fireAllRules();
    System.out.println(list);
    assertEquals(3, rules);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) 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) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 18 with EntryPoint

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

the class CepEspTest method testIdentityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testIdentityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
    StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    kbconf.setOption(EqualityBehaviorOption.IDENTITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession.addEventListener(ael1);
    EntryPoint ep1 = ksession.getEntryPoint("stocktick stream");
    FactHandle fh1 = ep1.insert(st1);
    FactHandle fh1_2 = ep1.insert(st1);
    FactHandle fh2 = ep1.insert(st2);
    FactHandle fh3 = ep1.insert(st3);
    assertSame(fh1, fh1_2);
    assertNotSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    assertNotSame(fh2, fh3);
    ksession.fireAllRules();
    // must have fired 3 times, one for each event identity
    verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) 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) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 19 with EntryPoint

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

the class CepEspTest method testCloudModeExpiration.

@Test(timeout = 10000)
public void testCloudModeExpiration() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException {
    String str = "package org.drools.cloud\n" + "import org.drools.compiler.*\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";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.CLOUD);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSession ksession = createKnowledgeSession(kbase);
    EntryPoint ep = ksession.getEntryPoint("X");
    ep.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(0, rulesFired);
    org.kie.api.definition.type.FactType event = kbase.getFactType("org.drools.cloud", "Event");
    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.currentThread().sleep(1000);
    // check both events are still in memory as we are running in CLOUD mode
    assertEquals(2, ep.getFactCount());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 20 with EntryPoint

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

the class CepEspTest method testUpdateEventThroughEntryPoint.

@Test
public void testUpdateEventThroughEntryPoint() throws Exception {
    String drl = "import org.drools.compiler.integrationtests.CepEspTest.TestEvent\n" + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "    @expires( 4s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "    no-loop\n" + "    when\n" + "        $event : TestEvent ( name != null ) over window:time( 4s ) from entry-point EventStream\n" + "    then\n" + "        // insert( new Message( $event.getValue().toString() ) );\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kieModule = ks.newKieModuleModel();
    kieModule.newKieBaseModel("KBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("KSession").setDefault(true);
    kfs.writeKModuleXML(kieModule.toXML());
    kfs.write("src/main/resources/lifecycle.drl", drl);
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
    TestEvent event = new TestEvent("testEvent1");
    FactHandle handle = entryPoint.insert(event);
    TestEvent event2 = new TestEvent("testEvent2");
    entryPoint.update(handle, event2);
    // make sure the event is in the entry-point
    assertFalse(entryPoint.getObjects().contains(event));
    assertTrue(entryPoint.getObjects().contains(event2));
    assertEquals(entryPoint.getObject(handle), event2);
    kieSession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) 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) KieModuleModel(org.kie.api.builder.model.KieModuleModel) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) 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