Search in sources :

Example 1 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testEventDeclarationInSeparatedDRL.

@Test
public void testEventDeclarationInSeparatedDRL() throws Exception {
    // DROOLS-1241
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n";
    String drl2 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"MY_CODE\");\n" + "end\n";
    String drl3 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"YOUR_CODE\") over window:time( 1s )\n" + "then\n" + "    list.add(\"YOUR_CODE\");\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1, drl2));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.insert(new SimpleEvent("2", "YOUR_CODE", 0));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("MY_CODE", list.get(0));
    list.clear();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-cep-upgrade", "1.1.2");
    // the null drl placeholder is used to have the same drl with a different file name
    // this causes the removal and readdition of both rules
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, drl1, drl2, drl3));
    Results results = kc.updateToVersion(releaseId2);
    assertEquals(0, results.getMessages().size());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("YOUR_CODE", list.get(0));
}
Also used : ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) 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 2 with KieModule

use of org.kie.api.builder.KieModule 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 3 with KieModule

use of org.kie.api.builder.KieModule 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 4 with KieModule

use of org.kie.api.builder.KieModule 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 5 with KieModule

use of org.kie.api.builder.KieModule 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

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5