Search in sources :

Example 1 with ResourceChangeSet

use of org.kie.internal.builder.ResourceChangeSet 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 2 with ResourceChangeSet

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

use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.

the class KieBaseUpdater method updateResourcesIncrementally.

private int updateResourcesIncrementally(KnowledgeBuilderImpl kbuilder, CompositeKnowledgeBuilder ckbuilder) {
    int fileCount = ctx.modifiedClasses.size();
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() != ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.newKM, ctx.newKieBaseModel, resourceName)) {
                List<ResourceChange> changes = rcs.getChanges();
                if (!changes.isEmpty()) {
                    // we need to deal with individual parts of the resource
                    fileCount += AbstractKieModule.updateResource(ckbuilder, ctx.newKM, resourceName, rcs) ? 1 : 0;
                } else {
                    // the whole resource has to handled
                    if (rcs.getChangeType() == ChangeType.UPDATED) {
                        Resource resource = ctx.currentKM.getResource(resourceName);
                        kbuilder.removeObjectsGeneratedFromResource(resource);
                    }
                    fileCount += ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, resourceName, rcs) ? 1 : 0;
                }
            }
        }
        for (ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) ctx.kBase.getKiePackage(loadOrder.getPkgName());
            if (pkg != null) {
                RuleImpl rule = pkg.getRule(loadOrder.getRuleName());
                if (rule != null) {
                    // rule can be null, if it didn't exist before
                    rule.setLoadOrder(loadOrder.getLoadOrder());
                }
            }
        }
    }
    return fileCount;
}
Also used : Resource(org.kie.api.io.Resource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint)

Example 4 with ResourceChangeSet

use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.

the class KieBaseUpdater method run.

@Override
public void run() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(ctx.kBase, ctx.newKM.getBuilderConfiguration(ctx.newKieBaseModel));
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl) kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    boolean shouldRebuild = applyResourceChanges(pkgbuilder, ckbuilder);
    // remove resources first
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() == ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.currentKM, ctx.currentKieBaseModel, resourceName)) {
                pkgbuilder.removeObjectsGeneratedFromResource(ctx.currentKM.getResource(resourceName));
            }
        }
    }
    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
    }
    if (shouldRebuild) {
        // readd unchanged dsl files to the kbuilder
        for (String dslFile : ctx.unchangedResources) {
            if (isFileInKBase(ctx.newKM, ctx.newKieBaseModel, dslFile)) {
                ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, dslFile);
            }
        }
        rebuildAll(pkgbuilder, ckbuilder);
    }
    ctx.kBase.setResolvedReleaseId(ctx.newReleaseId);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet)

Example 5 with ResourceChangeSet

use of org.kie.internal.builder.ResourceChangeSet 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 : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)20 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)10 Test (org.junit.Test)8 ResourceChange (org.kie.internal.builder.ResourceChange)8 ArrayList (java.util.ArrayList)6 KieJarChangeSet (org.drools.compiler.kie.util.KieJarChangeSet)6 HashMap (java.util.HashMap)5 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 List (java.util.List)3 Map (java.util.Map)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 ChangeType (org.kie.internal.builder.ChangeType)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2