Search in sources :

Example 51 with KieContainer

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

the class IncrementalCompilationTest method testIncrementalCompilation.

private void testIncrementalCompilation(ReleaseId releaseId1, ReleaseId releaseId2, boolean useDeclaredType) {
    String drl1 = "package org.drools.compiler\n" + "global java.util.List list\n" + "rule R0 when then list.add( \"000\" ); end \n" + "" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"a\" + $s );" + "end\n";
    String drl2 = useDeclaredType ? "package org.drools.compiler\n" + "global java.util.List list\n" + "declare StringWrapper\n" + " s : String\n" + "end\n" + "rule RInit when\n" + " $s : String() \n" + "then\n" + " insert( new StringWrapper( $s ) );" + "end\n" + "rule R2 when\n" + " $s : StringWrapper() \n" + "then\n" + " list.add( \"b\" + $s.getS() );" + "end\n" : "package org.drools.compiler\n" + "global java.util.List list\n" + "rule R2 when\n" + " $s : String() \n" + "then\n" + " list.add( \"b\" + $s );" + "end\n";
    KieServices ks = KieServices.Factory.get();
    createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("Foo");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("000", "aFoo")));
    list.clear();
    createAndDeployJar(ks, releaseId2, drl1, drl2);
    Results updateResults = kc.updateToVersion(releaseId2);
    assertEquals(0, updateResults.getMessages().size());
    ksession.insert("Bar");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.containsAll(asList("bBar", "bFoo", "aBar")));
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) KieContainer(org.kie.api.runtime.KieContainer)

Example 52 with KieContainer

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

the class IncrementalCompilationTest method testAddPackageToKieBaseModel.

@Test
public void testAddPackageToKieBaseModel() throws Exception {
    // DROOLS-1287
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.1");
    createKJarWIthPackages(ks, releaseId1, "pkg2");
    KieContainer container = ks.newKieContainer(releaseId1);
    KieSession ksession = container.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("test");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.containsAll(asList("R2")));
    createKJarWIthPackages(ks, releaseId2, "pkg1", "pkg2");
    Results results = container.updateToVersion(releaseId2);
    assertEquals(0, results.getMessages().size());
    ksession = container.newKieSession();
    list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("test");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("R1", "R2")));
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 53 with KieContainer

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

the class IncrementalCompilationTest method testRemoveAndReaddJavaClass.

@Test
public void testRemoveAndReaddJavaClass() {
    // DROOLS-1704
    String javaSource = "package org.drools.test;\n" + "public class Person { }\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.2.0");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId1);
    KieModuleModel kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kContainer = ks.newKieContainer(releaseId1);
    try {
        Class.forName("org.drools.test.Person", true, kContainer.getClassLoader());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    kContainer.getKieBase();
    kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId2);
    kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    ks.newKieBuilder(kfs).buildAll();
    kContainer.updateToVersion(releaseId2);
    kContainer.getKieBase();
    kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId3);
    kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
    ks.newKieBuilder(kfs).buildAll();
    kContainer.updateToVersion(releaseId3);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 54 with KieContainer

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace4_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace4_usingSingleQuote() throws Exception {
    // DROOLS-1399 quater (using single quote)
    String drl_1 = "package org.drools.compiler\n" + "rule Rx when\n" + "   $m : Message( message == 'Hello  World' )\n" + "then\n" + "end\n";
    String drl_2 = "package org.drools.compiler\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE typo was removed
    "   $m : Message( message == 'Hello World' )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl_2);
    kc.updateToVersion(releaseId2);
    // rule Rx is UNchanged and should NOT fire again
    assertEquals(1, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) 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 55 with KieContainer

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

the class IncrementalCompilationTest method testRuleRemoval.

@Test
public void testRuleRemoval() throws Exception {
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2 = "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl3 = "rule R3 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\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-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1 + drl2 + drl3);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KiePackage kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
    assertEquals(3, kpkg.getRules().size());
    Map<String, Rule> rules = rulestoMap(kpkg.getRules());
    assertNotNull(rules.get("R1"));
    assertNotNull(rules.get("R2"));
    assertNotNull(rules.get("R3"));
    RuleTerminalNode rtn1_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
    RuleTerminalNode rtn2_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R2")[0];
    RuleTerminalNode rtn3_1 = (RuleTerminalNode) ((KnowledgeBaseImpl) kc.getKieBase()).getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1 + drl3);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    KnowledgeBaseImpl rb_2 = ((KnowledgeBaseImpl) kc.getKieBase());
    RuleTerminalNode rtn1_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R1")[0];
    RuleTerminalNode rtn3_2 = (RuleTerminalNode) rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R3")[0];
    assertNull(rb_2.getReteooBuilder().getTerminalNodes("org.drools.compiler.R2"));
    assertSame(rtn3_1, rtn3_2);
    assertSame(rtn1_1, rtn1_2);
    kpkg = kc.getKieBase().getKiePackage("org.drools.compiler");
    assertEquals(2, kpkg.getRules().size());
    rules = rulestoMap(kpkg.getRules());
    assertNotNull(rules.get("R1"));
    assertNull(rules.get("R2"));
    assertNotNull(rules.get("R3"));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieServices(org.kie.api.KieServices) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) Rule(org.kie.api.definition.rule.Rule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) 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