use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.
the class CanonicalKieBaseUpdater method run.
@Override
public void run() {
CanonicalKieModule oldKM = (CanonicalKieModule) ctx.currentKM;
CanonicalKieModule newKM = (CanonicalKieModule) ctx.newKM;
List<RuleImpl> rulesToBeRemoved;
List<RuleImpl> rulesToBeAdded;
if (ctx.modifyingUsedClass) {
// remove all ObjectTypeNodes for the modified classes
for (Class<?> cls : ctx.modifiedClasses) {
clearInstancesOfModifiedClass(cls);
}
rulesToBeRemoved = getAllRulesInKieBase(oldKM, ctx.currentKieBaseModel);
rulesToBeAdded = getAllRulesInKieBase(newKM, ctx.newKieBaseModel);
} else {
newKM.setModuleClassLoader(((CanonicalKieModule) ctx.currentKM).getModuleClassLoader());
CanonicalKiePackages newPkgs = newKM.getKiePackages(ctx.newKieBaseModel);
rulesToBeRemoved = new ArrayList<>();
rulesToBeAdded = new ArrayList<>();
for (ResourceChangeSet changeSet : ctx.cs.getChanges().values()) {
if (!isPackageInKieBase(ctx.newKieBaseModel, changeSet.getResourceName())) {
continue;
}
InternalKnowledgePackage oldKpkg = ctx.kBase.getPackage(changeSet.getResourceName());
InternalKnowledgePackage kpkg = (InternalKnowledgePackage) newPkgs.getKiePackage(changeSet.getResourceName());
for (ResourceChange change : changeSet.getChanges()) {
String changedItemName = change.getName();
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.REMOVED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
ctx.kBase.removeGlobal(changedItemName);
} else {
rulesToBeRemoved.add(oldKpkg.getRule(changedItemName));
}
}
if (change.getChangeType() == ChangeType.UPDATED || change.getChangeType() == ChangeType.ADDED) {
if (change.getType() == ResourceChange.Type.GLOBAL) {
try {
ctx.kBase.addGlobal(changedItemName, kpkg.getTypeResolver().resolveType(kpkg.getGlobals().get(changedItemName)));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else {
rulesToBeAdded.add(kpkg.getRule(changedItemName));
}
}
}
}
}
ctx.kBase.removeRules(rulesToBeRemoved);
ctx.kBase.addRules(rulesToBeAdded);
}
use of org.kie.internal.builder.ResourceChangeSet in project drools by kiegroup.
the class ChangeSetBuilder method diffResource.
private static ResourceChangeSet diffResource(String file, byte[] ob, byte[] cb) {
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 opkg = new DrlParser().parse(new ByteArrayResource(ob));
PackageDescr cpkg = new DrlParser().parse(new ByteArrayResource(cb));
String pkgName = isEmpty(cpkg.getName()) ? getDefaultPackageName() : cpkg.getName();
String oldPkgName = isEmpty(opkg.getName()) ? getDefaultPackageName() : opkg.getName();
if (!oldPkgName.equals(pkgName)) {
// so it is useless to further investigate other changes
return pkgcs;
}
for (RuleDescr crd : cpkg.getRules()) {
pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
}
// needs to be cloned
List<RuleDescr> orules = new ArrayList<>(opkg.getRules());
diffDescrs(ob, cb, pkgcs, orules, cpkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
// needs to be cloned
List<FunctionDescr> ofuncs = new ArrayList<>(opkg.getFunctions());
diffDescrs(ob, cb, pkgcs, ofuncs, cpkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
// needs to be cloned
List<GlobalDescr> oglobals = new ArrayList<>(opkg.getGlobals());
diffDescrs(ob, cb, pkgcs, oglobals, cpkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
} 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 CanonicalKieModule method findChanges.
private void findChanges(KieJarChangeSet result, InternalKieModule newKieModule) {
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
byte[] oldBytes = getBytes(file);
byte[] newBytes = newKieModule.getBytes(file);
if (!Arrays.equals(oldBytes, newBytes)) {
// 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);
}
}
}
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 = new KieJarChangeSet();
findChanges(result, 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.removeFile(entry.getKey());
continue;
}
Model oldModel = entry.getValue();
ResourceChangeSet changeSet = calculateResourceChangeSet(oldModel, newModel);
if (!changeSet.getChanges().isEmpty()) {
result.registerChanges(entry.getKey(), changeSet);
}
}
return result;
}
use of org.kie.internal.builder.ResourceChangeSet 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") ) );
}
Aggregations