use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class OOPathCepTest method testTemporalOperatorDuringWithOOPath.
@Test
public void testTemporalOperatorDuringWithOOPath() {
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 during 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.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping"), DEFAULT_DURATION_IN_SECS));
this.insertEventAndAdvanceClock(new MessageEvent(MessageEvent.Type.sent, new Message("Pong"), 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 - 1500));
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 testTemporalOperatorCoincidesWithOOPath.
@Test
public void testTemporalOperatorCoincidesWithOOPath() {
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" + "end\n" + "rule R when\n" + " ev1: MessageEvent( /msg[ message == 'Ping' ] )\n" + " ev2: MessageEvent( $message: /msg[ message == 'Pong' ], this coincides[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")), clock, 2);
this.insertEvent(new MessageEvent(MessageEvent.Type.sent, new Message("Ping")));
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")));
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 insertEventsAndFire.
private int insertEventsAndFire(final KieSession kieSession, final SortedSet<Event> events) {
final SessionPseudoClock sessionClock = kieSession.getSessionClock();
final long startTime = sessionClock.getCurrentTime();
int fireCount = 0;
for (Event event : events) {
final long eventTime = startTime + event.getTimeValue();
sessionClock.advanceTime(eventTime - sessionClock.getCurrentTime(), TimeUnit.MILLISECONDS);
kieSession.insert(event);
fireCount += kieSession.fireAllRules();
}
return fireCount;
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class NegativePatternsTest method advanceTime.
private void advanceTime(final long amount) {
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(amount, TimeUnit.MILLISECONDS);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class TimerAndCalendarTest method testExpiredPropagations.
@Test
public void testExpiredPropagations() throws InterruptedException {
// DROOLS-244
String drl = "package org.drools.test;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "\t@role( event )\n" + "\t@timestamp( time )\n" + "end\n" + "\n" + "declare window ATicks\n" + " StockTick( company == \"AAA\" ) over window:time( 1s ) " + " from entry-point \"AAA\"\n" + "end\n" + "\n" + "declare window BTicks\n" + " StockTick( company == \"BBB\" ) over window:time( 1s ) " + " from entry-point \"BBB\"\n" + "end\n" + "\n" + "rule Ticks \n" + " when\n" + " String()\n" + " accumulate( $x : StockTick() from window ATicks, $a : count( $x ) )\n" + " accumulate( $y : StockTick() from window BTicks, $b : count( $y ) )\n" + " accumulate( $z : StockTick() over window:time( 1s ), $c : count( $z ) )\n" + " then\n" + " list.add( $a );\n" + " list.add( $b );\n" + " list.add( $c );\n" + "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(conf, null);
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.getSessionClock();
clock.advanceTime(1100, TimeUnit.MILLISECONDS);
StockTick tick = new StockTick(0, "AAA", 1.0, 0);
StockTick tock = new StockTick(1, "BBB", 1.0, 2500);
StockTick tack = new StockTick(1, "CCC", 1.0, 2700);
EntryPoint epa = ksession.getEntryPoint("AAA");
EntryPoint epb = ksession.getEntryPoint("BBB");
epa.insert(tick);
epb.insert(tock);
ksession.insert(tack);
FactHandle handle = ksession.insert("go1");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 1L, 1L), list);
list.clear();
ksession.retract(handle);
clock.advanceTime(2550, TimeUnit.MILLISECONDS);
handle = ksession.insert("go2");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 0L, 1L), list);
list.clear();
ksession.retract(handle);
clock.advanceTime(500, TimeUnit.MILLISECONDS);
handle = ksession.insert("go3");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 0L, 0L), list);
list.clear();
ksession.retract(handle);
ksession.dispose();
}
Aggregations