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.mvel.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());
}
use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.
the class ChangeSetBuilder method diffResource.
private static ResourceChangeSet diffResource(String file, byte[] originalBytes, byte[] currentBytes, List<TypeDeclarationDescr> typeDeclarations) {
ResourceChangeSet pkgcs = new ResourceChangeSet(file, ChangeType.UPDATED);
ResourceType type = ResourceType.determineResourceType(file);
if (ResourceType.DRL.equals(type) || ResourceType.GDRL.equals(type) || ResourceType.RDRL.equals(type) || ResourceType.TDRL.equals(type)) {
try {
PackageDescr originalPkg = new DrlParser().parse(new ByteArrayResource(originalBytes));
PackageDescr currentPkg = new DrlParser().parse(new ByteArrayResource(currentBytes));
String pkgName = isEmpty(currentPkg.getName()) ? getDefaultPackageName() : currentPkg.getName();
String oldPkgName = isEmpty(originalPkg.getName()) ? getDefaultPackageName() : originalPkg.getName();
if (!oldPkgName.equals(pkgName)) {
// so it is useless to further investigate other changes
return pkgcs;
}
pkgcs.setPackageName(pkgName);
for (RuleDescr crd : currentPkg.getRules()) {
pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
}
// needs to be cloned
List<RuleDescr> orules = new ArrayList<>(originalPkg.getRules());
diffDescrs(originalBytes, currentBytes, pkgcs, orules, currentPkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
// needs to be cloned
List<FunctionDescr> ofuncs = new ArrayList<>(originalPkg.getFunctions());
diffDescrs(originalBytes, currentBytes, pkgcs, ofuncs, currentPkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
// needs to be cloned
List<GlobalDescr> oglobals = new ArrayList<>(originalPkg.getGlobals());
diffDescrs(originalBytes, currentBytes, pkgcs, oglobals, currentPkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
for (TypeDeclarationDescr typeDeclaration : currentPkg.getTypeDeclarations()) {
if (isEmpty(typeDeclaration.getNamespace())) {
typeDeclaration.setNamespace(isEmpty(currentPkg.getNamespace()) ? DEFAULT_PACKAGE : currentPkg.getNamespace());
}
typeDeclarations.add(typeDeclaration);
}
} catch (Exception e) {
logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
}
}
pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
return pkgcs;
}
use of org.kie.internal.builder.ResourceChangeSet 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;
}
use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.
the class CanonicalKieModule method findChanges.
private KieJarChangeSet findChanges(InternalKieModule newKieModule) {
KieJarChangeSet result = new KieJarChangeSet();
Collection<String> oldFiles = getFileNames();
Collection<String> newFiles = newKieModule.getFileNames();
ArrayList<String> removedFiles = new ArrayList<>(oldFiles);
removedFiles.removeAll(newFiles);
if (!removedFiles.isEmpty()) {
for (String file : removedFiles) {
if (isChange(file, this)) {
result.removeFile(file);
}
}
}
for (String file : newFiles) {
if (oldFiles.contains(file) && isChange(file, this)) {
// check for modification
if (isClassChanged(newKieModule, file)) {
// parse the file to figure out the difference
result.registerChanges(file, new ResourceChangeSet(file, ChangeType.UPDATED));
}
} else if (isChange(file, ((CanonicalKieModule) newKieModule))) {
// file was added
result.addFile(file);
}
}
return result;
}
use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.
the class CanonicalKieModule method getChanges.
@Override
public KieJarChangeSet getChanges(InternalKieModule newKieModule) {
KieJarChangeSet result = findChanges(newKieModule);
Map<String, Model> oldModels = getModels();
Map<String, Model> newModels = ((CanonicalKieModule) newKieModule).getModels();
for (Map.Entry<String, Model> entry : oldModels.entrySet()) {
Model newModel = newModels.get(entry.getKey());
if (newModel == null) {
result.registerChanges(entry.getKey(), buildAllItemsChangeSet(entry.getValue(), ChangeType.REMOVED));
continue;
}
Model oldModel = entry.getValue();
for (ResourceChangeSet changeSet : calculateResourceChangeSet(oldModel, newModel)) {
if (!changeSet.getChanges().isEmpty()) {
result.registerChanges(entry.getKey(), changeSet);
}
}
}
for (Map.Entry<String, Model> entry : newModels.entrySet()) {
if (oldModels.get(entry.getKey()) == null) {
result.registerChanges(entry.getKey(), buildAllItemsChangeSet(entry.getValue(), ChangeType.ADDED));
}
}
KieJarChangeSet internalChanges = internalKieModule.getChanges(((CanonicalKieModule) newKieModule).internalKieModule);
internalChanges.removeFile(getProjectModelResourceName());
return result.merge(internalChanges);
}
Aggregations