Search in sources :

Example 6 with EntryPoint

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

the class ModifyCommand method execute.

public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint wmep = ksession.getEntryPoint(factHandle.getEntryPointId());
    Object object = wmep.getObject(this.factHandle);
    MVELSafeHelper.getEvaluator().eval(getMvelExpr(), object);
    wmep.update(factHandle, object);
    return object;
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 7 with EntryPoint

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

the class InsertObjectInEntryPointCommand method execute.

public FactHandle execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(entryPoint);
    FactHandle factHandle = ep.insert(object);
    DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) factHandle).clone();
    disconnectedHandle.disconnect();
    if (outIdentifier != null) {
        if (this.returnObject) {
            ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, object);
        }
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).getFactHandles().put(this.outIdentifier, disconnectedHandle);
    }
    return disconnectedHandle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 8 with EntryPoint

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

the class WindowTest method testTimeWindow.

@Test
public void testTimeWindow() throws InterruptedException {
    EntryPoint entryPoint = ksession.getEntryPoint("EventStream");
    final long[] results = new long[] { 1, 2, 3, 3, 3 };
    TestEvent event;
    for (int i = 0; i < 5; i++) {
        event = new TestEvent(null, "time", null);
        entryPoint.insert(event);
        assertEquals(results[i], ksession.getQueryResults("TestTimeWindow").iterator().next().get("$eventCount"));
        clock.advanceTime(100, TimeUnit.MILLISECONDS);
    }
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 9 with EntryPoint

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

the class StreamsTest method testModifyOnEntryPointFacts.

// (timeout=10000)
@Test
public void testModifyOnEntryPointFacts() throws Exception {
    String str = "package org.drools.compiler\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 100\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep1\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R2 salience 90\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep2\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R3 salience 80\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep3\n" + "    then\n" + "        StockTick s = $s1;\n" + "        modify( s ) { setPrice( 50 ) };\n" + "end\n";
    // read in the source
    KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
    KieSession ksession = createKnowledgeSession(kbase);
    org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep1 = ksession.getEntryPoint("ep1");
    EntryPoint ep2 = ksession.getEntryPoint("ep2");
    EntryPoint ep3 = ksession.getEntryPoint("ep3");
    ep1.insert(new StockTick(1, "RHT", 10, 1000));
    ep2.insert(new StockTick(1, "RHT", 10, 1000));
    ep3.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(3, rulesFired);
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(3)).afterMatchFired(captor.capture());
    List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(1).getMatch().getRule().getName(), is("R2"));
    assertThat(aafe.get(2).getMatch().getRule().getName(), is("R3"));
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 10 with EntryPoint

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

the class MarshallingTest method testMarshallEntryPointsWithNot.

@Test
public void testMarshallEntryPointsWithNot() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\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" + "   $a : A() from entry-point 'a-ep'\n" + "   not B( this after[0s, 10s] $a) from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kBase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = kBase.newKieSession(ksconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(3, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(0, list.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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) 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