Search in sources :

Example 31 with KieBaseConfiguration

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

the class CepEspTest method testWindowExpireActionDeserialization.

@Test
public void testWindowExpireActionDeserialization() throws InterruptedException {
    String drl = "package org.drools.test;\n" + "import org.drools.compiler.StockTick; \n" + "global java.util.List list; \n" + "\n" + "declare StockTick\n" + "  @role( event )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  StockTick( $id : seq, company == \"BBB\" ) over window:time( 1s )\n" + "then\n" + "  list.add( $id );\n" + "end\n" + "\n" + "";
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
    KieSession ks = kb.newKieSession();
    ks.insert(new StockTick(2, "BBB", 1.0, 0));
    Thread.sleep(1500);
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ArrayList list = new ArrayList();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    ks.insert(new StockTick(3, "BBB", 1.0, 0));
    ks.fireAllRules();
    System.out.print(list);
    assertEquals(1, list.size());
    assertEquals(Arrays.asList(3L), list);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 32 with KieBaseConfiguration

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

the class CepEspTest method testEventTimestamp.

@Test
public void testEventTimestamp() {
    // DROOLS-268
    String drl = "\n" + "import org.drools.compiler.integrationtests.CepEspTest.Event; \n" + "global java.util.List list; \n" + "global org.kie.api.time.SessionPseudoClock clock; \n" + "" + "declare Event \n" + " @role( event )\n" + " @timestamp( time ) \n" + " @expires( 10000000 ) \n" + "end \n" + "" + "" + "rule \"inform about E1\"\n" + "when\n" + " $event1 : Event( type == 1 )\n" + " //there is an event (T2) with value 0 between 0,2m after doorClosed\n" + " $event2: Event( type == 2, value == 1, this after [0, 1200ms] $event1, $timestamp : time )\n" + " //there is no newer event (T2) within the timeframe\n" + " not Event( type == 2, this after [0, 1200ms] $event1, time > $timestamp ) \n" + "then\n" + " list.add( clock.getCurrentTime() ); \n " + "end\n" + "\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());
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    // init stateful knowledge session
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    ksession.setGlobal("clock", clock);
    // 0
    ksession.insert(new Event(1, -1, clock.getCurrentTime()));
    clock.advanceTime(600, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // 600
    ksession.insert(new Event(2, 0, clock.getCurrentTime()));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // 700
    ksession.insert(new Event(2, 0, clock.getCurrentTime()));
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // 1000
    ksession.insert(new Event(2, 0, clock.getCurrentTime()));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // 1100
    ksession.insert(new Event(2, 1, clock.getCurrentTime()));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // 1300
    ksession.insert(new Event(2, 0, clock.getCurrentTime()));
    clock.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertFalse(list.isEmpty());
    assertEquals(1, list.size());
    Long time = (Long) list.get(0);
    assertTrue(time > 1000 && time < 1500);
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) AgendaGroupPoppedEvent(org.kie.api.event.rule.AgendaGroupPoppedEvent) StockTickEvent(org.drools.compiler.StockTickEvent) OrderEvent(org.drools.compiler.OrderEvent) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 33 with KieBaseConfiguration

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

the class CepEspTest method testTemporalOperatorWithConstantAndJoin.

@Test
public void testTemporalOperatorWithConstantAndJoin() throws Exception {
    // BZ 1096243
    String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "global java.util.List list" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( dateEvt )\n" + "end\n" + "\n" + "rule R \n" + "    when\n" + "        $e1 : SimpleEvent( this after \"01-Jan-2014\"  )\n" + "        $e2 : SimpleEvent( this after $e1 ) \n" + "    then\n" + "        list.add(\"1\");\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());
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    SimpleEvent event1 = new SimpleEvent("code1", DateUtils.parseDate("18-Mar-2014").getTime());
    ksession.insert(event1);
    SimpleEvent event2 = new SimpleEvent("code2", DateUtils.parseDate("19-Mar-2014").getTime());
    ksession.insert(event2);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 34 with KieBaseConfiguration

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

the class CepEspTest method testSalienceWithEventsPseudoClock.

@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() throws IOException, ClassNotFoundException {
    String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R2 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R3 salience 100\n" + "    when\n" + "        $s2 : StockTick( company == 'ACME' )\n" + "    then\n" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(ksconf, null);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick(1, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(2, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(3, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(4, "ACME", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    int rulesFired = ksession.fireAllRules();
    assertEquals(4, rulesFired);
    ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(captor.capture());
    List<AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 35 with KieBaseConfiguration

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

the class CepEspTest method testDelayingNotWithPreEpochClock.

@Test(timeout = 10000)
public void testDelayingNotWithPreEpochClock() throws Exception {
    String str = "package org.drools.compiler\n" + "declare A @role(event) symbol : String end\n" + "declare B @role(event) symbol : String end\n" + "rule Setup when\n" + "then\n" + "    insert( new A() );\n" + "end\n" + "rule X\n" + "when\n" + "    $a : A() and not( B( this after $a ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    // Getting a pre-epoch date (i.e., before 1970)
    Calendar ts = Calendar.getInstance();
    ts.set(1900, 1, 1);
    // Initializing the clock to that date
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(ts.getTimeInMillis(), TimeUnit.MILLISECONDS);
    // rule X should not be delayed as the delay would be infinite
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) Calendar(java.util.Calendar) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)151 Test (org.junit.Test)131 KieSession (org.kie.api.runtime.KieSession)116 KieBase (org.kie.api.KieBase)99 ArrayList (java.util.ArrayList)76 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)57 List (java.util.List)50 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 StockTick (org.drools.compiler.StockTick)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)31 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)24 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)21 FactHandle (org.kie.api.runtime.rule.FactHandle)20 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)17 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)14 StockTickInterface (org.drools.compiler.StockTickInterface)13 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)13