Search in sources :

Example 46 with EntryPoint

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

the class WindowTest method testDeclaredLengthWindowInQuery.

@Test
public void testDeclaredLengthWindowInQuery() {
    EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
    TestEvent event;
    for (int i = 1; i <= 10; i++) {
        event = new TestEvent(null, "lengthDec", null);
        entryPoint.insert(event);
        assertEquals((i < 5 ? i : 5), ((Long) ksession.getQueryResults("TestDeclaredLengthWindow").iterator().next().get("$eventCount")).intValue());
    }
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 47 with EntryPoint

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

the class MultithreadTest method testRaceOnAccumulateNodeSimple.

@Test(timeout = 5000)
public void testRaceOnAccumulateNodeSimple() throws InterruptedException {
    final String drl = "package org.drools.integrationtests;\n" + "" + "import " + Server.class.getCanonicalName() + ";\n" + "import " + IntEvent.class.getCanonicalName() + ";\n" + "" + "declare IntEvent\n" + "  @role ( event )\n" + "  @expires( 15s )\n" + "end\n" + "\n" + "" + "rule \"average temperature\"\n" + "when\n" + "  $s : Server (hostname == \"hiwaesdk\")\n" + " $avg := Number( ) from accumulate ( " + "      IntEvent ( $temp : data ) over window:length(10) from entry-point ep01; " + "      average ($temp)\n" + "  )\n" + "then\n" + "  $s.avgTemp = $avg.intValue();\n" + "  System.out.println( $avg );\n" + "end\n" + "\n";
    final KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconfig.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconfig, drl);
    final KieSession session = kbase.newKieSession();
    final EntryPoint ep01 = session.getEntryPoint("ep01");
    final Runner t = new Runner(session);
    t.start();
    Thread.sleep(1000);
    final Server hiwaesdk = new Server("hiwaesdk");
    session.insert(hiwaesdk);
    final long LIMIT = 20;
    for (long i = LIMIT; i > 0; i--) {
        // Thread.sleep (0x1); }
        ep01.insert(new IntEvent((int) i));
        if (i % 1000 == 0) {
            System.out.println(i);
        }
    }
    try {
        Thread.sleep(1000);
        System.out.println("Halting ..");
        session.halt();
    } catch (final Exception e) {
        Assertions.fail(e.getMessage());
    }
    if (t.getError() != null) {
        Assertions.fail(t.getError().getMessage());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 48 with EntryPoint

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

the class MultithreadTest method testSlidingTimeWindows.

@Test(timeout = 1000000)
public void testSlidingTimeWindows() {
    final String str = "package org.drools\n" + "global java.util.List list; \n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "" + "declare StockTick @role(event) end\n" + "" + "rule R\n" + "when\n" + " accumulate( $st : StockTick() over window:time(400ms)\n" + "             from entry-point X,\n" + "             $c : count(1) )" + "then\n" + "   list.add( $c ); \n" + "end \n";
    final List<Exception> errors = new ArrayList<Exception>();
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconf, str);
    final KieSession ksession = kbase.newKieSession();
    final EntryPoint ep = ksession.getEntryPoint("X");
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {

        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    // runs for 10 seconds
    final int RUN_TIME = 5000;
    final int THREAD_NR = 2;
    final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
    ecs.submit(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            try {
                ksession.fireUntilHalt();
                return true;
            } catch (final Exception e) {
                errors.add(e);
                e.printStackTrace();
                return false;
            }
        }
    });
    for (int i = 0; i < THREAD_NR; i++) {
        ecs.submit(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                try {
                    final String s = Thread.currentThread().getName();
                    final long endTS = System.currentTimeMillis() + RUN_TIME;
                    int j = 0;
                    long lastTimeInserted = -1;
                    while (System.currentTimeMillis() < endTS) {
                        final long currentTimeInMillis = System.currentTimeMillis();
                        if (currentTimeInMillis > lastTimeInserted) {
                            lastTimeInserted = currentTimeInMillis;
                            ep.insert(new StockTick(j++, s, 0.0, 0));
                        }
                    }
                    return true;
                } catch (final Exception e) {
                    errors.add(e);
                    e.printStackTrace();
                    return false;
                }
            }
        });
    }
    boolean success = true;
    for (int i = 0; i < THREAD_NR; i++) {
        try {
            success = ecs.take().get() && success;
        } catch (final Exception e) {
            errors.add(e);
        }
    }
    ksession.halt();
    try {
        success = ecs.take().get() && success;
    } catch (final Exception e) {
        errors.add(e);
    }
    for (final Exception e : errors) {
        e.printStackTrace();
    }
    Assertions.assertThat(errors).isEmpty();
    Assertions.assertThat(success).isTrue();
    ksession.dispose();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) Executor(java.util.concurrent.Executor) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 49 with EntryPoint

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

the class StreamsTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPoint.drl");
    // final RuleBase ruleBase = loadRuleBase( reader );
    KieSessionConfiguration conf = SessionConfiguration.newInstance();
    ((SessionConfiguration) conf).setClockType(ClockType.PSEUDO_CLOCK);
    KieSession session = kbase.newKieSession(conf, null);
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis());
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    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());
    session.fireAllRules();
    assertEquals(0, results.size());
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 15, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("StockStream");
    InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
    InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
    InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
    InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
    assertNotNull(handle5);
    assertNotNull(handle6);
    assertNotNull(handle7);
    assertNotNull(handle8);
    assertTrue(handle5.isEvent());
    assertTrue(handle6.isEvent());
    assertTrue(handle7.isEvent());
    assertTrue(handle8.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertSame(tick7, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Example 50 with EntryPoint

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

the class StreamsTest method testWindowDeclaration.

public void testWindowDeclaration() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + "    StockTick( company == 'RHT' )\n" + "               over window:length(5)\n" + "               from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + "    accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + "                $sum : sum( $s.getPrice() ),\n" + "                $cnt : count( $s ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep = ksession.getEntryPoint("ticks");
    // not in the window
    ep.insert(new StockTick(1, "ACME", 20, 1000));
    // not > 20
    ep.insert(new StockTick(2, "RHT", 20, 1000));
    ep.insert(new StockTick(3, "RHT", 30, 1000));
    // not in the window
    ep.insert(new StockTick(4, "ACME", 30, 1000));
    ep.insert(new StockTick(5, "RHT", 25, 1000));
    // not in the window
    ep.insert(new StockTick(6, "ACME", 10, 1000));
    // not > 20
    ep.insert(new StockTick(7, "RHT", 10, 1000));
    ep.insert(new StockTick(8, "RHT", 40, 1000));
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(95));
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession)

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