Search in sources :

Example 41 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler 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 42 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler 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)

Example 43 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class EventDeserializationInPastTest method testSerializationWithEventInPastBZ1205666.

@Test
public void testSerializationWithEventInPastBZ1205666() {
    // DROOLS-749
    final String drl = "import " + Event1.class.getCanonicalName() + "\n" + "declare Event1\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 3h )\n" + "end\n" + "\n" + "rule R\n" + " when\n" + " $evt: Event1()\n" + " not Event1(this != $evt, this after[0, 1h] $evt)\n" + " then\n" + " System.out.println($evt.getCode());\n" + "end\n";
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert(new Event1("id1", 0));
    final PseudoClockScheduler clock = ksession.getSessionClock();
    clock.advanceTime(2, TimeUnit.HOURS);
    ksession.fireAllRules();
    ksession = marshallAndUnmarshall(KieServices.Factory.get(), kbase, ksession, sessionConfig);
    ksession.insert(new Event1("id2", 0));
    ksession.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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 44 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class ExpirationTest method testBetaRightExpired.

@Test
public void testBetaRightExpired() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + "  $a: A( $Aid: id > 0 )\n" + "  $b: B( id == $Aid )\n" + "then\n" + "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + "  counter.incrementAndGet();\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 sessionClock = ksession.getSessionClock();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    ksession.insert(new A(1));
    sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
    ksession.insert(new B(1));
    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) 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) Test(org.junit.Test)

Example 45 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class ExpirationTest method testBetaLeftExpired.

@Test
public void testBetaLeftExpired() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + "  $a: A( $Aid: id > 0 )\n" + "  $b: B( id == $Aid )\n" + "then\n" + "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + "  counter.incrementAndGet();\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 sessionClock = ksession.getSessionClock();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    ksession.insert(new B(1));
    sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
    ksession.insert(new A(1));
    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) 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) Test(org.junit.Test)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)81 KieSession (org.kie.api.runtime.KieSession)76 Test (org.junit.Test)75 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)72 KieBase (org.kie.api.KieBase)60 ArrayList (java.util.ArrayList)39 KieHelper (org.kie.internal.utils.KieHelper)39 List (java.util.List)29 Date (java.util.Date)19 Arrays.asList (java.util.Arrays.asList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14 StockTick (org.drools.compiler.StockTick)13 DateFormat (java.text.DateFormat)9 SimpleDateFormat (java.text.SimpleDateFormat)9 Calendar (org.kie.api.time.Calendar)8 StockTickInterface (org.drools.compiler.StockTickInterface)7 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 IOException (java.io.IOException)5 FactA (org.drools.compiler.FactA)5