Search in sources :

Example 6 with ResourceChange

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

the class CanonicalKieModule method addModifiedItemsToChangeSet.

private void addModifiedItemsToChangeSet(ResourceChangeSet changeSet, ResourceChange.Type type, List<? extends NamedModelItem> oldItems, List<? extends NamedModelItem> newItems) {
    if (oldItems.isEmpty()) {
        if (!newItems.isEmpty()) {
            for (NamedModelItem newItem : newItems) {
                changeSet.getChanges().add(new ResourceChange(ChangeType.ADDED, type, newItem.getName()));
            }
        }
        return;
    } else if (newItems.isEmpty()) {
        for (NamedModelItem oldItem : oldItems) {
            changeSet.getChanges().add(new ResourceChange(ChangeType.REMOVED, type, oldItem.getName()));
        }
        return;
    }
    oldItems.sort(Comparator.comparing(NamedModelItem::getName));
    newItems.sort(Comparator.comparing(NamedModelItem::getName));
    Iterator<? extends NamedModelItem> oldRulesIterator = oldItems.iterator();
    Iterator<? extends NamedModelItem> newRulesIterator = newItems.iterator();
    NamedModelItem currentOld = oldRulesIterator.next();
    NamedModelItem currentNew = newRulesIterator.next();
    while (true) {
        int compare = currentOld.getName().compareTo(currentNew.getName());
        if (compare == 0) {
            if (!areEqualInModel(currentOld, currentNew)) {
                changeSet.getChanges().add(new ResourceChange(ChangeType.UPDATED, type, currentOld.getName()));
            }
            if (oldRulesIterator.hasNext()) {
                currentOld = oldRulesIterator.next();
            } else {
                break;
            }
            if (newRulesIterator.hasNext()) {
                currentNew = newRulesIterator.next();
            } else {
                break;
            }
        } else if (compare < 0) {
            changeSet.getChanges().add(new ResourceChange(ChangeType.REMOVED, type, currentOld.getName()));
            if (oldRulesIterator.hasNext()) {
                currentOld = oldRulesIterator.next();
            } else {
                break;
            }
        } else {
            changeSet.getChanges().add(new ResourceChange(ChangeType.ADDED, type, currentNew.getName()));
            if (newRulesIterator.hasNext()) {
                currentNew = newRulesIterator.next();
            } else {
                break;
            }
        }
    }
    while (oldRulesIterator.hasNext()) {
        changeSet.getChanges().add(new ResourceChange(ChangeType.REMOVED, type, oldRulesIterator.next().getName()));
    }
    while (newRulesIterator.hasNext()) {
        changeSet.getChanges().add(new ResourceChange(ChangeType.ADDED, type, newRulesIterator.next().getName()));
    }
}
Also used : ResourceChange(org.kie.internal.builder.ResourceChange) NamedModelItem(org.drools.model.NamedModelItem)

Example 7 with ResourceChange

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

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

the class KieBaseUpdaterImpl method createKieBaseUpdate.

private KieBaseUpdate createKieBaseUpdate() {
    KieBaseUpdate kieBaseUpdate = new KieBaseUpdate();
    for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
        if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getPackageName())) {
            continue;
        }
        InternalKnowledgePackage currentPkg = ctx.currentKM.getPackage(changeSet.getPackageName());
        InternalKnowledgePackage newPkg = ctx.newKM.getPackage(changeSet.getPackageName());
        for (ResourceChange change : changeSet.getChanges()) {
            if (change.getType() == ResourceChange.Type.RULE) {
                switch(change.getChangeType()) {
                    case ADDED:
                        kieBaseUpdate.registerRuleToBeAdded(newPkg.getRule(change.getName()));
                        break;
                    case REMOVED:
                        kieBaseUpdate.registerRuleToBeRemoved(currentPkg.getRule(change.getName()));
                        break;
                    case UPDATED:
                        kieBaseUpdate.registerRuleToBeAdded(newPkg.getRule(change.getName()));
                        kieBaseUpdate.registerRuleToBeRemoved(currentPkg.getRule(change.getName()));
                        break;
                }
            }
        }
    }
    return kieBaseUpdate;
}
Also used : KieBaseUpdate(org.drools.core.impl.KieBaseUpdate) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

ResourceChange (org.kie.internal.builder.ResourceChange)8 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)2 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 KieBaseUpdate (org.drools.core.impl.KieBaseUpdate)2 Test (org.junit.Test)2 Resource (org.kie.api.io.Resource)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InternalKnowledgeBuilder (org.drools.compiler.builder.InternalKnowledgeBuilder)1 CompositeKnowledgeBuilderImpl (org.drools.compiler.builder.impl.CompositeKnowledgeBuilderImpl)1 KieBaseUpdaterImpl (org.drools.compiler.kie.builder.impl.KieBaseUpdaterImpl)1