use of org.drools.compiler.kie.builder.impl.InternalKieModule in project graylog2-server by Graylog2.
the class DroolsEngine method createKJar.
private byte[] createKJar(KieServices ks, ReleaseId releaseId, String pom, String... drls) throws RulesCompilationException {
KieFileSystem kfs = ks.newKieFileSystem();
if (pom != null) {
kfs.write("pom.xml", pom);
} else {
kfs.generateAndWritePomXML(releaseId);
}
for (int i = 0; i < drls.length; i++) {
if (drls[i] != null) {
kfs.write("src/main/resources/r" + i + ".drl", drls[i]);
}
}
for (URI builtinRuleUrl : builtinRuleUrls) {
final String rulesFileName = FilenameUtils.getName(builtinRuleUrl.getPath());
final String path = "src/main/resources/" + rulesFileName;
final URL url;
try {
url = builtinRuleUrl.toURL();
} catch (MalformedURLException e) {
throw new RulesCompilationException(Collections.<org.kie.api.builder.Message>emptyList());
}
final Resource resource = ResourceFactory.newUrlResource(url).setSourcePath(path).setResourceType(ResourceType.DRL);
kfs.write(resource);
}
final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
if (kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RulesCompilationException(kb.getResults().getMessages());
}
final InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
return kieModule.getBytes();
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class ChangeSetBuilderTest method testNoChanges.
@Test
public void testNoChanges() {
String drl1 = "package org.drools\n" + "rule R1 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools\n" + "rule R2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
InternalKieModule kieJar1 = createKieJar(drl1, drl2);
InternalKieModule kieJar2 = createKieJar(drl1, drl2);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
assertThat(changes.getChanges().size(), is(0));
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class ChangeSetBuilderTest method testRemoved.
@Test
public void testRemoved() {
String drl1 = "package org.drools\n" + "rule R1 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools\n" + "rule R2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
InternalKieModule kieJar1 = createKieJar(drl1, drl2);
InternalKieModule kieJar2 = createKieJar(drl1);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
String removedFile = (String) kieJar1.getFileNames().toArray()[1];
assertThat(changes.getChanges().size(), is(1));
ResourceChangeSet cs = changes.getChanges().get(removedFile);
assertThat(cs, not(nullValue()));
assertThat(cs.getChangeType(), is(ChangeType.REMOVED));
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class ChangeSetBuilderTest 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";
InternalKieModule kieJar1 = createKieJar(drl1 + drl2 + drl3);
InternalKieModule kieJar2 = createKieJar(drl1 + drl3);
KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
assertEquals(1, changes.getChanges().size());
ResourceChangeSet rcs = changes.getChanges().values().iterator().next();
assertEquals(1, rcs.getChanges().size());
assertEquals(ChangeType.REMOVED, rcs.getChanges().get(0).getChangeType());
}
use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.
the class KieCompilationCacheTest method testCacheWigAccumulate.
@Test
public void testCacheWigAccumulate() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "rule R11 when\n" + " Number() from accumulate(String(), \n" + " init(int x = 0;)," + " action(x++;)," + " reverse(x--;)," + " result(x))\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "cache-accumulate", "1.0-SNAPSHOT");
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
ks.newKieBuilder(kfs).buildAll();
InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
byte[] jar = kieModule.getBytes();
MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
assertNotNull(file);
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new String("Hello World"));
assertEquals(1, ksession.fireAllRules());
}
Aggregations