Search in sources :

Example 1 with InternalKieModule

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.graylog2.plugin.Message) Resource(org.kie.api.io.Resource) URI(java.net.URI) URL(java.net.URL) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 2 with InternalKieModule

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));
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 3 with InternalKieModule

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));
}
Also used : ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 4 with InternalKieModule

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());
}
Also used : ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 5 with InternalKieModule

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());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)201 ReleaseId (org.kie.api.builder.ReleaseId)125 KieServices (org.kie.api.KieServices)120 Test (org.junit.Test)86 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)72 File (java.io.File)62 ArrayList (java.util.ArrayList)57 KieContainer (org.kie.api.runtime.KieContainer)57 KieBuilder (org.kie.api.builder.KieBuilder)55 KieFileSystem (org.kie.api.builder.KieFileSystem)54 FileOutputStream (java.io.FileOutputStream)53 KieMavenRepository (org.kie.scanner.KieMavenRepository)51 KieSession (org.kie.api.runtime.KieSession)43 HashMap (java.util.HashMap)28 Before (org.junit.Before)28 KieModule (org.kie.api.builder.KieModule)24 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)23 KieScanner (org.kie.api.builder.KieScanner)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)17