use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class MTEntryPointsTest method testTwoEntryPoints.
/**
* Inserts events using multiple threads into two EntryPoints. The insert
* operation is synchronized on corresponding SessionEntryPoint instance.
*/
@Test
public void testTwoEntryPoints() throws Exception {
final EntryPoint firstThreadEntryPoint = kieSession.getEntryPoint("FirstStream");
final EntryPoint secondThreadEntryPoint = kieSession.getEntryPoint("SecondStream");
int numInsertersInEachEntryPoint = 10;
int numThreadPoolCapacity = numInsertersInEachEntryPoint * 2;
ExecutorService executorService = Executors.newFixedThreadPool(numThreadPoolCapacity);
List<Future<?>> futures = new ArrayList<Future<?>>();
for (int i = 0; i < numInsertersInEachEntryPoint; i++) {
// working only with first stream, future for exception watching
Future<?> futureForFirstThread = executorService.submit(new TestInserter(kieSession, firstThreadEntryPoint));
futures.add(futureForFirstThread);
// working only with second stream, future for exception watching
Future<?> futureForSecondThread = executorService.submit(new TestInserter(kieSession, secondThreadEntryPoint));
futures.add(futureForSecondThread);
}
try {
for (Future<?> f : futures) {
f.get(30, TimeUnit.SECONDS);
}
} catch (ExecutionException ex) {
throw ex;
}
}
use of org.kie.api.runtime.rule.EntryPoint 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.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testIdentityAssertBehaviorOnEntryPoints.
@Test(timeout = 10000)
public void testIdentityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
kbconf.setOption(EqualityBehaviorOption.IDENTITY);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
final KieSession ksession = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession.addEventListener(ael1);
EntryPoint ep1 = ksession.getEntryPoint("stocktick stream");
FactHandle fh1 = ep1.insert(st1);
FactHandle fh1_2 = ep1.insert(st1);
FactHandle fh2 = ep1.insert(st2);
FactHandle fh3 = ep1.insert(st3);
assertSame(fh1, fh1_2);
assertNotSame(fh1, fh2);
assertNotSame(fh1, fh3);
assertNotSame(fh2, fh3);
ksession.fireAllRules();
// must have fired 3 times, one for each event identity
verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
ksession.dispose();
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testCloudModeExpiration.
@Test(timeout = 10000)
public void testCloudModeExpiration() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException {
String str = "package org.drools.cloud\n" + "import org.drools.compiler.*\n" + "declare Event\n" + " @role ( event )\n" + " name : String\n" + " value : Object\n" + "end\n" + "declare AnotherEvent\n" + " @role ( event )\n" + " message : String\n" + " type : String\n" + "end\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule \"two events\"\n" + " when\n" + " Event( value != null ) from entry-point X\n" + " StockTick( company != null ) from entry-point X\n" + " then\n" + "end";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.CLOUD);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSession ksession = createKnowledgeSession(kbase);
EntryPoint ep = ksession.getEntryPoint("X");
ep.insert(new StockTick(1, "RHT", 10, 1000));
int rulesFired = ksession.fireAllRules();
assertEquals(0, rulesFired);
org.kie.api.definition.type.FactType event = kbase.getFactType("org.drools.cloud", "Event");
Object e1 = event.newInstance();
event.set(e1, "name", "someKey");
event.set(e1, "value", "someValue");
ep.insert(e1);
rulesFired = ksession.fireAllRules();
assertEquals(1, rulesFired);
// let some time be spent
Thread.currentThread().sleep(1000);
// check both events are still in memory as we are running in CLOUD mode
assertEquals(2, ep.getFactCount());
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testUpdateEventThroughEntryPoint.
@Test
public void testUpdateEventThroughEntryPoint() throws Exception {
String drl = "import org.drools.compiler.integrationtests.CepEspTest.TestEvent\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + " @expires( 4s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + " no-loop\n" + " when\n" + " $event : TestEvent ( name != null ) over window:time( 4s ) from entry-point EventStream\n" + " then\n" + " // insert( new Message( $event.getValue().toString() ) );\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
KieModuleModel kieModule = ks.newKieModuleModel();
kieModule.newKieBaseModel("KBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("KSession").setDefault(true);
kfs.writeKModuleXML(kieModule.toXML());
kfs.write("src/main/resources/lifecycle.drl", drl);
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
KieSession kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
TestEvent event = new TestEvent("testEvent1");
FactHandle handle = entryPoint.insert(event);
TestEvent event2 = new TestEvent("testEvent2");
entryPoint.update(handle, event2);
// make sure the event is in the entry-point
assertFalse(entryPoint.getObjects().contains(event));
assertTrue(entryPoint.getObjects().contains(event2));
assertEquals(entryPoint.getObject(handle), event2);
kieSession.dispose();
}
Aggregations