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()));
}
}
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 ) );
}
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;
}
Aggregations