Search in sources :

Example 1 with CanonicalKiePackages

use of org.drools.modelcompiler.CanonicalKiePackages 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)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 CanonicalKieModule (org.drools.modelcompiler.CanonicalKieModule)1 CanonicalKiePackages (org.drools.modelcompiler.CanonicalKiePackages)1 ResourceChange (org.kie.internal.builder.ResourceChange)1 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)1