Search in sources :

Example 1 with KieSessionConfiguration

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

the class WindowTest method initialization.

@Before
public void initialization() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.write("src/main/resources/kbase1/window_test.drl", drl);
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
    assertEquals(res.toString(), 0, res.size());
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieBase(kbconf);
    KieSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconfig.setOption(ClockTypeOption.get("pseudo"));
    ksession = kbase.newKieSession(ksconfig, null);
    clock = ksession.getSessionClock();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieBase(org.kie.api.KieBase) KieBuilder(org.kie.api.builder.KieBuilder) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Before(org.junit.Before)

Example 2 with KieSessionConfiguration

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

the class MultithreadTest method testConcurrentFireAndDispose.

@Test(timeout = 10000)
public void testConcurrentFireAndDispose() throws InterruptedException {
    // DROOLS-1103
    final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + "    String()\n" + "then\n" + "end";
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    final KieSession ksession = kbase.newKieSession(ksconf, null);
    final Thread t1 = new Thread() {

        @Override
        public void run() {
            LOG.info("before: sleep, dispose().");
            try {
                Thread.sleep(100);
            } catch (final InterruptedException _e) {
            }
            LOG.info("before: dispose().");
            ksession.dispose();
            LOG.info("after: dispose().");
        }
    };
    t1.setDaemon(true);
    t1.start();
    try {
        int i = 0;
        LOG.info("before: while.");
        while (true) {
            ksession.insert("" + i++);
            ksession.fireAllRules();
        }
    } catch (final IllegalStateException e) {
        // java.lang.IllegalStateException: Illegal method call. This session was previously disposed.
        // ignore and exit
        LOG.info("after: while.");
    } catch (final java.util.concurrent.RejectedExecutionException e) {
        e.printStackTrace();
        Assertions.fail("java.util.concurrent.RejectedExecutionException should not happen");
    }
    LOG.info("last line of test.");
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 3 with KieSessionConfiguration

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

the class MultithreadTest method testConcurrencyWithChronThreads.

@Test
@Ignore
public void testConcurrencyWithChronThreads() throws InterruptedException {
    final String drl = "package it.intext.drools.fusion.bug;\n" + "\n" + "import " + MyFact.class.getCanonicalName() + ";\n " + " global java.util.List list; \n" + "\n" + "declare MyFact\n" + "\t@role( event )\n" + "\t@expires( 1s )\n" + "end\n" + "\n" + "rule \"Dummy\"\n" + "timer( cron: 0/1 * * * * ? )\n" + "when\n" + "  Number( $count : intValue ) from accumulate( MyFact( ) over window:time(1s); sum(1) )\n" + "then\n" + "    System.out.println($count+\" myfact(s) seen in the last 1 seconds\");\n" + "    list.add( $count ); \n" + "end";
    final KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconfig.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconfig, drl);
    final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("REALTIME"));
    final KieSession ksession = kbase.newKieSession(conf, null);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    final Runner t = new Runner(ksession);
    t.start();
    final int FACTS_PER_POLL = 1000;
    final int POLL_INTERVAL = 500;
    final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(new Runnable() {

        public void run() {
            for (int j = 0; j < FACTS_PER_POLL; j++) {
                ksession.insert(new MyFact());
            }
        }
    }, 0, POLL_INTERVAL, TimeUnit.MILLISECONDS);
    Thread.sleep(10200);
    executor.shutdownNow();
    ksession.halt();
    t.join();
    if (t.getError() != null) {
        Assertions.fail(t.getError().getMessage());
    }
    System.out.println("Final size " + ksession.getObjects().size());
    // assertEquals( 2000, ksession.getObjects().size() );
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with KieSessionConfiguration

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

the class MultithreadTest method testFireUntilHaltAndDispose.

@Test(timeout = 10000)
public void testFireUntilHaltAndDispose() throws InterruptedException {
    // DROOLS-1103
    final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + "    String()\n" + "then\n" + "end";
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    final KieSession ksession = kbase.newKieSession(ksconf, null);
    new Thread() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    }.start();
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.insert("xxx");
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 5 with KieSessionConfiguration

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

the class IncrementalCompilationTest method testAlphaNodeSharingIsOK.

@Test
public void testAlphaNodeSharingIsOK() throws Exception {
    // inspired by drools-usage Fmt9wZUFi8g
    // check timer -scheduled activations are preserved if rule untouched by incremental compilation even with alpha node sharing.
    StringBuffer drl = new StringBuffer();
    drl.append("package org.drools.compiler\n");
    drl.append("global java.util.List list;\n");
    drl.append("global java.util.List list2;\n");
    drl.append("rule R1\n");
    drl.append(" timer (int: 3s)\n");
    drl.append(" when\n");
    drl.append("   $m : String()\n");
    drl.append(" then\n");
    drl.append("   list.add( $m );\n");
    drl.append("   retract( $m );\n");
    drl.append("end\n");
    drl.append("rule RS\n");
    drl.append(" timer (int: 3s)\n");
    drl.append(" salience 1\n");
    drl.append(" when\n");
    drl.append("   $i : Integer()\n");
    drl.append("   $m : String()\n");
    drl.append(" then\n");
    drl.append("   System.out.println($i + \" \"+ $m);");
    drl.append("   list2.add($i + \" \"+ $m);\n");
    drl.append("end\n");
    StringBuffer drl2 = new StringBuffer();
    drl2.append("package org.drools.compiler\n");
    drl2.append("global java.util.List list;\n");
    drl2.append("global java.util.List list2;\n");
    drl2.append("rule R1\n");
    drl2.append(" timer (int: 3s)\n");
    drl2.append(" when\n");
    drl2.append("   $m : String()\n");
    drl2.append(" then\n");
    drl2.append("   list.add( $m );\n");
    drl2.append("   list.add( $m );\n");
    drl2.append("   retract( $m );\n");
    drl2.append("end\n");
    drl2.append("rule RS\n");
    drl2.append(" timer (int: 3s)\n");
    drl2.append(" salience 1\n");
    drl2.append(" when\n");
    drl2.append("   $i : Integer()\n");
    drl2.append("   $m : String()\n");
    drl2.append(" then\n");
    drl2.append("   System.out.println($i + \" \"+ $m);");
    drl2.append("   list2.add($i + \" \"+ $m);\n");
    drl2.append("end\n");
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJarInStreamMode(ks, releaseId1, drl.toString());
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSessionConfiguration ksconf = ks.newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kc.newKieSession(ksconf);
    SessionPseudoClock timeService = ksession.getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    List list2 = new ArrayList();
    ksession.setGlobal("list2", list2);
    ksession.insert(new String("A"));
    ksession.insert(1);
    ksession.fireAllRules();
    assertEquals("1. Initial run: no message expected after rule fired immediately after fireAllRules due to duration of 5 sec", 0, list.size());
    assertEquals("1. Initial run: no message expected after rule fired immediately after fireAllRules due to duration of 5 sec", 0, list2.size());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.1");
    km = createAndDeployJarInStreamMode(ks, releaseId2, drl2.toString());
    kc.updateToVersion(releaseId2);
    timeService.advanceTime(3200, TimeUnit.MILLISECONDS);
    assertEquals("1. R1 is NOT preserved", 0, list.size());
    assertEquals("1. RS is preserved", 1, list2.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Date(java.util.Date) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)202 KieSession (org.kie.api.runtime.KieSession)157 KieBase (org.kie.api.KieBase)138 Test (org.junit.Test)136 ArrayList (java.util.ArrayList)76 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)61 List (java.util.List)55 KieHelper (org.kie.internal.utils.KieHelper)32 Arrays.asList (java.util.Arrays.asList)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)25 SessionConfiguration (org.drools.core.SessionConfiguration)23 Date (java.util.Date)21 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)20 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)16 FactHandle (org.kie.api.runtime.rule.FactHandle)16 Collection (java.util.Collection)15 Person (org.drools.testcoverage.common.model.Person)15 Properties (java.util.Properties)14