Search in sources :

Example 71 with KieSessionConfiguration

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

the class CepEspTest method testTemporalOperatorWithGlobal.

@Test
public void testTemporalOperatorWithGlobal() {
    // DROOLS-993
    String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "global " + SimpleEvent.class.getCanonicalName() + " baseEvent;\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( dateEvt )\n" + "end\n" + "\n" + "rule R \n" + "    when\n" + "        $e : SimpleEvent( dateEvt before[10s] baseEvent.dateEvt )\n" + "    then\n" + "        list.add(\"1\");\n" + "    end\n " + "";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.setGlobal("baseEvent", new SimpleEvent("1", 15000L));
    SimpleEvent event1 = new SimpleEvent("1", 0L);
    ksession.insert(event1);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 72 with KieSessionConfiguration

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

the class CepEspTest method testEventExpirationDuringAccumulate.

@Test(timeout = 10000)
public void testEventExpirationDuringAccumulate() throws Exception {
    // DROOLS-70
    String str = "package org.drools.integrationtests\n" + "\n" + "import java.util.List;\n" + "\n" + "declare Stock\n" + "    @role( event )\n" + "    @expires( 1s ) // setting to a large value causes the test to pass\n" + "    name : String\n" + "    value : Double\n" + "end\n" + "\n" + "rule \"collect events\"\n" + "when\n" + "    stocks := List()\n" + "        from accumulate( $zeroStock : Stock( value == 0.0 );\n" + "                         collectList( $zeroStock ) )\n" + "then\n" + "    // empty consequence\n" + "end";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieSession ksession = kbase.newKieSession(conf, null);
    final StockFactory stockFactory = new StockFactory(kbase);
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    final Future sessionFuture = executor.submit(new Runnable() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    });
    try {
        for (int iteration = 0; iteration < 100; iteration++) {
            this.populateSessionWithStocks(ksession, stockFactory);
        }
        // let the engine finish its job
        Thread.sleep(2000);
    } finally {
        ksession.halt();
        // not to swallow possible exception
        sessionFuture.get();
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) 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)

Example 73 with KieSessionConfiguration

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

the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.

@Test
public void testSerializationWithWindowLengthAndLiaSharing() throws Exception {
    // DROOLS-953
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + "    $e : StockTick() over window:length(1)\n" + "then\n" + "    list.add($e.getCompany());\n" + "end\n" + "\n" + "rule ReportEventInserted when\n" + "   $e : StockTick()\n" + "then\n" + "   System.out.println(\"Event Insert : \" + $e);\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new StockTick(1, "ACME", 50));
    ksession.insert(new StockTick(2, "DROO", 50));
    ksession.insert(new StockTick(3, "JBPM", 50));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("JBPM", list.get(0));
    try {
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(0, list.size());
}
Also used : StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 74 with KieSessionConfiguration

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

the class CepEspTest method testEventOffsetExpirationOverflow.

@Test
public void testEventOffsetExpirationOverflow() {
    // DROOLS-455
    String drl = "\n" + "import java.util.*; " + "" + "declare LongLastingEvent \n" + " @role( event )" + " @timestamp( start ) " + " @duration( duration ) " + "      start : long " + "      duration : long " + "end \n" + "" + "rule Insert " + "  when " + "  then " + "      insert( new LongLastingEvent( 100, Long.MAX_VALUE ) ); " + "  end " + " " + " " + "rule Collect \n" + "when\n" + " accumulate( $x: LongLastingEvent() over window:time(1h), $num : count($x) ) \n" + "then " + "end " + "";
    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);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    // generate the event
    ksession.fireAllRules();
    // move on..
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // The event should still be there...
    assertEquals(1, ksession.getObjects().size());
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 75 with KieSessionConfiguration

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

the class CepEspTest method testPseudoSchedulerRemoveJobTest.

@Test(timeout = 10000)
public void testPseudoSchedulerRemoveJobTest() {
    String str = "import " + CepEspTest.class.getName() + ".A\n";
    str += "declare A\n";
    str += "    @role( event )\n";
    str += "end\n";
    str += "rule A\n";
    str += "when\n";
    str += "   $a : A()\n";
    str += "   not A(this after [1s,10s] $a)\n";
    str += "then\n";
    str += "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(str)), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
    knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = knowledgeBase.newKieSession(sessionConfig, KieServices.get().newEnvironment());
    PseudoClockScheduler pseudoClock = ksession.<PseudoClockScheduler>getSessionClock();
    FactHandle h = ksession.insert(new A());
    ksession.retract(h);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) 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) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)204 KieSession (org.kie.api.runtime.KieSession)168 Test (org.junit.Test)160 KieBase (org.kie.api.KieBase)126 ArrayList (java.util.ArrayList)98 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)71 KieHelper (org.kie.internal.utils.KieHelper)69 List (java.util.List)59 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)57 StockTick (org.drools.compiler.StockTick)40 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)39 EntryPoint (org.kie.api.runtime.rule.EntryPoint)28 Arrays.asList (java.util.Arrays.asList)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)26 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)22 Date (java.util.Date)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 StockTickInterface (org.drools.compiler.StockTickInterface)18 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)16 SessionConfiguration (org.drools.core.SessionConfiguration)15