Search in sources :

Example 16 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.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());
}
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)

Example 17 with ResourceChangeSet

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;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Arrays(java.util.Arrays) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) ResourceChange(org.kie.internal.builder.ResourceChange) RuleDescr(org.drools.drl.ast.descr.RuleDescr) TypeResolver(org.drools.core.addon.TypeResolver) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DrlParser(org.drools.drl.parser.DrlParser) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Logger(org.slf4j.Logger) ClassUtils.convertClassToResourcePath(org.drools.core.util.ClassUtils.convertClassToResourcePath) Iterator(java.util.Iterator) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Collection(java.util.Collection) Set(java.util.Set) BaseDescr(org.drools.drl.ast.descr.BaseDescr) List(java.util.List) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DEFAULT_PACKAGE(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl.DEFAULT_PACKAGE) Comparator(java.util.Comparator) ClassUtils.convertResourceToClassName(org.drools.core.util.ClassUtils.convertResourceToClassName) StringUtils(org.drools.core.util.StringUtils) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) ArrayList(java.util.ArrayList) ResourceType(org.kie.api.io.ResourceType) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 18 with ResourceChangeSet

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

Example 19 with ResourceChangeSet

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;
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ArrayList(java.util.ArrayList) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet)

Example 20 with ResourceChangeSet

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);
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ModelComponent.areEqualInModel(org.drools.model.impl.ModelComponent.areEqualInModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) PomModel(org.kie.util.maven.support.PomModel) Model(org.drools.model.Model) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

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