Search in sources :

Example 51 with EntryPoint

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

the class StreamsTest method testEntryPointWithAccumulateAndMVEL.

@Test(timeout = 10000)
public void testEntryPointWithAccumulateAndMVEL() throws Exception {
    String str = "package org.drools.compiler\n" + "rule R1 dialect 'mvel'\n" + "    when\n" + "        $n : Number() from accumulate( \n" + "                 StockTick() from entry-point ep1,\n" + "                 count(1))" + "    then\n" + "end\n";
    // read in the source
    KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
    KieSession ksession = createKnowledgeSession(kbase);
    org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep1 = ksession.getEntryPoint("ep1");
    ep1.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(1, rulesFired);
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 52 with EntryPoint

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

the class StreamsTest method testModifyRetracOnEntryPointFacts.

@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_modifyRetractEntryPoint.drl");
    KieSession session = kbase.newKieSession();
    final List<? extends Number> results = new ArrayList<Number>();
    session.setGlobal("results", results);
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("stream1");
    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();
    System.out.println(results);
    assertEquals(2, results.size());
    assertEquals(30, ((Number) results.get(0)).intValue());
    assertEquals(110, ((Number) results.get(1)).intValue());
    // the 3 non-matched facts continue to exist in the entry point
    assertEquals(3, entry.getObjects().size());
    // but no fact was inserted into the main session
    assertEquals(0, session.getObjects().size());
}
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) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 53 with EntryPoint

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

the class StreamsTest method testWindowDeclaration2.

@Test(timeout = 10000)
public void testWindowDeclaration2() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare Double\n" + "    @role(event)\n" + "end\n" + "declare window Streem\n" + "    Double() over window:length( 10 ) from entry-point data\n" + "end\n" + "rule \"See\"\n" + "when\n" + "    $sum : Double() from accumulate (\n" + "        $d: Double()\n" + "            from window Streem,\n" + "        sum( $d )\n" + "    )\n" + "then\n" + "end";
    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("data");
    ep.insert(Double.valueOf(10));
    ep.insert(Double.valueOf(11));
    ep.insert(Double.valueOf(12));
    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(33));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 54 with EntryPoint

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

the class StreamsTest method testGetEntryPointList.

@Test(timeout = 10000)
public void testGetEntryPointList() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
    KieSession session = kbase.newKieSession();
    EntryPoint def = session.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
    EntryPoint s1 = session.getEntryPoint("stream1");
    EntryPoint s2 = session.getEntryPoint("stream2");
    EntryPoint s3 = session.getEntryPoint("stream3");
    Collection<? extends EntryPoint> eps = session.getEntryPoints();
    assertEquals(4, eps.size());
    assertTrue(eps.contains(def));
    assertTrue(eps.contains(s1));
    assertTrue(eps.contains(s2));
    assertTrue(eps.contains(s3));
}
Also used : KieBase(org.kie.api.KieBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 55 with EntryPoint

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

the class StreamsTest method testEntryPointReference.

// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
    KieSession session = kbase.newKieSession();
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("stream1");
    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) InternalFactHandle(org.drools.core.common.InternalFactHandle) 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