Search in sources :

Example 6 with ResourceChangeSet

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);
}
Also used : CanonicalKiePackages(org.drools.modelcompiler.CanonicalKiePackages) CanonicalKieModule(org.drools.modelcompiler.CanonicalKieModule) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 7 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[] 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;
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Arrays(java.util.Arrays) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ResourceChange(org.kie.internal.builder.ResourceChange) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceType(org.kie.api.io.ResourceType) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Collection(java.util.Collection) Set(java.util.Set) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) List(java.util.List) DrlParser(org.drools.compiler.compiler.DrlParser) Comparator(java.util.Comparator) StringUtils(org.drools.core.util.StringUtils) 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.compiler.lang.descr.FunctionDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Example 8 with ResourceChangeSet

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

Example 9 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 = 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;
}
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.appformer.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) HashMap(java.util.HashMap)

Example 10 with ResourceChangeSet

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") ) );
}
Also used : ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Aggregations

ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)11 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)5 Test (org.junit.Test)4 ResourceChange (org.kie.internal.builder.ResourceChange)4 ArrayList (java.util.ArrayList)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 PomModel (org.appformer.maven.support.PomModel)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 KieJarChangeSet (org.drools.compiler.kie.util.KieJarChangeSet)1