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));
}
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());
}
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);
}
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());
}
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);
}
Aggregations