use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testDelayingNotWithPreEpochClock.
@Test(timeout = 10000)
public void testDelayingNotWithPreEpochClock() throws Exception {
String str = "package org.drools.compiler\n" + "declare A @role(event) symbol : String end\n" + "declare B @role(event) symbol : String end\n" + "rule Setup when\n" + "then\n" + " insert( new A() );\n" + "end\n" + "rule X\n" + "when\n" + " $a : A() and not( B( this after $a ) )\n" + "then\n" + "end\n";
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(conf, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
// Getting a pre-epoch date (i.e., before 1970)
Calendar ts = Calendar.getInstance();
ts.set(1900, 1, 1);
// Initializing the clock to that date
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(ts.getTimeInMillis(), TimeUnit.MILLISECONDS);
// rule X should not be delayed as the delay would be infinite
int rules = ksession.fireAllRules();
assertEquals(2, rules);
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testSimpleTimeWindow.
@Test(timeout = 10000)
public void testSimpleTimeWindow() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_SimpleTimeWindow.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
List results = new ArrayList();
wm.setGlobal("results", results);
// how to initialize the clock?
// how to configure the clock?
SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
clock.advanceTime(5, // 5 seconds
TimeUnit.SECONDS);
EventFactHandle handle1 = (EventFactHandle) wm.insert(new OrderEvent("1", "customer A", 70));
assertEquals(5000, handle1.getStartTimestamp());
assertEquals(0, handle1.getDuration());
// wm = SerializationHelper.getSerialisedStatefulSession( wm );
// results = (List) wm.getGlobal( "results" );
// clock = (SessionPseudoClock) wm.getSessionClock();
wm.fireAllRules();
assertEquals(1, results.size());
assertEquals(70, ((Number) results.get(0)).intValue());
// advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
EventFactHandle handle2 = (EventFactHandle) wm.insert(new OrderEvent("2", "customer A", 60));
assertEquals(15000, handle2.getStartTimestamp());
assertEquals(0, handle2.getDuration());
wm.fireAllRules();
assertEquals(2, results.size());
assertEquals(65, ((Number) results.get(1)).intValue());
// advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
EventFactHandle handle3 = (EventFactHandle) wm.insert(new OrderEvent("3", "customer A", 50));
assertEquals(25000, handle3.getStartTimestamp());
assertEquals(0, handle3.getDuration());
wm.fireAllRules();
assertEquals(3, results.size());
assertEquals(60, ((Number) results.get(2)).intValue());
// advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
EventFactHandle handle4 = (EventFactHandle) wm.insert(new OrderEvent("4", "customer A", 25));
assertEquals(35000, handle4.getStartTimestamp());
assertEquals(0, handle4.getDuration());
wm.fireAllRules();
// first event should have expired, making average under the rule threshold, so no additional rule fire
assertEquals(3, results.size());
// advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
EventFactHandle handle5 = (EventFactHandle) wm.insert(new OrderEvent("5", "customer A", 70));
assertEquals(45000, handle5.getStartTimestamp());
assertEquals(0, handle5.getDuration());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
// still under the threshold, so no fire
assertEquals(3, results.size());
// advance clock and assert new data
clock.advanceTime(10, // 10 seconds
TimeUnit.SECONDS);
EventFactHandle handle6 = (EventFactHandle) wm.insert(new OrderEvent("6", "customer A", 115));
assertEquals(55000, handle6.getStartTimestamp());
assertEquals(0, handle6.getDuration());
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(70, ((Number) results.get(3)).intValue());
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testTimeAndLengthWindowConflict.
@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() throws Exception {
// JBRULES-3671
String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\n" + "end\n";
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconf, drl);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
List<Number> timeResults = new ArrayList<Number>();
List<Number> lengthResults = new ArrayList<Number>();
ksession.setGlobal("timeResults", timeResults);
ksession.setGlobal("lengthResults", lengthResults);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
// 5 seconds
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("1", "customer A", 70));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(70.0));
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("2", "customer A", 60));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(65.0));
// Third interaction: advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("3", "customer A", 50));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(60.0));
// Fourth interaction: advance clock and assert new data
// 60 seconds
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("4", "customer A", 25));
ksession.fireAllRules();
System.out.println(lengthResults);
// assertTrue( lengthResults.contains( 45 ) );
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testPropertyReactiveWithDurationOnRule.
@Test
public void testPropertyReactiveWithDurationOnRule() {
// DROOLS-2238
String drl = "package org.drools.test " + " " + "declare Bean " + " @PropertyReactive " + " label : String " + " active : boolean " + "end " + " " + " " + "rule Init " + "when " + "then " + " insert( new Bean( \"aaa\", true ) ); " + "end " + " " + "rule Close " + " duration (100) " + "when " + " $b : Bean( label == \"aaa\" ) " + "then " + " modify( $b ) { " + " setActive( false ); " + " } " + "end" + " ";
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kieBase.newKieSession(sessionConfig, null);
assertEquals(1, ksession.fireAllRules());
((SessionPseudoClock) ksession.getSessionClock()).advanceTime(200, TimeUnit.MILLISECONDS);
assertEquals(1, ksession.fireAllRules(10));
}
use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.
the class CepEspTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertion.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession session = createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
}
Aggregations