Search in sources :

Example 26 with StockTick

use of org.drools.compiler.StockTick in project drools by kiegroup.

the class DeserializationWithCompositeTriggerTest method testSerializationAndDeserialization.

/**
 * Verifies that serialization of a rule with composite trigger does not fail on
 * org.drools.core.time.impl.CompositeMaxDurationTrigger class serialization.
 */
@Test
public void testSerializationAndDeserialization() throws Exception {
    this.ksession.insert(new StockTick(2, "AAA", 1.0, 0));
    this.ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
    Assertions.assertThat(this.ksession).isNotNull();
}
Also used : StockTick(org.drools.compiler.StockTick) Test(org.junit.Test)

Example 27 with StockTick

use of org.drools.compiler.StockTick in project drools by kiegroup.

the class LifecycleTest method testExpires.

@Test
public void testExpires() throws Exception {
    EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
    StockTick event = new StockTick();
    FactHandle handle = entryPoint.insert(event);
    assertTrue(entryPoint.getFactHandles().contains(handle));
    kieSession.fireAllRules();
    assertTrue(entryPoint.getFactHandles().contains(handle));
    advanceTime(5, TimeUnit.SECONDS);
    kieSession.fireAllRules();
    assertFalse(entryPoint.getFactHandles().contains(handle));
}
Also used : StockTick(org.drools.compiler.StockTick) FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 28 with StockTick

use of org.drools.compiler.StockTick in project drools by kiegroup.

the class FirstOrderLogicTest method testForallWithSlidingWindow.

@Test
public void testForallWithSlidingWindow() throws Exception {
    final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieBase kbase = loadKnowledgeBase("test_ForallSlidingWindow.drl");
    KieSession ksession = createKnowledgeSession(kbase, conf);
    final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);
    // advance time... no events, so forall should fire
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    int seq = 1;
    // advance time... there are matching events now, but forall still not fire
    ksession.insert(new StockTick(seq++, "RHT", 10, // 60
    clock.getCurrentTime()));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    ksession.insert(new StockTick(seq++, "RHT", 10, // 65
    clock.getCurrentTime()));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    // advance time... there are non-matching events now, so forall de-activates
    ksession.insert(new StockTick(seq++, "IBM", 10, // 70
    clock.getCurrentTime()));
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    // advance time... there are non-matching events now, so forall is still deactivated
    ksession.insert(new StockTick(seq++, "RHT", 10, // 80
    clock.getCurrentTime()));
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    // advance time... non-matching event expires now, so forall should fire
    ksession.insert(new StockTick(seq++, "RHT", 10, // 90
    clock.getCurrentTime()));
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());
    // advance time... forall still matches and should not fire
    ksession.insert(new StockTick(seq++, "RHT", 10, // 100
    clock.getCurrentTime()));
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());
    // advance time... forall still matches and should not fire
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, results.size());
}
Also used : StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.compiler.SpecialString) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 29 with StockTick

use of org.drools.compiler.StockTick in project drools by kiegroup.

the class LengthSlidingWindowTest method checkPrice.

private void checkPrice(String drl, double expectedPrice) {
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
    List<Double> list = new ArrayList<Double>();
    ksession.setGlobal("list", list);
    ksession.insert("RHT");
    ksession.insert(new StockTick(1L, "RHT", 10.0));
    ksession.insert(new StockTick(2L, "RHT", 10.0));
    ksession.insert(new StockTick(3L, "ABC", 20.0));
    ksession.insert(new StockTick(4L, "RHT", 10.0));
    ksession.insert(new StockTick(5L, "XYZ", 20.0));
    ksession.insert(new StockTick(6L, "XYZ", 20.0));
    ksession.insert(new StockTick(7L, "RHT", 10.0));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(expectedPrice, (double) list.get(0), 0.01);
}
Also used : StockTick(org.drools.compiler.StockTick) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Example 30 with StockTick

use of org.drools.compiler.StockTick in project drools by kiegroup.

the class LengthSlidingWindowTest method testCompilationFailureWithUnknownWindow.

@Test
public void testCompilationFailureWithUnknownWindow() {
    // DROOLS-841
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "declare StockTick @role( event ) end\n" + "declare window RhtStocksWindow\n" + "    StockTick() over window:length( 3 )\n" + "end\n" + "rule R\n" + "when \n" + "   accumulate( StockTick( company == \"RHT\", $price : price ) from window AbcStocksWindow; $total : sum($price) )\n" + "then \n" + "    list.add($total);\n" + "end \n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertEquals(1, results.getMessages().size());
}
Also used : StockTick(org.drools.compiler.StockTick) KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Aggregations

StockTick (org.drools.compiler.StockTick)64 Test (org.junit.Test)61 KieSession (org.kie.api.runtime.KieSession)60 KieBase (org.kie.api.KieBase)47 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)40 ArrayList (java.util.ArrayList)37 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)31 EntryPoint (org.kie.api.runtime.rule.EntryPoint)27 StockTickInterface (org.drools.compiler.StockTickInterface)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 List (java.util.List)15 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)13 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)11 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)9 EventFactHandle (org.drools.core.common.EventFactHandle)8 IOException (java.io.IOException)7 ParseException (java.text.ParseException)7