Search in sources :

Example 11 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class MarshallingTest method testMarshallEntryPointsWithSlidingLengthWindow.

@Test
public void testMarshallEntryPointsWithSlidingLengthWindow() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $l : List() from collect( A()  over window:length(3) from entry-point 'a-ep') \n" + "then\n" + "   list.add( $l );" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(2, ((List) list.get(0)).size());
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(3, ((List) list.get(0)).size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.compiler.FactA) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 12 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class GetEntryPointCommand method execute.

public EntryPoint execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(name);
    if (ep == null) {
        return null;
    }
    EntryPointCreator epCreator = (EntryPointCreator) context.get(EntryPointCreator.class.getName());
    return epCreator != null ? epCreator.getEntryPoint(name) : ep;
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPointCreator(org.drools.core.command.EntryPointCreator) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 13 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class DeleteObjectCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(entryPoint);
    if (ep != null) {
        FactHandle handle = ksession.getEntryPoint(entryPoint).getFactHandle(object);
        ksession.delete(handle);
    }
    return null;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 14 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class InsertElementsCommand method execute.

public Collection<FactHandle> execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    List<FactHandle> handles = new ArrayList<FactHandle>();
    EntryPoint wmep;
    if (StringUtils.isEmpty(this.entryPoint)) {
        wmep = ksession;
    } else {
        wmep = ksession.getEntryPoint(this.entryPoint);
    }
    for (Object object : objects) {
        handles.add(wmep.insert(object));
    }
    if (outIdentifier != null) {
        if (this.returnObject) {
            ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, objects);
        }
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).getFactHandles().put(this.outIdentifier, handles);
    }
    return handles;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 15 with EntryPoint

use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.

the class NegativePatternsTest method testMultipleEntryPoints.

@Test
public void testMultipleEntryPoints() throws InterruptedException {
    EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
    EntryPoint otherStream = ksession.getEntryPoint("OtherStream");
    int count = 0;
    count++;
    ksession.fireAllRules();
    advanceTime(LONG_SLEEP_TIME);
    ksession.fireAllRules();
    assertEquals(count, firedRulesListener.ruleFiredCount("MultipleEntryPoints"));
    FactHandle handle;
    for (int i = 0; i < LOOPS; i++) {
        handle = entryPoint.insert(new TestEvent(count, "EventA"));
        ksession.fireAllRules();
        advanceTime(LONG_SLEEP_TIME);
        entryPoint.delete(handle);
        ksession.fireAllRules();
        count++;
        advanceTime(LONG_SLEEP_TIME);
        ksession.fireAllRules();
    }
    for (int i = 0; i < LOOPS; i++) {
        handle = otherStream.insert(new TestEvent(count, "EventB"));
        advanceTime(LONG_SLEEP_TIME);
        ksession.fireAllRules();
        otherStream.delete(handle);
        count++;
        advanceTime(SHORT_SLEEP_TIME);
        ksession.fireAllRules();
    }
    ksession.fireAllRules();
    assertEquals(count, firedRulesListener.ruleFiredCount("MultipleEntryPoints"));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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