use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class FireUntilHaltTest method testFireUntilHaltFailingAcrossEntryPoints.
@Test
public void testFireUntilHaltFailingAcrossEntryPoints() throws Exception {
String rule1 = "package org.drools.compiler\n";
rule1 += "global java.util.List list\n";
rule1 += "rule testFireUntilHalt\n";
rule1 += "when\n";
rule1 += " Cheese()\n";
rule1 += " $p : Person() from entry-point \"testep2\"\n";
rule1 += "then \n";
rule1 += " list.add( $p ) ;\n";
rule1 += "end\n";
final KieBase kbase = loadKnowledgeBaseFromString(rule1);
final KieSession ksession = createKnowledgeSession(kbase);
final EntryPoint ep = ksession.getEntryPoint("testep2");
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Cheese("cheddar"));
ksession.fireAllRules();
final Thread t1 = new Thread(ksession::fireUntilHalt);
t1.start();
Thread.sleep(500);
ep.insert(new Person("darth"));
Thread.sleep(500);
ksession.halt();
t1.join(5000);
final boolean alive = t1.isAlive();
if (alive) {
t1.interrupt();
}
assertFalse("Thread should have died!", alive);
assertEquals(1, list.size());
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class MTEntryPointsTest method testOneEntryPoint.
/**
* Inserts events using multiple threads into one EntryPoint. The insert
* operation is synchronized on corresponding SessionEntryPoint instance.
*/
@Test
public void testOneEntryPoint() throws Exception {
final EntryPoint firstThreadEntryPoint = kieSession.getEntryPoint("FirstStream");
int numInsertersInEachEntryPoint = 10;
int numThreadPoolCapacity = numInsertersInEachEntryPoint;
ExecutorService executorService = Executors.newFixedThreadPool(numThreadPoolCapacity);
List<Future<?>> futures = new ArrayList<Future<?>>();
for (int i = 0; i < numInsertersInEachEntryPoint; i++) {
// future for exception watching
Future<?> futureForFirstThread = executorService.submit(new TestInserter(kieSession, firstThreadEntryPoint));
futures.add(futureForFirstThread);
}
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 testTemporalQuery.
@Test
public void testTemporalQuery() {
// BZ-967441
String drl = "package org.drools.compiler.integrationtests;\n" + "\n" + "import org.drools.compiler.integrationtests.CepEspTest.TestEvent;\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + "end\n" + "\n" + "query EventsBeforeNineSeconds\n" + " $event : TestEvent() from entry-point EStream\n" + " $result : TestEvent ( this after [0s, 9s] $event) from entry-point EventStream\n" + "end\n";
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.write("src/main/resources/querytest.drl", drl);
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
KieBase kbase = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
KieSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksconfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
EntryPoint ePoint = ksession.getEntryPoint("EStream");
EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
ePoint.insert(new TestEvent("zero"));
entryPoint.insert(new TestEvent("one"));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new TestEvent("two"));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new TestEvent("three"));
QueryResults results = ksession.getQueryResults("EventsBeforeNineSeconds");
assertEquals(1, results.size());
ksession.dispose();
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method AfterOperatorInCEPQueryTest.
@Test
public void AfterOperatorInCEPQueryTest() {
String drl = "package org.drools;\n" + "import org.drools.compiler.StockTick; \n" + "\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "query EventsBeforeNineSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 9s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeNineteenSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 19s] $event) from entry-point EventStream\n" + "end\n" + "\n" + "query EventsBeforeHundredSeconds\n" + " $event : StockTick() from entry-point EStream\n" + " $result : StockTick ( this after [0s, 100s] $event) from entry-point EventStream\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);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
EntryPoint ePoint = ksession.getEntryPoint("EStream");
EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
ePoint.insert(new StockTick(0L, "zero", 0.0, 0));
entryPoint.insert(new StockTick(1L, "one", 0.0, 0));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new StockTick(2L, "two", 0.0, 0));
clock.advanceTime(10, TimeUnit.SECONDS);
entryPoint.insert(new StockTick(3L, "three", 0.0, 0));
QueryResults results = ksession.getQueryResults("EventsBeforeNineSeconds");
assertEquals(0, results.size());
results = ksession.getQueryResults("EventsBeforeNineteenSeconds");
assertEquals(0, results.size());
results = ksession.getQueryResults("EventsBeforeHundredSeconds");
assertEquals(1, results.size());
ksession.dispose();
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class CepEspTest method testExpirationAtHighRates.
@Test
@Ignore
public void testExpirationAtHighRates() throws InterruptedException {
// DROOLS-130
String drl = "package droolsfusioneval\n" + "" + "global java.util.List list; \n" + "" + "import org.drools.compiler.integrationtests.CepEspTest.ProbeEvent;\n" + "import org.drools.compiler.integrationtests.CepEspTest.ProbeCounter;\n" + "\n" + "declare ProbeEvent\n" + " @role (event)\n" + " @expires(1ms)\n" + "end\n" + "\n" + "rule \"Probe rule\"\n" + "when\n" + " $pe : ProbeEvent () from entry-point ep01\n" + " $pc : ProbeCounter ()\n" + "then\n" + " list.add( $pe.getValue() ); \n" + " $pc.addValue ();\n" + "end";
KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconfig);
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.err.println(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
EntryPoint ep01 = session.getEntryPoint("ep01");
new Thread() {
public void run() {
session.fireUntilHalt();
}
}.start();
int eventLimit = 5000;
ProbeCounter pc = new ProbeCounter();
long myTotal = 0;
try {
FactHandle pch = session.insert(pc);
for (int i = 0; i < eventLimit; i++) {
ep01.insert(new ProbeEvent(i));
myTotal++;
}
Thread.sleep(2500);
} catch (Throwable t) {
fail(t.getMessage());
}
assertEquals(eventLimit, myTotal);
assertEquals(eventLimit, list.size());
assertEquals(0, session.getEntryPoint("ep01").getObjects().size());
}
Aggregations