Search in sources :

Example 86 with KieContainer

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

the class KieBaseIncludesTest method testKieBaseIncludesCrossKJarDifferentPackageNames.

/**
 * Test the inclusion of a KieBase defined in one KJAR into the KieBase of another KJAR.
 * <p/>
 * The 2 KieBases use different package names for the rules (i.e. "rules" and "rules2").
 */
@Test
public void testKieBaseIncludesCrossKJarDifferentPackageNames() {
    // @formatter:off
    String pomContent1 = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" + "<modelVersion>4.0.0</modelVersion>\n" + "<groupId>org.kie</groupId>\n" + "<artifactId>rules-1</artifactId>\n" + "<version>1.0.0</version>\n" + "<packaging>jar</packaging>\n" + "<dependencies>\n" + "<dependency>\n" + "<groupId>org.kie</groupId>\n" + "<artifactId>rules-2</artifactId>\n" + "<version>1.0.0</version>\n" + "</dependency>\n" + "</dependencies>\n" + "</project>\n";
    String kmoduleContent1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "<kbase name=\"kbase1\" equalsBehavior=\"equality\" default=\"true\" packages=\"rules\" includes=\"kbase2\">\n" + "<ksession name=\"ksession1\" default=\"true\" type=\"stateful\"/>\n" + "</kbase>\n" + "</kmodule>";
    String drl1 = "package org.jboss.ddoyle.drools.rules1\n" + "\n" + "rule \"Rule in KieBase 1\"\n" + "when\n" + "then\n" + "System.out.println(\"Rule in KieBase1\");\n" + "end";
    String kmoduleContent2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "<kbase name=\"kbase2\" equalsBehavior=\"equality\" default=\"false\" packages=\"rules2\">\n" + "<ksession name=\"ksession2\" default=\"false\" type=\"stateful\"/>\n" + "</kbase>\n" + "</kmodule>";
    String drl2 = "package org.jboss.ddoyle.drools.rules2\n" + "\n" + "rule \"Rule in KieBase 2\"\n" + "when\n" + "then\n" + "System.out.println(\"Rule in KieBase2\");\n" + "end";
    // @formatter:on
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "rules-1", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "rules-2", "1.0.0");
    // First deploy the second KJAR on which the first one depends.
    KieFileSystem kfs2 = ks.newKieFileSystem().generateAndWritePomXML(releaseId2).write("src/main/resources/rules2/rules.drl", drl2).writeKModuleXML(kmoduleContent2);
    ks.newKieBuilder(kfs2).buildAll();
    KieFileSystem kfs1 = ks.newKieFileSystem().writePomXML(pomContent1).write("src/main/resources/rules/rules.drl", drl1).writeKModuleXML(kmoduleContent1);
    ks.newKieBuilder(kfs1).buildAll();
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieBase kieBase = kc.getKieBase();
    // Assert the number of rules in the KieBase.
    long nrOfRules = getNumberOfRules(kieBase);
    // We should have 2 rules in our KieBase. One from our own DRL and one from the DRL in the KieBase we've included.
    assertEquals(2, nrOfRules);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 87 with KieContainer

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

the class KieBuilderTest method testStatelessSessionDeclarativeChannelRegistration.

@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"));
}
Also used : Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 88 with KieContainer

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

the class KieContainerTest method testUpdateToNonExistingRelease.

@Test
public void testUpdateToNonExistingRelease() {
    // DROOLS-1562
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-release", "1.0.0");
    createAndDeployJar(ks, releaseId, createDRL("ruleA"));
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    Results results = kieContainer.updateToVersion(ks.newReleaseId("org.kie", "test-release", "1.0.1"));
    assertEquals(1, results.getMessages(Level.ERROR).size());
    assertEquals("1.0.0", ((InternalKieContainer) kieContainer).getContainerReleaseId().getVersion());
}
Also used : Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) InternalKieContainer(org.drools.core.impl.InternalKieContainer) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 89 with KieContainer

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

the class KieContainerTest method testGetDefaultKieSessionModelEmptyKmodule.

@Test
public void testGetDefaultKieSessionModelEmptyKmodule() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource = kieServices.getResources().newReaderResource(new StringReader(drl), "UTF-8");
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModelEmptyKmodule", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) KieSessionModel(org.kie.api.builder.model.KieSessionModel) Test(org.junit.Test)

Example 90 with KieContainer

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

the class KieContainerTest method testSharedTypeDeclarationsUsingFactTypes.

@Test
public void testSharedTypeDeclarationsUsingFactTypes() throws Exception {
    String type = "package org.drools.test\n" + "declare Message\n" + "   message : String\n" + "end\n";
    String drl1 = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    String drl2 = "package org.drools.test\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(ks, releaseId1, type, drl1, drl2);
    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieContainer kieContainer2 = ks.newKieContainer(releaseId1);
    KieSession ksession = kieContainer.newKieSession();
    KieSession ksession2 = kieContainer2.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(2, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-delete", "1.0.1");
    createAndDeployJar(ks, releaseId2, type, null, drl2);
    kieContainer.updateToVersion(releaseId2);
    // test with the old ksession ...
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(1, ksession.fireAllRules());
    // ... and with a brand new one
    ksession = kieContainer.newKieSession();
    insertMessageFromTypeDeclaration(ksession);
    assertEquals(1, ksession.fireAllRules());
    // check that the second kieContainer hasn't been affected by the update of the first one
    insertMessageFromTypeDeclaration(ksession2);
    assertEquals(2, ksession2.fireAllRules());
    ksession2 = kieContainer2.newKieSession();
    insertMessageFromTypeDeclaration(ksession2);
    assertEquals(2, ksession2.fireAllRules());
}
Also used : KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieContainer (org.kie.api.runtime.KieContainer)357 KieServices (org.kie.api.KieServices)271 Test (org.junit.Test)246 KieSession (org.kie.api.runtime.KieSession)191 ReleaseId (org.kie.api.builder.ReleaseId)185 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)108 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)90 KieFileSystem (org.kie.api.builder.KieFileSystem)81 KieModule (org.kie.api.builder.KieModule)75 KieBuilder (org.kie.api.builder.KieBuilder)56 KieModuleModel (org.kie.api.builder.model.KieModuleModel)54 KieBase (org.kie.api.KieBase)49 ArrayList (java.util.ArrayList)44 Results (org.kie.api.builder.Results)40 Resource (org.kie.api.io.Resource)29 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)29 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)27 Message (org.drools.compiler.Message)26 KieBaseModel (org.kie.api.builder.model.KieBaseModel)26 InternalKieContainer (org.drools.core.impl.InternalKieContainer)25