Search in sources :

Example 26 with EntryPoint

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());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 27 with EntryPoint

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;
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 28 with EntryPoint

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 29 with EntryPoint

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();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 30 with EntryPoint

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());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

EntryPoint (org.kie.api.runtime.rule.EntryPoint)70 Test (org.junit.Test)50 KieSession (org.kie.api.runtime.KieSession)46 ArrayList (java.util.ArrayList)26 KieBase (org.kie.api.KieBase)26 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)19 StockTick (org.drools.compiler.StockTick)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)16 FactHandle (org.kie.api.runtime.rule.FactHandle)15 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)13 RegistryContext (org.drools.core.command.impl.RegistryContext)12 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)8 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)6 EventFactHandle (org.drools.core.common.EventFactHandle)6 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)6 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)6