use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testTimedRuleWithAccumulate.
@Test
@Ignore("Cannot reproduce with pseudoclock and takes too long with system clock")
public void testTimedRuleWithAccumulate() {
// BZ-1083103
String drl = "import " + SynthEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare SynthEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + "end\n" + "\n" + "declare EventCounter\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " id : long\n" + " key : String\n" + " timestamp : Date\n" + "end\n" + "\n" + "rule \"Create counter\"\n" + "when\n" + "$e : SynthEvent() from entry-point \"synth\"\n" + "then\n" + " entryPoints[\"counters\"].insert(new EventCounter( $e.getId(), \"event\", $e.getTimestamp() ) );\n" + "end\n" + "\n" + "rule \"Count epm\"\n" + "timer ( cron: 0/10 * * * * ? )\n" + // "timer ( int: 2s 1s )\n" +
"when\n" + " Number( $count : intValue ) from accumulate(\n" + " EventCounter( key == \"event\" ) over window:time( 60s ) from entry-point \"counters\", count(1) )\n" + "then\n" + " System.out.println(\"[\" + new Date() + \"] epm = \" + $count );\n" + "end\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());
final KieSession ksession = kbase.newKieSession();
EntryPoint synthEP = ksession.getEntryPoint("synth");
new Thread() {
public void run() {
System.out.println("[" + new Date() + "] start!");
ksession.fireUntilHalt();
}
}.start();
long counter = 0;
while (true) {
counter++;
synthEP.insert(new SynthEvent(counter));
try {
Thread.sleep(20L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if ((counter % 1000) == 0) {
System.out.println("Total events: " + counter);
}
}
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testEventExpiration4.
@Test(timeout = 10000)
public void testEventExpiration4() throws Exception {
// read in the source
final KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration4.drl");
final KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sconf.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = createKnowledgeSession(kbase, sconf);
EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
final List results = new ArrayList();
ksession.setGlobal("results", results);
EventFactHandle handle1 = (EventFactHandle) eventStream.insert(new StockTick(1, "ACME", 50, System.currentTimeMillis(), 3));
ksession.fireAllRules();
clock.advanceTime(11, TimeUnit.SECONDS);
/**
* clock.advance() will put the event expiration in the queue to be executed,
* but it has to wait for a "thread" to do that
* so we fire rules again here to get that
* alternative could run fireUntilHalt() *
*/
ksession.fireAllRules();
assertTrue(results.size() == 1);
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
}
use of org.kie.api.runtime.rule.EntryPoint 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.runtime.rule.EntryPoint in project drools by kiegroup.
the class DataDictionaryTest method testEntryPoints.
@Test
public void testEntryPoints() throws Exception {
setKSession(getModelSession(source, VERBOSE));
setKbase(getKSession().getKieBase());
FactType gender = getKbase().getFactType(packageName, "Gender");
FactType noclaims = getKbase().getFactType(packageName, "NoOfClaims");
FactType scrambled = getKbase().getFactType(packageName, "Scrambled");
FactType domicile = getKbase().getFactType(packageName, "Domicile");
FactType agecar = getKbase().getFactType(packageName, "AgeOfCar");
FactType amklaims = getKbase().getFactType(packageName, "AmountOfClaims");
assertEquals(7, getKSession().getEntryPoints().size());
EntryPoint gender_ep = getKSession().getEntryPoint("in_Gender");
Assert.assertNotNull(gender_ep);
EntryPoint noclaims_ep = getKSession().getEntryPoint("in_NoOfClaims");
Assert.assertNotNull(noclaims_ep);
EntryPoint scrambled_ep = getKSession().getEntryPoint("in_Scrambled");
Assert.assertNotNull(scrambled_ep);
EntryPoint domicile_ep = getKSession().getEntryPoint("in_Domicile");
Assert.assertNotNull(domicile_ep);
EntryPoint agecar_ep = getKSession().getEntryPoint("in_AgeOfCar");
Assert.assertNotNull(agecar_ep);
EntryPoint amklaims_ep = getKSession().getEntryPoint("in_AmountOfClaims");
Assert.assertNotNull(amklaims_ep);
gender_ep.insert("M");
noclaims_ep.insert("> 4");
scrambled_ep.insert(4);
domicile_ep.insert("way out");
agecar_ep.insert(new Double("3.4"));
amklaims_ep.insert(9);
getKSession().fireAllRules();
assertEquals(6, getKSession().getObjects().size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(gender.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(noclaims.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(scrambled.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(domicile.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(agecar.getFactClass())).size());
assertEquals(1, getKSession().getObjects(new ClassObjectFilter(amklaims.getFactClass())).size());
checkFirstDataFieldOfTypeStatus(amklaims, true, false, null, 9);
checkFirstDataFieldOfTypeStatus(domicile, false, false, null, "way out");
checkGeneratedRules();
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class LifecycleTest method testExpires.
@Test
public void testExpires() throws Exception {
EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
StockTick event = new StockTick();
FactHandle handle = entryPoint.insert(event);
assertTrue(entryPoint.getFactHandles().contains(handle));
kieSession.fireAllRules();
assertTrue(entryPoint.getFactHandles().contains(handle));
advanceTime(5, TimeUnit.SECONDS);
kieSession.fireAllRules();
assertFalse(entryPoint.getFactHandles().contains(handle));
}
Aggregations