use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class OOPathCepTest method testTemporalOperatorOverlappedByWithOOPath.
@Test
public void testTemporalOperatorOverlappedByWithOOPath() {
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 overlappedby[ 1s ] 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);
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS), clock, 1);
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS), clock, 1);
this.kieSession.fireAllRules();
Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
final Message pongMessage = this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), DEFAULT_DURATION_IN_SECS));
this.kieSession.fireAllRules();
Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class OOPathCepTest method testEventExplicitExpirationWithOOPath.
@Test
public void testEventExplicitExpirationWithOOPath() {
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" + " @expires( 2s )\n" + "end\n" + "rule R when\n" + " MessageEvent( /msg[ message == 'Ping' ] )\n" + " MessageEvent( $message: /msg[ message == 'Pong' ] )\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);
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Ping")), clock, 3);
final Message pongMessage = this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong")), clock, 1);
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")));
this.kieSession.fireAllRules();
Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class OOPathCepTest method testTemporalOperatorOverlapsWithOOPath.
@Test
public void testTemporalOperatorOverlapsWithOOPath() {
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 overlaps[ 1s ] 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);
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), 1), clock, 2);
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), 1), clock, 1);
this.kieSession.fireAllRules();
Assertions.assertThat(this.messages).as("The first sequence of events should NOT make the rule fire").isEmpty();
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("Ping"), DEFAULT_DURATION_IN_SECS));
this.kieSession.fireAllRules();
Assertions.assertThat(this.messages).as("The last event should make the rule fire").containsExactlyInAnyOrder(pongMessage);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class FusionAfterBeforeTest method testAfterBeforeOperators.
@Test
public void testAfterBeforeOperators() {
final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("fusionAfterBeforeTest.drl", getClass());
final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
final KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
final KieSession ksession = kieBase.newKieSession(ksconf, null);
final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
ksession.addEventListener(listener);
final EntryPoint stream = ksession.getEntryPoint("EventStream");
SessionPseudoClock clock = ksession.getSessionClock();
try {
for (int i = 0; i < 3; i++) {
final MessageEvent tc = new MessageEvent(MessageEvent.Type.received, new Message());
stream.insert(tc);
ksession.fireAllRules();
clock.advanceTime(8, TimeUnit.SECONDS);
}
ksession.fireAllRules();
} finally {
ksession.dispose();
}
Assertions.assertThat(listener.isRuleFired("AfterMessageEvent")).as("Rule 'AfterMessageEvent' was no fired!").isTrue();
Assertions.assertThat(listener.isRuleFired("BeforeMessageEvent")).as("Rule 'BeforeMessageEvent' was no fired!").isTrue();
// each rules should be fired 2 times
int firedCount = 2;
int actuallyFired = listener.ruleFiredCount("AfterMessageEvent");
Assertions.assertThat(firedCount).as("Rule 'AfterMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
firedCount = listener.ruleFiredCount("BeforeMessageEvent");
Assertions.assertThat(firedCount).as("Rule 'BeforeMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class LifecycleTest method advanceTime.
private void advanceTime(long amount, TimeUnit unit) throws InterruptedException {
if (kieSession.getSessionClock() instanceof SessionPseudoClock) {
SessionPseudoClock clock = kieSession.getSessionClock();
clock.advanceTime(amount, unit);
} else {
Thread.sleep(TimeUnit.MILLISECONDS.convert(amount, unit));
}
}
Aggregations