Java Code Examples for org.kie.api.runtime.KieContainer#newStatelessKieSession()

The following examples show how to use org.kie.api.runtime.KieContainer#newStatelessKieSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListenersTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void init() {
    ReleaseId kieModuleId = prepareKieModule();

    final KieContainer kieContainer = ks.newKieContainer(kieModuleId);
    this.kieSession = kieContainer.newKieSession();
    this.statelessKieSession = kieContainer.newStatelessKieSession();
}
 
Example 2
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
    final String drl1 = "package org.drools.compiler\n" +
            "rule R1 when\n" +
            "   $m : Message()\n" +
            "then\n" +
            "end\n";

    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
            "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"kbase1\">\n" +
            "    <ksession name=\"ksession1\" default=\"true\" type=\"stateless\">\n" +
            "       <channels>\n" +
            "         <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" +
            "       </channels>\n" +
            "    </ksession>" +
            "  </kbase>\n" +
            "</kmodule>";

    final KieServices ks = KieServices.Factory.get();

    final ReleaseId releaseId1 = ks.newReleaseId( "org.kie", "test-kie-builder", "1.0.0" );
    final Resource r1 = ResourceFactory.newByteArrayResource( drl1.getBytes() ).setResourceType( ResourceType.DRL ).setSourcePath( "kbase1/drl1.drl" );
    final KieModule km = createAndDeployJar( ks,
                                       kmodule,
                                       releaseId1,
                                       r1);

    KieContainer kieContainer = ks.newKieContainer( km.getReleaseId());
    
    StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
    assertEquals(1, statelessKieSession.getChannels().size());
    assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
 
Example 3
Source File: KieLoggersTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclarativeKieConsoleLoggerStateless() throws Exception {
    String drl = "package org.drools.integrationtests\n" +
                 "import org.drools.compiler.Message;\n" +
                 "rule \"Hello World\"\n" +
                 "    when\n" +
                 "        m : Message( myMessage : message )\n" +
                 "    then\n" +
                 "end";

    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();

    kproj.newKieBaseModel("KBase1")
         .newKieSessionModel("KSession1")
         .setType(KieSessionModel.KieSessionType.STATELESS)
         .setConsoleLogger("logger");

    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/KBase1/rule.drl", drl);

    KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());

    StatelessKieSession ksession = kieContainer.newStatelessKieSession("KSession1");
    ksession.execute( new Message("Hello World") );

    KieRuntimeLogger logger = ksession.getLogger();
    assertNotNull(logger);
    logger.close();
}
 
Example 4
Source File: MBeansMonitoringTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testKieClasspathMBeans() throws Exception {
    MBeanServer mbserver = ManagementFactory.getPlatformMBeanServer();
    KieServices ks = KieServices.Factory.get();

    KieContainer kc = ks.getKieClasspathContainer("myID");
    
    KieContainerMonitorMXBean c1Monitor = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID"),
            KieContainerMonitorMXBean.class);
    KieBase kb = kc.getKieBase("org.kie.monitoring.kbase1");
    KieSession statefulKieSession = kc.newKieSession("org.kie.monitoring.kbase1.ksession1");
    StatelessKieSession statelessKieSession = kc.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
    
    KieSessionMonitoringMXBean statefulKieSessionMonitor = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"),
            KieSessionMonitoringMXBean.class);
    StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"),
            StatelessKieSessionMonitoringMXBean.class);
    
    assertEquals("myID", c1Monitor.getContainerId() );
    assertTrue(c1Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertTrue(c1Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1",           statefulKieSessionMonitor.getKieBaseId());
    assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1",           statelessKieSessionMonitor.getKieBaseId());
    
    
    KieContainer kc2 = ks.newKieClasspathContainer("myID2");
    KieContainerMonitorMXBean c2Monitor = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID2"),
            KieContainerMonitorMXBean.class);
    KieBase kb2 = kc2.getKieBase("org.kie.monitoring.kbase1");
    KieSession statefulKieSession2 = kc2.newKieSession("org.kie.monitoring.kbase1.ksession1");
    StatelessKieSession statelessKieSession2 = kc2.newStatelessKieSession("org.kie.monitoring.kbase1.ksession2");
    KieSessionMonitoringMXBean statefulKieSessionMonitor2 = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"),
            KieSessionMonitoringMXBean.class);
    StatelessKieSessionMonitoringMXBean statelessKieSessionMonitor2 = JMX.newMXBeanProxy(
            mbserver,
            DroolsManagementAgent.createObjectNameBy("myID2", "org.kie.monitoring.kbase1", KieSessionType.STATEFUL, "org.kie.monitoring.kbase1.ksession1"),
            StatelessKieSessionMonitoringMXBean.class);
    
    assertEquals("myID2", c2Monitor.getContainerId() );
    assertTrue(c2Monitor.getConfiguredReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertTrue(c2Monitor.getResolvedReleaseId().sameGAVof(KieContainerMonitorMXBean.CLASSPATH_KIECONTAINER_RELEASEID));
    assertEquals("org.kie.monitoring.kbase1.ksession1", statefulKieSessionMonitor2.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1",           statefulKieSessionMonitor2.getKieBaseId());
    assertEquals("org.kie.monitoring.kbase1.ksession1", statelessKieSessionMonitor2.getKieSessionName());
    assertEquals("org.kie.monitoring.kbase1",           statelessKieSessionMonitor2.getKieBaseId());
    
    kc.dispose();
    kc2.dispose();
}
 
Example 5
Source File: KieSessionFactory.java    From NiFi-Rule-engine-processor with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getNewKieSession(String drlFileName) {
	KieServices kieServices = KieServices.Factory.get();
	
	KieResources kieResources = kieServices.getResources();
	KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
	KieRepository kieRepository = kieServices.getRepository();

	Resource resource = kieResources.newFileSystemResource(drlFileName);
	kieFileSystem.write(resource);

	KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);

	kb.buildAll();

	if (kb.getResults().hasMessages(Level.ERROR)) {
		throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
	}

	KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
	
	return kContainer.newStatelessKieSession();
}
 
Example 6
Source File: KnowledgeSessionHelper.java    From droolscourse with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getStatelessKnowledgeSession(KieContainer kieContainer, String sessionName) {
     StatelessKieSession kSession = kieContainer.newStatelessKieSession(sessionName);

    return kSession;
}
 
Example 7
Source File: KnowledgeSessionHelper.java    From droolscourse with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getStatelessKnowledgeSession(KieContainer kieContainer, String sessionName) {
    StatelessKieSession kSession = kieContainer.newStatelessKieSession(sessionName);

    return kSession;
}
 
Example 8
Source File: KnowledgeSessionHelper.java    From droolscourse with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getStatelessKnowledgeSession(KieContainer kieContainer, String sessionName) {
    StatelessKieSession kSession = kieContainer.newStatelessKieSession(sessionName);

    return kSession;
}