Search in sources :

Example 31 with KieContainer

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

the class IncrementalCompilationTest method testKeepBuilderConfAfterIncrementalUpdate.

@Test
public void testKeepBuilderConfAfterIncrementalUpdate() throws Exception {
    // DROOLS-1282
    String drl1 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + "  DummyEvent() @watch(id)\n" + "then end\n";
    String drl2 = "import " + DummyEvent.class.getCanonicalName() + "\n" + "rule R1 when\n" + "  DummyEvent() @watch(*)\n" + "then end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel().setConfigurationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.ALWAYS.toString());
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer container = ks.newKieContainer(releaseId1);
    container.newKieSession();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-property-reactive-upgrade", "2");
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl2));
    Results results = container.updateToVersion(releaseId2);
    assertEquals(0, results.getMessages().size());
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) 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 32 with KieContainer

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

the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.

@Test
public void testUpdateVersionWithKSessionLogger() {
    // DROOLS-790
    String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + "   $a : Integer()\n" + "then\n" + "   insert(new ArrayList());\n" + "end\n";
    String drl2 = "rule \"Test2\"\n" + "when\n" + "   $b : List()\n" + " then\n" + "   $b.isEmpty();\n" + "end";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
    KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
    List<Command> cmds = new ArrayList<Command>();
    cmds.add(CommandFactory.newInsertElements(new ArrayList()));
    FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
    cmds.add(fireAllRulesCommand);
    cmds.add(CommandFactory.newGetObjects("returnedObjects"));
    BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
    statelessKieSession.execute(batchExecutionCommand);
    kieRuntimeLogger.close();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
    kc.updateToVersion(km.getReleaseId());
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) 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 33 with KieContainer

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

the class IncrementalCompilationTest method testAddFieldToDeclaredType.

@Test
public void testAddFieldToDeclaredType() throws Exception {
    // DROOLS-2197
    String declares1 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "end";
    String declares2 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "   flg: String\n" + "end";
    String rules1 = "rule R when\n" + "    a : Address( city == \"Antibes\" )\n" + "then\n" + "    a.setStreetName(\"Av. Jean Medecin\");\n" + "end";
    String rules2 = "rule R when\n" + "    a : Address( city == \" Paris \", flg == \"yes\" )\n" + "then\n" + "    a.setStreetName(\" Champs Elisees \");\n" + "end";
    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");
    createAndDeployJar(ks, releaseId1, declares1, rules1);
    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieSession kieSession = kieContainer.newKieSession();
    assertEquals(0, kieSession.fireAllRules());
    createAndDeployJar(ks, releaseId2, declares2, rules2);
    kieContainer.updateToVersion(releaseId2);
    assertEquals(0, kieSession.fireAllRules());
}
Also used : 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 34 with KieContainer

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

the class IncrementalCompilationTest method testSplitAfterQuery.

@Test
public void testSplitAfterQuery() throws Exception {
    String drl1 = "global java.util.List list; " + "query foo( Integer $i ) " + "   $i := Integer( this < 10 ) " + "end\n" + "\n" + "rule r2 when " + "   foo( $i; ) " + "   Integer( this == 20 ) " + "then " + "   System.out.println(\"20 \" + $i);" + "   list.add( 20 + $i );\n" + "end\n" + "rule r3 when " + "   $i : Integer( this == 1 ) " + "then " + "   System.out.println($i);" + "   update( kcontext.getKieRuntime().getFactHandle( $i ), $i + 1 );" + "end\n" + "\n";
    String drl2 = "global java.util.List list; " + "query foo( Integer $i ) " + "   $i := Integer( this < 10 ) " + "end\n" + "\n" + "rule r1 when " + "   foo( $i ; ) " + "   Integer( this == 10 ) " + "then " + "   System.out.println(\"10 \" + $i);" + "   list.add( 10 + $i );\n" + "end\n" + "rule r2 when " + "   foo( $i; ) " + "   Integer( this == 20 ) " + "then " + "   System.out.println(\"20 \" + $i);" + "   list.add( 20 + $i );\n" + "end\n" + "rule r3 when " + "   $i : Integer( this == 1 ) " + "then " + "   System.out.println($i);" + "   update( kcontext.getKieRuntime().getFactHandle( $i ), $i + 1 );" + "end\n" + "\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(20);
    ksession.fireAllRules();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(21, (int) list.get(0));
    assertEquals(22, (int) list.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) 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 35 with KieContainer

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

the class IncrementalCompilationTest method testRetractLogicalAssertedObjectOnRuleRemoval.

@Test
public void testRetractLogicalAssertedObjectOnRuleRemoval() throws Exception {
    // DROOLS-951
    String drl1 = "rule R1 when\n" + "    exists( Integer() )\n" + "then\n" + "    insertLogical( \"found1\" );" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + "    exists( Integer() )\n" + "then\n" + "    insertLogical( \"found2\" );" + "end\n";
    String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + "    exists( Integer() )\n" + "then\n" + "    insertLogical( \"found3\");" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2, drl3);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(4);
    ksession.fireAllRules();
    assertEquals(3, ksession.getObjects(new ClassObjectFilter(String.class)).size());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl1, drl2);
    kc.updateToVersion(releaseId2);
    ksession.fireAllRules();
    assertEquals(2, ksession.getObjects(new ClassObjectFilter(String.class)).size());
    ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.3");
    km = createAndDeployJar(ks, releaseId3, drl1);
    kc.updateToVersion(releaseId3);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(String.class)).size());
    ReleaseId releaseId4 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.4");
    km = createAndDeployJar(ks, releaseId4);
    kc.updateToVersion(releaseId4);
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(String.class)).size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) 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)

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