Search in sources :

Example 56 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.

the class ChangeSetBuilderTest method testModified2.

@Test
public void testModified2() {
    String drl1 = "package org.drools\n" + "rule \"Rule 1\" when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"An updated rule\" when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"A removed rule\" when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    String drl1_5 = "package org.drools\n" + "rule \"Rule 1\" when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule \"An updated rule\" when\n" + "   $m : Message( message == \"Good Bye World\" )\n" + "then\n" + "end\n" + "rule \"An added rule\" when\n" + "   $m : Message( message == \"Good Bye World\" )\n" + "then\n" + "end\n";
    String drl2 = "package org.drools\n" + "rule \"This is the name of rule 3\" when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    String drl3 = "package org.drools\n" + "rule \"Another dumb rule\" when\n" + "   $m : Message( message == \"Good bye World\" )\n" + "then\n" + "end\n";
    InternalKieModule kieJar1 = createKieJar(drl1, drl2);
    InternalKieModule kieJar2 = createKieJar(drl1_5, null, drl3);
    KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
    // System.out.println( builder.toProperties( changes ) );
    String modifiedFile = (String) kieJar2.getFileNames().toArray()[0];
    String addedFile = (String) kieJar2.getFileNames().toArray()[1];
    String removedFile = (String) kieJar1.getFileNames().toArray()[1];
    assertThat(changes.getChanges().size(), is(3));
    ResourceChangeSet cs = changes.getChanges().get(removedFile);
    assertThat(cs, not(nullValue()));
    assertThat(cs.getChangeType(), is(ChangeType.REMOVED));
    assertThat(cs.getChanges().size(), is(0));
    cs = changes.getChanges().get(addedFile);
    assertThat(cs, not(nullValue()));
    assertThat(cs.getChangeType(), is(ChangeType.ADDED));
    assertThat(cs.getChanges().size(), is(0));
    cs = changes.getChanges().get(modifiedFile);
    assertThat(cs, not(nullValue()));
    assertThat(cs.getChangeType(), is(ChangeType.UPDATED));
// assertThat( cs.getChanges().size(), is(3) );
// assertThat( cs.getChanges().get( 0 ), is( new ResourceChange(ChangeType.ADDED, Type.RULE, "An added rule") ) );
// assertThat( cs.getChanges().get( 1 ), is( new ResourceChange(ChangeType.REMOVED, Type.RULE, "A removed rule") ) );
// assertThat( cs.getChanges().get( 2 ), is( new ResourceChange(ChangeType.UPDATED, Type.RULE, "An updated rule") ) );
}
Also used : ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 57 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.

the class ChangeSetBuilderTest method createKieJar.

private InternalKieModule createKieJar(String... drls) {
    InternalKieModule kieJar = mock(InternalKieModule.class);
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
    List<String> drlFs = new ArrayList<String>();
    for (int i = 0; i < drls.length; i++) {
        if (drls[i] != null) {
            String fileName = "src/main/resources/org/pkg1/drlFile" + i + ".drl";
            drlFs.add(fileName);
            when(kieJar.getBytes(fileName)).thenReturn(drls[i].getBytes());
        }
    }
    when(kieJar.getBytes(KieModuleModelImpl.KMODULE_JAR_PATH)).thenReturn(createKieProjectWithPackages(ks, releaseId).toXML().getBytes());
    when(kieJar.getFileNames()).thenReturn(drlFs);
    return (InternalKieModule) kieJar;
}
Also used : ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 58 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.

the class ChangeSetBuilderTest method testModified.

@Test
public void testModified() {
    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";
    String drl3 = "package org.drools\n" + "rule R3 when\n" + "   $m : Message( message == \"Good bye World\" )\n" + "then\n" + "end\n";
    InternalKieModule kieJar1 = createKieJar(drl1, drl2);
    InternalKieModule kieJar2 = createKieJar(drl1, drl3);
    KieJarChangeSet changes = ChangeSetBuilder.build(kieJar1, kieJar2);
    String modifiedFile = (String) kieJar2.getFileNames().toArray()[1];
    assertThat(changes.getChanges().size(), is(1));
    ResourceChangeSet cs = changes.getChanges().get(modifiedFile);
    assertThat(cs, not(nullValue()));
    assertThat(cs.getChangeType(), is(ChangeType.UPDATED));
    assertThat(cs.getChanges().size(), is(2));
    assertThat(cs.getChanges().get(1), is(new ResourceChange(ChangeType.ADDED, Type.RULE, "R3")));
    assertThat(cs.getChanges().get(0), is(new ResourceChange(ChangeType.REMOVED, Type.RULE, "R2")));
// ChangeSetBuilder builder = new ChangeSetBuilder();
// System.out.println( builder.toProperties( changes ) );
}
Also used : ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 59 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.

the class KieHelper method createJar.

public static byte[] createJar(KieServices ks, ReleaseId releaseId, Resource... resources) {
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId);
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] != null) {
            kfs.write(resources[i]);
        }
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    ((InternalKieBuilder) kieBuilder).buildAll(o -> true);
    Results results = kieBuilder.getResults();
    if (results.hasMessages(Message.Level.ERROR)) {
        throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString());
    }
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    return jar;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) Results(org.kie.api.builder.Results) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 60 with InternalKieModule

use of org.drools.compiler.kie.builder.impl.InternalKieModule in project drools by kiegroup.

the class AbstractKieCiTest method createKieJar.

protected InternalKieModule createKieJar(KieServices ks, ReleaseId releaseId, String pomXml, boolean isdefault, String... rules) throws IOException {
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, isdefault);
    kfs.writePomXML(pomXml);
    for (String rule : rules) {
        String file = "org/test/" + rule + ".drl";
        kfs.write("src/main/resources/KBase1/" + file, createDRL(rule));
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
    return (InternalKieModule) kieBuilder.getKieModule();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Aggregations

InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)88 ReleaseId (org.kie.api.builder.ReleaseId)56 KieServices (org.kie.api.KieServices)51 Test (org.junit.Test)45 KieContainer (org.kie.api.runtime.KieContainer)40 KieFileSystem (org.kie.api.builder.KieFileSystem)31 KieSession (org.kie.api.runtime.KieSession)30 KieBuilder (org.kie.api.builder.KieBuilder)28 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)28 KieScanner (org.kie.api.builder.KieScanner)19 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 File (java.io.File)14 FileOutputStream (java.io.FileOutputStream)9 IOException (java.io.IOException)9 KieModule (org.kie.api.builder.KieModule)8 ArrayList (java.util.ArrayList)7 Resource (org.kie.api.io.Resource)7 KieRepositoryImpl (org.drools.compiler.kie.builder.impl.KieRepositoryImpl)6 KieServicesImpl (org.drools.compiler.kie.builder.impl.KieServicesImpl)5 Ignore (org.junit.Ignore)4