use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class TimerAndCalendarTest method testRaceConditionWithTimedRuleExectionOption.
@Test(timeout = 10000)
@Ignore("the listener callback holds some locks so blocking in it is not safe")
public void testRaceConditionWithTimedRuleExectionOption() throws Exception {
// BZ-1073880
String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx @Propagation(EAGER)\n" + " timer (int:30s 10s) " + "when \n" + " $s: String()\n" + "then \n" + " list.add($s); \n" + "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
conf.setOption(TimedRuleExecutionOption.YES);
KieBase kbase = loadKnowledgeBaseFromString(str);
KieSession ksession = createKnowledgeSession(kbase, conf);
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicBoolean aBool = new AtomicBoolean(true);
AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {
public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
try {
if (aBool.get()) {
barrier.await();
aBool.set(false);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
};
ksession.addEventListener(agendaEventListener);
List list = new ArrayList();
ksession.setGlobal("list", list);
// Using the Pseudo Clock.
SessionClock clock = ksession.getSessionClock();
SessionPseudoClock pseudoClock = (SessionPseudoClock) clock;
// Insert the event.
String eventOne = "one";
ksession.insert(eventOne);
// Advance the time .... so the timer will fire.
pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
// Rule doesn't fire in PHREAK. This is because you need to call 'fireAllRules' after you've inserted the fact, otherwise the timer
// job is not created.
ksession.fireAllRules();
// Rule still doesn't fire, because the DefaultTimerJob is created now, and now we need to advance the timer again.
pseudoClock.advanceTime(30000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
aBool.set(true);
pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
aBool.set(true);
String eventTwo = "two";
ksession.insert(eventTwo);
ksession.fireAllRules();
// 60
pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
aBool.set(true);
// 70
pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
aBool.set(true);
// From here, the second rule should fire.
// phaser.register();
pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
aBool.set(true);
// Now 2 rules have fired, and those will now fire every 10 seconds.
pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
barrier.await();
barrier.reset();
pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
aBool.set(true);
barrier.await();
barrier.reset();
pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
aBool.set(true);
barrier.await();
barrier.reset();
ksession.destroy();
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class AdvanceSessionTimeCommand method execute.
@Override
public Long execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
SessionPseudoClock sessionClock = ksession.<SessionPseudoClock>getSessionClock();
sessionClock.advanceTime(amount, unit);
long result = sessionClock.getCurrentTime();
ExecutionResultImpl results = ((RegistryContext) context).lookup(ExecutionResultImpl.class);
if (results != null) {
results.getResults().put(this.outIdentifier, result);
}
return result;
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testEventTimestamp.
@Test
public void testEventTimestamp() {
// DROOLS-268
String drl = "\n" + "import org.drools.compiler.integrationtests.CepEspTest.Event; \n" + "global java.util.List list; \n" + "global org.kie.api.time.SessionPseudoClock clock; \n" + "" + "declare Event \n" + " @role( event )\n" + " @timestamp( time ) \n" + " @expires( 10000000 ) \n" + "end \n" + "" + "" + "rule \"inform about E1\"\n" + "when\n" + " $event1 : Event( type == 1 )\n" + " //there is an event (T2) with value 0 between 0,2m after doorClosed\n" + " $event2: Event( type == 2, value == 1, this after [0, 1200ms] $event1, $timestamp : time )\n" + " //there is no newer event (T2) within the timeframe\n" + " not Event( type == 2, this after [0, 1200ms] $event1, time > $timestamp ) \n" + "then\n" + " list.add( clock.getCurrentTime() ); \n " + "end\n" + "\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
ksession.setGlobal("clock", clock);
// 0
ksession.insert(new Event(1, -1, clock.getCurrentTime()));
clock.advanceTime(600, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// 600
ksession.insert(new Event(2, 0, clock.getCurrentTime()));
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// 700
ksession.insert(new Event(2, 0, clock.getCurrentTime()));
clock.advanceTime(300, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// 1000
ksession.insert(new Event(2, 0, clock.getCurrentTime()));
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// 1100
ksession.insert(new Event(2, 1, clock.getCurrentTime()));
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
// 1300
ksession.insert(new Event(2, 0, clock.getCurrentTime()));
clock.advanceTime(1000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertFalse(list.isEmpty());
assertEquals(1, list.size());
Long time = (Long) list.get(0);
assertTrue(time > 1000 && time < 1500);
ksession.dispose();
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testTemporalOperatorsInfinity.
@Test(timeout = 10000)
public void testTemporalOperatorsInfinity() throws Exception {
// read in the source
final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators3.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(sconf, null);
List list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
EntryPoint ep = ksession.getEntryPoint("X");
clock.advanceTime(1000, TimeUnit.SECONDS);
int rules = 0;
ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
// int rules = ksession.fireAllRules();
System.out.println(list);
ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
// rules = ksession.fireAllRules();
System.out.println(list);
ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
clock.advanceTime(8, TimeUnit.SECONDS);
rules = ksession.fireAllRules();
System.out.println(list);
assertEquals(3, rules);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testSalienceWithEventsPseudoClock.
@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() throws IOException, ClassNotFoundException {
String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R2 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R3 salience 100\n" + " when\n" + " $s2 : StockTick( company == 'ACME' )\n" + " then\n" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(ksconf, null);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
ksession.insert(new StockTick(1, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(2, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(3, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(4, "ACME", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
int rulesFired = ksession.fireAllRules();
assertEquals(4, rulesFired);
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(captor.capture());
List<AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
Aggregations