Search in sources :

Example 81 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testSegMemInitializationWithForceEagerActivationAndAcc.

@Test(timeout = 10000L)
public void testSegMemInitializationWithForceEagerActivationAndAcc() throws InterruptedException {
    // DROOLS-1247
    String drl = "global java.util.List list\n" + "declare  SimpleFact end\n" + "declare  AnotherFact end\n" + "\n" + "rule Init when\n" + "    not (SimpleFact())\n" + "    not (AnotherFact())\n" + "then\n" + "    insert(new SimpleFact());\n" + "    insert(new AnotherFact());\n" + "end\n" + "\n" + "rule R1 no-loop when\n" + "    $f : SimpleFact()  \n" + "    $h : AnotherFact() \n" + "    $s : String() \n" + "    accumulate($i: Integer(), $res : count($i))\n" + "then\n" + "    list.add(\"1\");\n" + "end\n" + "\n" + "rule R2 no-loop when\n" + "    $f : SimpleFact()  \n" + "    $h : AnotherFact()  \n" + "    $s : String() \n" + "then\n" + "    list.add(\"2\");\n" + "end";
    KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
    config.setOption(ForceEagerActivationOption.YES);
    final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(config, null);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("test");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("1", "2")));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 82 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.

@Test
public void testActivateOnMatchAndUpdate() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().update(alarm, new Alarm());
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 83 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testOneLinkedAndOneUnlinkedPath.

@Test
public void testOneLinkedAndOneUnlinkedPath() throws Exception {
    String str = "package org.simple \n" + "rule xxx \n" + "when \n" + "  String()\n" + "  Integer()\n" + "  Long()\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  String()\n" + "  Integer()\n" + "  Boolean()\n" + "then \n" + "end  \n";
    KieServices ks = KieServices.Factory.get();
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add(event.getMatch().getRule().getName());
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    assertEquals(0, list.size());
    ksession.insert(Boolean.TRUE);
    assertEquals(0, list.size());
    ksession.insert(1);
    assertEquals(1, list.size());
    assertEquals("yyy", list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 84 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.

@Test
public void testActivateOnMatchAndDelete() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().delete(alarm);
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
    ksession.fireAllRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 85 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testOneLazyAndOneImmediateSubPath.

@Test
public void testOneLazyAndOneImmediateSubPath() throws Exception {
    String str = "package org.simple \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  exists( Integer() or Long() )\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  $s : String()\n" + "  exists( Integer() or Long() )\n" + "then \n" + "end  \n";
    KieServices ks = KieServices.Factory.get();
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {

        @Override
        public boolean accept(Rule rule) {
            return rule.getName().equals("yyy");
        }
    }));
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add(event.getMatch().getRule().getName());
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    assertEquals(0, list.size());
    ksession.insert(1);
    assertEquals(1, list.size());
    assertEquals("yyy", list.get(0));
}
Also used : ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) ForceEagerActivationFilter(org.kie.internal.runtime.conf.ForceEagerActivationFilter) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)204 KieSession (org.kie.api.runtime.KieSession)168 Test (org.junit.Test)160 KieBase (org.kie.api.KieBase)126 ArrayList (java.util.ArrayList)98 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)71 KieHelper (org.kie.internal.utils.KieHelper)69 List (java.util.List)59 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)57 StockTick (org.drools.compiler.StockTick)40 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)39 EntryPoint (org.kie.api.runtime.rule.EntryPoint)28 Arrays.asList (java.util.Arrays.asList)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)26 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)22 Date (java.util.Date)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 StockTickInterface (org.drools.compiler.StockTickInterface)18 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)16 SessionConfiguration (org.drools.core.SessionConfiguration)15