use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testExpireEventOnEndTimestamp.
@Test(timeout = 10000)
public void testExpireEventOnEndTimestamp() throws Exception {
// DROOLS-40
String str = "package org.drools.compiler;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @duration( duration )\n" + "end\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockTick( company == \"DROO\" )\n" + "$b : StockTick( company == \"ACME\", this after[60,80] $a )\n" + "then\n" + " resultsAfter.add( $b );\n" + "end";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(conf, null);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.getSessionClock();
List<StockTick> resultsAfter = new ArrayList<StockTick>();
ksession.setGlobal("resultsAfter", resultsAfter);
// inserting new StockTick with duration 30 at time 0 => rule
// after[60,80] should fire when ACME lasts at 100-120
ksession.insert(new StockTick(1, "DROO", 0, 0, 30));
clock.advanceTime(100, TimeUnit.MILLISECONDS);
ksession.insert(new StockTick(2, "ACME", 0, 0, 20));
ksession.fireAllRules();
assertEquals(1, resultsAfter.size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testBeforeOnArbitraryDates.
@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.drl");
KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession wm = createKnowledgeSession(kbase, sconf);
final List<?> results = new ArrayList<Object>();
wm.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
104000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
100000, 3);
InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(tick1, results.get(0));
assertEquals(tick2, results.get(1));
assertEquals(tick1, results.get(2));
assertEquals(tick2, results.get(3));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testTemporalOperators.
@Test(timeout = 10000)
public void testTemporalOperators() throws Exception {
// read in the source
final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators.drl");
KieSession ksession = createKnowledgeSession(kbase);
ksession.insert(new StockTick(1, "A", 10, 1000));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleAdditionsWithEntryPoints.
@Test(timeout = 10000)
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl");
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
KieSession ksession = createKnowledgeSession(kbase);
// now lets add some knowledge to the kbase
kbase.addPackages(kpkgs);
List<StockTick> results = new ArrayList<StockTick>();
ksession.setGlobal("results", results);
EntryPoint ep = ksession.getEntryPoint("in-channel");
ep.insert(new StockTick(1, "RHT", 20, 10000));
ep.insert(new StockTick(2, "RHT", 21, 15000));
ep.insert(new StockTick(3, "RHT", 22, 20000));
ksession.fireAllRules();
assertEquals(3, results.size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testTimeWindowWithPastEvents.
@Test(timeout = 10000)
public void testTimeWindowWithPastEvents() throws Exception {
// JBRULES-2258
String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time ) \n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " accumulate(\n" + " $o : StockTick() over window:time(10ms)," + " $tot : count( $o );" + " $tot > 0 )\n" + "then\n" + " System.out.println( $tot ); \n" + " timeResults.add( $tot );\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>();
ksession.setGlobal("timeResults", timeResults);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
int count = 0;
StockTick tick1 = new StockTick(count++, "X", 0.0, 1);
StockTick tick2 = new StockTick(count++, "X", 0.0, 3);
StockTick tick3 = new StockTick(count++, "X", 0.0, 7);
StockTick tick4 = new StockTick(count++, "X", 0.0, 9);
StockTick tick5 = new StockTick(count++, "X", 0.0, 15);
clock.advanceTime(30, TimeUnit.MILLISECONDS);
ksession.insert(tick1);
ksession.insert(tick2);
ksession.insert(tick3);
ksession.insert(tick4);
ksession.insert(tick5);
ksession.fireAllRules();
System.out.println(timeResults);
assertTrue(timeResults.isEmpty());
clock.advanceTime(0, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
clock.advanceTime(3, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(timeResults.isEmpty());
}
Aggregations