use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testStreamModeNoSerialization.
@Test(timeout = 10000)
public void testStreamModeNoSerialization() throws IOException, ClassNotFoundException {
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_StreamMode.drl");
KieBase kbase2 = (KieBase) DroolsStreamUtils.streamIn(DroolsStreamUtils.streamOut(kbase1), null);
final KieSession ksession1 = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession1.addEventListener(ael1);
final KieSession ksession2 = kbase2.newKieSession();
AgendaEventListener ael2 = mock(AgendaEventListener.class);
ksession2.addEventListener(ael2);
// -------------
// first, check the non-serialized session
// -------------
ksession1.insert(new Sensor(10, 10));
ksession1.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> aafe1 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael1, times(1)).afterMatchFired(aafe1.capture());
List<AfterMatchFiredEvent> events1 = aafe1.getAllValues();
assertThat(events1.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
ksession1.insert(new Sensor(20, 20));
ksession1.fireAllRules();
verify(ael1, times(2)).afterMatchFired(aafe1.capture());
events1 = aafe1.getAllValues();
assertThat(events1.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
ksession1.insert(new Sensor(30, 30));
ksession1.fireAllRules();
verify(ael1, times(3)).afterMatchFired(aafe1.capture());
assertThat(events1.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
ksession1.dispose();
// -------------
// now we check the serialized session
// -------------
ArgumentCaptor<AfterMatchFiredEvent> aafe2 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
ksession2.insert(new Sensor(10, 10));
ksession2.fireAllRules();
verify(ael2, times(1)).afterMatchFired(aafe2.capture());
List<AfterMatchFiredEvent> events2 = aafe2.getAllValues();
assertThat(events2.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
ksession2.insert(new Sensor(20, 20));
ksession2.fireAllRules();
verify(ael2, times(2)).afterMatchFired(aafe2.capture());
events2 = aafe2.getAllValues();
assertThat(events2.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
ksession2.insert(new Sensor(30, 30));
ksession2.fireAllRules();
verify(ael2, times(3)).afterMatchFired(aafe2.capture());
assertThat(events2.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
ksession2.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testCoincidesOnArbitraryDates.
@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
// read in the source
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.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
100000, 3);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 50 milliseconds after DROO
100050, 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.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testDuplicateFiring2.
@Test
public void testDuplicateFiring2() throws InterruptedException {
String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule Tick when $s : StockTick() then System.out.println( $s ); end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "\t$n: Number ( intValue > 0 ) from accumulate ( $e: StockTick() over window:time(3s), count($e))\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
// Check the builder for errors
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
// configure knowledge base
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.CLOUD);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
// init session clock
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
// insert events
for (int i = 1; i < 3; i++) {
StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
clock.advanceTime(1001, TimeUnit.MILLISECONDS);
ksession.insert(event);
System.out.println(i + ". rule invocation");
ksession.fireAllRules();
}
clock.advanceTime(3001, TimeUnit.MILLISECONDS);
StockTick event = new StockTick(3, "XXX", 1.0, 0);
System.out.println("3. rule invocation");
ksession.insert(event);
ksession.fireAllRules();
clock.advanceTime(3001, TimeUnit.MILLISECONDS);
StockTick event2 = new StockTick(3, "XXX", 1.0, 0);
System.out.println("4. rule invocation");
ksession.insert(event2);
ksession.fireAllRules();
ksession.dispose();
assertEquals(Arrays.asList(1L, 2L, 1L, 1L), list);
}
use of org.kie.api.KieBaseConfiguration 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.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testCancelActivation.
@Test(timeout = 10000)
public void testCancelActivation() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " kcontext.cancelMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
final List cancelled = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void beforeMatchFired(BeforeMatchFiredEvent event) {
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
public void afterMatchFired(AfterMatchFiredEvent event) {
}
public void matchCreated(MatchCreatedEvent event) {
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void matchCancelled(MatchCancelledEvent event) {
cancelled.add(event);
}
});
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(0, list.size());
assertEquals(1, cancelled.size());
assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
ksession.dispose();
}
Aggregations