use of org.kie.api.builder.Results 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));
}
use of org.kie.api.builder.Results 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());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.
@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end ";
String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end\n" + "declare Score " + " value : double " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testIncremenatalCompilationAddingFieldToDeclaredType.
@Test
public void testIncremenatalCompilationAddingFieldToDeclaredType() {
// 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 \" )\n" + "then\n" + " a.setStreetName(\" Champs Elisees \");\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
kfs.write(ks.getResources().newReaderResource(new StringReader(declares1)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
kfs.write(ks.getResources().newReaderResource(new StringReader(rules1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
ksession.fireAllRules();
kfs.write(ks.getResources().newReaderResource(new StringReader(declares2)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
kfs.write(ks.getResources().newReaderResource(new StringReader(rules2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testJavaClassRedefinition.
@Test
public void testJavaClassRedefinition() {
// DROOLS-1402
String JAVA1 = "package org.test;" + " public class MyBean {\n" + " private String firstName;\n" + " public MyBean() { /* empty constructor */ }\n" + " public MyBean(String firstName) { this.firstName = firstName; }\n" + " public String getFirstName() { return firstName; }\n" + " public void setFirstName(String firstName) { this.firstName = firstName; }\n" + " }";
String DRL1 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + " when\n" + " $b : MyBean( firstName == null )\n" + " then\n" + " System.out.println($b);" + "end";
String INIT_DRL = "package org.test; rule RINIT when eval(true) then insert(new MyBean()); end";
String INIT_DRL_2 = "package org.test; rule RINIT when eval(1==1) then insert(new MyBean()); end";
String JAVA2 = "package org.test;" + " public class MyBean {\n" + " private String firstName;\n" + " private String lastName;\n" + " public MyBean() { /* empty constructor */ }\n" + " public MyBean(String firstName) { this.firstName = firstName; }\n" + " public MyBean(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }\n" + " public String getFirstName() { return firstName; }\n" + " public void setFirstName(String firstName) { this.firstName = firstName; }\n" + " public String getLastName() { return lastName; }\n" + " public void setLastName(String lastName) { this.lastName = lastName; }\n" + " }";
String DRL2 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + " when\n" + " $b : MyBean( firstName == null , lastName == null )\n" + " then\n" + " System.out.println($b);" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "myTest", "1.0.0");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
kfs.write("src/main/java/org/test/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA1)));
kfs.write(ks.getResources().newReaderResource(new StringReader(DRL1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
kfs.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
int fired = ksession.fireAllRules();
assertEquals(2, fired);
ReleaseId id2 = ks.newReleaseId("org.test", "myTest", "2.0.0");
KieFileSystem kfs2 = ks.newKieFileSystem();
KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
kfs2.generateAndWritePomXML(id2);
kfs2.write("src/main/java/org/test/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA2)));
kfs2.write(ks.getResources().newReaderResource(new StringReader(DRL2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
kfs2.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL_2)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
kieBuilder2.buildAll();
Results updateResults = kc.updateToVersion(id2);
assertFalse(updateResults.hasMessages(Level.ERROR));
fired = ksession.fireAllRules();
assertEquals(2, fired);
}
Aggregations