Search in sources :

Example 31 with EntryPoint

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

the class DynamicRulesTest method testDynamicRuleAdditionsWithEntryPoints.

@Test(timeout = 10000)
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl");
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    KieSession ksession = createKnowledgeSession(kbase);
    // now lets add some knowledge to the kbase
    kbase.addPackages(kpkgs);
    List<StockTick> results = new ArrayList<StockTick>();
    ksession.setGlobal("results", results);
    EntryPoint ep = ksession.getEntryPoint("in-channel");
    ep.insert(new StockTick(1, "RHT", 20, 10000));
    ep.insert(new StockTick(2, "RHT", 21, 15000));
    ep.insert(new StockTick(3, "RHT", 22, 20000));
    ksession.fireAllRules();
    assertEquals(3, results.size());
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 32 with EntryPoint

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

the class CepEspTest method testTemporalOperators2.

@Test(timeout = 10000)
public void testTemporalOperators2() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators2.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    EntryPoint ep = ksession.getEntryPoint("X");
    clock.advanceTime(1000, TimeUnit.SECONDS);
    ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    int rules = ksession.fireAllRules();
// assertEquals( 2,
// rules );
// assertEquals( 1, list.size() );
// StockTick[] stocks = ( StockTick[] ) list.get(0);
// assertSame( tick4, stocks[0]);
// assertSame( tick2, stocks[1]);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 33 with EntryPoint

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

the class CepEspTest method testEqualityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testEqualityAssertBehaviorOnEntryPoints() 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.EQUALITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession1 = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession1.addEventListener(ael1);
    EntryPoint ep1 = ksession1.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);
    assertSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    ksession1.fireAllRules();
    // must have fired 2 times, one for each event equality
    verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession1.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) 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) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 34 with EntryPoint

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

the class CepEspTest method testDuplicateFiring1.

@Test
public void testDuplicateFiring1() throws InterruptedException {
    String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "  accumulate ( $e: StockTick() over window:time(300ms) from entry-point SensorEventStream, " + "              $n : count( $e );" + "              $n > 0 )\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "" + "\n" + "rule \"timerRuleAfterAllEvents\"\n" + "        timer ( int: 2s )\n" + "when\n" + "        $room : String( )\n" + "then\n" + "  list.add( -1 ); \n" + "  System.out.println(\"2sec after room was modified\");\n" + "end " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    // Check the builder for errors
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // configure knowledge base
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // init session clock
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    // init stateful knowledge session
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    SessionPseudoClock clock = ksession.getSessionClock();
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    // entry point for sensor events
    EntryPoint sensorEventStream = ksession.getEntryPoint("SensorEventStream");
    ksession.insert("Go");
    System.out.println("1. fireAllRules()");
    // insert events
    for (int i = 2; i < 8; i++) {
        StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
        sensorEventStream.insert(event);
        System.out.println(i + ". fireAllRules()");
        ksession.fireAllRules();
        clock.advanceTime(105, TimeUnit.MILLISECONDS);
    }
    // let thread sleep for another 1m to see if dereffered rules fire (timers, (not) after rules)
    clock.advanceTime(100 * 40 * 1, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(Arrays.asList(1L, 2L, 3L, 3L, 3L, 3L, -1), list);
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 35 with EntryPoint

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

the class DataDictionaryTest method testEntryPoints.

@Test
public void testEntryPoints() throws Exception {
    setKSession(getModelSession(source, VERBOSE));
    setKbase(getKSession().getKieBase());
    FactType gender = getKbase().getFactType(packageName, "Gender");
    FactType noclaims = getKbase().getFactType(packageName, "NoOfClaims");
    FactType scrambled = getKbase().getFactType(packageName, "Scrambled");
    FactType domicile = getKbase().getFactType(packageName, "Domicile");
    FactType agecar = getKbase().getFactType(packageName, "AgeOfCar");
    FactType amklaims = getKbase().getFactType(packageName, "AmountOfClaims");
    assertEquals(7, getKSession().getEntryPoints().size());
    EntryPoint gender_ep = getKSession().getEntryPoint("in_Gender");
    Assert.assertNotNull(gender_ep);
    EntryPoint noclaims_ep = getKSession().getEntryPoint("in_NoOfClaims");
    Assert.assertNotNull(noclaims_ep);
    EntryPoint scrambled_ep = getKSession().getEntryPoint("in_Scrambled");
    Assert.assertNotNull(scrambled_ep);
    EntryPoint domicile_ep = getKSession().getEntryPoint("in_Domicile");
    Assert.assertNotNull(domicile_ep);
    EntryPoint agecar_ep = getKSession().getEntryPoint("in_AgeOfCar");
    Assert.assertNotNull(agecar_ep);
    EntryPoint amklaims_ep = getKSession().getEntryPoint("in_AmountOfClaims");
    Assert.assertNotNull(amklaims_ep);
    gender_ep.insert("M");
    noclaims_ep.insert("> 4");
    scrambled_ep.insert(4);
    domicile_ep.insert("way out");
    agecar_ep.insert(new Double("3.4"));
    amklaims_ep.insert(9);
    getKSession().fireAllRules();
    assertEquals(6, getKSession().getObjects().size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(gender.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(noclaims.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(scrambled.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(domicile.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(agecar.getFactClass())).size());
    assertEquals(1, getKSession().getObjects(new ClassObjectFilter(amklaims.getFactClass())).size());
    checkFirstDataFieldOfTypeStatus(amklaims, true, false, null, 9);
    checkFirstDataFieldOfTypeStatus(domicile, false, false, null, "way out");
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) EntryPoint(org.kie.api.runtime.rule.EntryPoint) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) 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