Search in sources :

Example 51 with KieSessionConfiguration

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

the class CepEspTest method testTimeAndLengthWindowConflict.

@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() throws Exception {
    // JBRULES-3671
    String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\n" + "end\n";
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconf, drl);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List<Number> timeResults = new ArrayList<Number>();
    List<Number> lengthResults = new ArrayList<Number>();
    ksession.setGlobal("timeResults", timeResults);
    ksession.setGlobal("lengthResults", lengthResults);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    // 5 seconds
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("1", "customer A", 70));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(70.0));
    // 10 seconds
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("2", "customer A", 60));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(65.0));
    // Third interaction: advance clock and assert new data
    // 10 seconds
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("3", "customer A", 50));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(60.0));
    // Fourth interaction: advance clock and assert new data
    // 60 seconds
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("4", "customer A", 25));
    ksession.fireAllRules();
    System.out.println(lengthResults);
// assertTrue( lengthResults.contains( 45 ) );
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 52 with KieSessionConfiguration

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

the class CepEspTest method testSerializationDeserliaizationWithRectractedExpireFact.

@Test(timeout = 10000)
public void testSerializationDeserliaizationWithRectractedExpireFact() {
    // DROOLS-1328
    String drl = "package " + TestEvent.class.getPackage().getName() + "\n" + "declare " + TestEvent.class.getCanonicalName() + "\n" + "   @role( event ) \n" + "   @expires( 60d ) \n" + "end\n" + "rule \"retract test rule\"\n" + "salience 10 \n" + "when\n" + "   $e : TestEvent() over window:length(1)\n" + "then\n" + "   delete($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);
    ksession.insert(new TestEvent("test1"));
    ksession.fireAllRules();
    KieSession kieSessionDeserialized = null;
    try {
        kieSessionDeserialized = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ksession.dispose();
    kieSessionDeserialized.insert(new TestEvent("test2"));
    kieSessionDeserialized.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 53 with KieSessionConfiguration

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

the class CepEspTest method testDeserializationWithCompositeTrigger.

@Test
public void testDeserializationWithCompositeTrigger() 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" + "  @expires( 1s )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  $event : StockTick( )\n" + "  not StockTick( company == \"BBB\", this after[0,96h] $event )\n" + "  not StockTick( company == \"CCC\", this after[0,96h] $event )\n" + "then\n" + "end";
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KieSessionConfiguration knowledgeSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    knowledgeSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
    KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
    KieSession ks = kb.newKieSession(knowledgeSessionConfiguration, null);
    ks.insert(new StockTick(2, "AAA", 1.0, 0));
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 54 with KieSessionConfiguration

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

the class CepEspTest method testPropertyReactiveWithDurationOnRule.

@Test
public void testPropertyReactiveWithDurationOnRule() {
    // DROOLS-2238
    String drl = "package org.drools.test " + " " + "declare Bean " + "   @PropertyReactive " + "   label : String " + "   active : boolean " + "end " + " " + " " + "rule Init " + "when " + "then " + "   insert( new Bean( \"aaa\", true ) ); " + "end " + " " + "rule Close " + "  duration (100) " + "when " + "    $b : Bean( label == \"aaa\" )   " + "then " + "    modify( $b ) {  " + "       setActive( false ); " + "    }  " + "end" + " ";
    KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kieBase.newKieSession(sessionConfig, null);
    assertEquals(1, ksession.fireAllRules());
    ((SessionPseudoClock) ksession.getSessionClock()).advanceTime(200, TimeUnit.MILLISECONDS);
    assertEquals(1, ksession.fireAllRules(10));
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 55 with KieSessionConfiguration

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

the class CepEspTest method testCEPNamedConsTimers.

@Test
public void testCEPNamedConsTimers() throws InterruptedException {
    String drl = "package org.drools " + "global java.util.List list; " + "declare  Msg " + "    @role( event ) " + "    sender : String  @key " + "end " + "rule Init " + "when " + "  $s : String() " + "then " + "  System.out.println( 'Msg ' + $s ); " + "  insert( new Msg( $s ) ); " + "end " + "rule 'Viol' " + "when " + "    $trigger : Msg( 'John' ; ) " + "    not Msg( 'Peter' ; this after[0, 100ms] $trigger ) do[viol]" + "then " + "  list.add( 0 ); " + "then[viol] " + "  list.add( -2 ); " + "end " + "";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("John");
    ksession.fireAllRules();
    assertTrue(list.isEmpty());
    ((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertTrue(list.contains(-2));
    assertTrue(list.contains(0));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) 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