Search in sources :

Example 76 with KieSessionConfiguration

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

the class CepEspTest method testAnnotatedEventAssertion.

@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession session = createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    clock.advanceTime(30, TimeUnit.SECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTickEvent(org.drools.compiler.StockTickEvent) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 77 with KieSessionConfiguration

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

the class CommandsTest method testSessionTimeCommands.

@Test
public void testSessionTimeCommands() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    KieServices ks = KieServices.get();
    KieCommands kieCommands = ks.getCommands();
    KieSessionConfiguration sessionConfig = ks.newKieSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession kSession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(sessionConfig, null);
    assertEquals(0L, (long) kSession.execute(kieCommands.newGetSessionTime()));
    assertEquals(2000L, (long) kSession.execute(kieCommands.newAdvanceSessionTime(2, TimeUnit.SECONDS)));
    assertEquals(2000L, (long) kSession.execute(kieCommands.newGetSessionTime()));
    kSession.dispose();
}
Also used : KieCommands(org.kie.api.command.KieCommands) KieHelper(org.kie.internal.utils.KieHelper) Cheese(org.drools.compiler.Cheese) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 78 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2Paths.

@Test
public void testEagerEvaluationWith2Paths() throws Exception {
    String str = "package org.simple \n" + "rule xxx \n" + "when \n" + "  $s : String()\n" + "  $i : Integer()\n" + "then \n" + "end  \n" + "rule yyy \n" + "when \n" + "  $s : String()\n" + "  $i : Integer()\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() {

        @Override
        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("activated");
        }

        @Override
        public void matchCancelled(MatchCancelledEvent event) {
            list.add("cancelled");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    assertEquals(0, list.size());
    FactHandle fh = ksession.insert(1);
    assertEquals(2, list.size());
    assertEquals("activated", list.get(0));
    assertEquals("activated", list.get(1));
    list.clear();
    ksession.delete(fh);
    assertEquals(2, list.size());
    assertEquals("cancelled", list.get(0));
    assertEquals("cancelled", list.get(1));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieServices(org.kie.api.KieServices) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) 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) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 79 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testSegMemInitializationWithForceEagerActivation.

@Test(timeout = 10000L)
public void testSegMemInitializationWithForceEagerActivation() 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" + "    eval(true)\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 80 with KieSessionConfiguration

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

the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2SubPaths.

@Test
public void testEagerEvaluationWith2SubPaths() 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(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("activated");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    assertEquals(0, list.size());
    ksession.insert(1);
    assertEquals(2, list.size());
}
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)

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