Search in sources :

Example 56 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepEspTest method populateSessionWithStocks.

private void populateSessionWithStocks(KieSession ksession, StockFactory stockFactory) {
    final SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST1", 0d));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST2", 1d));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST3", 0d));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST4", 0d));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST5", 0d));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.insert(stockFactory.createStock("ST6", 1d));
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock)

Example 57 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class PatternDSLTest method testNegatedAfter.

@Test
public void testNegatedAfter() throws Exception {
    Variable<StockTick> var_$a = declarationOf(StockTick.class, "$a");
    Variable<StockTick> var_$b = declarationOf(StockTick.class, "$b");
    Rule rule = rule("R").build(pattern(var_$a).expr("$expr$1$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).expr("$expr$2$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$3$", var_$a, not(after(5, java.util.concurrent.TimeUnit.SECONDS, 8, java.util.concurrent.TimeUnit.SECONDS))), execute(() -> {
        System.out.println("fired");
    }));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model, EventProcessingOption.STREAM);
    KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kieBase.newKieSession(conf, null);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Index(org.drools.model.Index) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) BitMask(org.drools.model.BitMask) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) Variable(org.drools.model.Variable) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Assert.assertEquals(org.junit.Assert.assertEquals) StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 58 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testSessionConfigurationFromContainer.

@Test
public void testSessionConfigurationFromContainer() {
    // DROOLS-1002
    String str = "rule R when then end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kmodel = ks.newKieModuleModel();
    kmodel.newKieBaseModel("kbase1").newKieSessionModel("ksession1").setClockType(ClockTypeOption.get("pseudo"));
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str).writeKModuleXML(kmodel.toXML());
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieSessionConfiguration conf = kcontainer.getKieSessionConfiguration("ksession1");
    assertEquals("pseudo", conf.getOption(ClockTypeOption.class).getClockType());
    KieSession ksession = ks.getStoreServices().newKieSession(kcontainer.getKieBase("kbase1"), conf, env);
    assertTrue(ksession.getSessionClock() instanceof SessionPseudoClock);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 59 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class TimerAndCalendarTest method testTimerRuleAfterIntReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterIntReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 500ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (int:1000 500) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    Assert.assertEquals(0, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1700, TimeUnit.MILLISECONDS);
    Assert.assertEquals(2, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000, TimeUnit.MILLISECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(4, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 60 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class OOPathCepTest method testTemporalOperatorIncludesWithOOPath.

@Test
public void testTemporalOperatorIncludesWithOOPath() {
    final String drl = "import org.drools.testcoverage.common.model.Message;\n" + "import org.drools.testcoverage.common.model.MessageEvent;\n" + "global java.util.List events\n" + "global java.util.List messages\n" + "\n" + "declare org.drools.testcoverage.common.model.MessageEvent\n" + "  @role( event )\n" + "  @duration( duration )\n" + "end\n" + "rule R when\n" + "  ev1: MessageEvent( /msg[ message == 'Ping' ] )\n" + "  ev2: MessageEvent( $message: /msg[ message == 'Pong' ], this includes ev1 )\n" + "then\n" + "  messages.add( $message );\n" + "end\n";
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModuleFromDrl(MODULE_GROUP_ID, kieBaseTestConfiguration, drl);
    final SessionPseudoClock clock = this.initKieSessionWithPseudoClock(kieBase);
    final Message pongMessage = this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS), clock, 1);
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS - 1500));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
    this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS - 1500));
    this.kieSession.fireAllRules();
    Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
Also used : Message(org.drools.testcoverage.common.model.Message) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Test(org.junit.Test)

Aggregations

SessionPseudoClock (org.kie.api.time.SessionPseudoClock)74 Test (org.junit.Test)66 KieSession (org.kie.api.runtime.KieSession)52 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)39 KieBase (org.kie.api.KieBase)37 ArrayList (java.util.ArrayList)30 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)15 EntryPoint (org.kie.api.runtime.rule.EntryPoint)15 StockTick (org.drools.compiler.StockTick)14 Message (org.drools.testcoverage.common.model.Message)14 List (java.util.List)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 StockTick (org.drools.modelcompiler.domain.StockTick)10 OrderEvent (org.drools.compiler.OrderEvent)6 QueryResults (org.kie.api.runtime.rule.QueryResults)4 KieHelper (org.kie.internal.utils.KieHelper)4 StockTickInterface (org.drools.compiler.StockTickInterface)3