Search in sources :

Example 66 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testStreamModeNoSerialization.

@Test(timeout = 10000)
public void testStreamModeNoSerialization() throws IOException, ClassNotFoundException {
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_StreamMode.drl");
    KieBase kbase2 = (KieBase) DroolsStreamUtils.streamIn(DroolsStreamUtils.streamOut(kbase1), null);
    final KieSession ksession1 = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession1.addEventListener(ael1);
    final KieSession ksession2 = kbase2.newKieSession();
    AgendaEventListener ael2 = mock(AgendaEventListener.class);
    ksession2.addEventListener(ael2);
    // -------------
    // first, check the non-serialized session
    // -------------
    ksession1.insert(new Sensor(10, 10));
    ksession1.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> aafe1 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael1, times(1)).afterMatchFired(aafe1.capture());
    List<AfterMatchFiredEvent> events1 = aafe1.getAllValues();
    assertThat(events1.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
    ksession1.insert(new Sensor(20, 20));
    ksession1.fireAllRules();
    verify(ael1, times(2)).afterMatchFired(aafe1.capture());
    events1 = aafe1.getAllValues();
    assertThat(events1.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
    ksession1.insert(new Sensor(30, 30));
    ksession1.fireAllRules();
    verify(ael1, times(3)).afterMatchFired(aafe1.capture());
    assertThat(events1.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
    ksession1.dispose();
    // -------------
    // now we check the serialized session
    // -------------
    ArgumentCaptor<AfterMatchFiredEvent> aafe2 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    ksession2.insert(new Sensor(10, 10));
    ksession2.fireAllRules();
    verify(ael2, times(1)).afterMatchFired(aafe2.capture());
    List<AfterMatchFiredEvent> events2 = aafe2.getAllValues();
    assertThat(events2.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
    ksession2.insert(new Sensor(20, 20));
    ksession2.fireAllRules();
    verify(ael2, times(2)).afterMatchFired(aafe2.capture());
    events2 = aafe2.getAllValues();
    assertThat(events2.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
    ksession2.insert(new Sensor(30, 30));
    ksession2.fireAllRules();
    verify(ael2, times(3)).afterMatchFired(aafe2.capture());
    assertThat(events2.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
    ksession2.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 67 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testCoincidesOnArbitraryDates.

@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
    100000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 50 milliseconds after DROO
    100050, 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(tick1, results.get(0));
    assertEquals(tick2, results.get(1));
    assertEquals(tick1, results.get(2));
    assertEquals(tick2, results.get(3));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 68 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testDuplicateFiring2.

@Test
public void testDuplicateFiring2() throws InterruptedException {
    String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule Tick when $s : StockTick() then System.out.println( $s ); end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "\t$n: Number ( intValue > 0 ) from accumulate ( $e: StockTick() over window:time(3s), count($e))\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    // Check the builder for errors
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // configure knowledge base
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.CLOUD);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // init session clock
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    // init stateful knowledge session
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    SessionPseudoClock clock = ksession.getSessionClock();
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    // insert events
    for (int i = 1; i < 3; i++) {
        StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
        clock.advanceTime(1001, TimeUnit.MILLISECONDS);
        ksession.insert(event);
        System.out.println(i + ". rule invocation");
        ksession.fireAllRules();
    }
    clock.advanceTime(3001, TimeUnit.MILLISECONDS);
    StockTick event = new StockTick(3, "XXX", 1.0, 0);
    System.out.println("3. rule invocation");
    ksession.insert(event);
    ksession.fireAllRules();
    clock.advanceTime(3001, TimeUnit.MILLISECONDS);
    StockTick event2 = new StockTick(3, "XXX", 1.0, 0);
    System.out.println("4. rule invocation");
    ksession.insert(event2);
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(Arrays.asList(1L, 2L, 1L, 1L), list);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 69 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testBeforeOnArbitraryDates.

@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
    104000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
    100000, 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(tick1, results.get(0));
    assertEquals(tick2, results.get(1));
    assertEquals(tick1, results.get(2));
    assertEquals(tick2, results.get(3));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 70 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DeclarativeAgendaTest method testCancelActivation.

@Test(timeout = 10000)
public void testCancelActivation() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    kcontext.cancelMatch( $i ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    final List cancelled = new ArrayList();
    ksession.addEventListener(new AgendaEventListener() {

        public void beforeMatchFired(BeforeMatchFiredEvent event) {
        }

        public void agendaGroupPushed(AgendaGroupPushedEvent event) {
        }

        public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
        }

        public void matchCreated(MatchCreatedEvent event) {
        }

        public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
        }

        public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event);
        }
    });
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(0, list.size());
    assertEquals(1, cancelled.size());
    assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
    ksession.dispose();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) RuleFlowGroupDeactivatedEvent(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) RuleFlowGroupActivatedEvent(org.kie.api.event.rule.RuleFlowGroupActivatedEvent) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) BeforeMatchFiredEvent(org.kie.api.event.rule.BeforeMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) AgendaGroupPushedEvent(org.kie.api.event.rule.AgendaGroupPushedEvent) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7