private int updateResourcesIncrementally(
      InternalKieModule currentKM,
      InternalKieModule newKM,
      KieJarChangeSet cs,
      List<String> modifiedClasses,
      KieBase kBase,
      KieBaseModel kieBaseModel,
      KnowledgeBuilderImpl kbuilder,
      CompositeKnowledgeBuilder ckbuilder) {
    int fileCount = modifiedClasses.size();
    for (ResourceChangeSet rcs : cs.getChanges().values()) {
      if (rcs.getChangeType() != ChangeType.REMOVED) {
        String resourceName = rcs.getResourceName();
        if (!resourceName.endsWith(".properties")
            && isFileInKBase(newKM, kieBaseModel, resourceName)) {
          List<ResourceChange> changes = rcs.getChanges();
          if (!changes.isEmpty()) {
            // we need to deal with individual parts of the resource
            fileCount +=
                AbstractKieModule.updateResource(ckbuilder, newKM, resourceName, rcs) ? 1 : 0;
          } else {
            // the whole resource has to handled
            if (rcs.getChangeType() == ChangeType.UPDATED) {
              Resource resource = currentKM.getResource(resourceName);
              kbuilder.removeObjectsGeneratedFromResource(resource);
            }
            fileCount += newKM.addResourceToCompiler(ckbuilder, kieBaseModel, resourceName) ? 1 : 0;
          }
        }
      }

      for (ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder()) {
        KnowledgePackageImpl pkg =
            (KnowledgePackageImpl) kBase.getKiePackage(loadOrder.getPkgName());
        if (pkg != null) {
          RuleImpl rule = pkg.getRule(loadOrder.getRuleName());
          if (rule != null) {
            // rule can be null, if it didn't exist before
            rule.setLoadOrder(loadOrder.getLoadOrder());
          }
        }
      }
    }
    return fileCount;
  }
 private List<String> getModifiedClasses(KieJarChangeSet cs) {
   List<String> modifiedClasses = new ArrayList<String>();
   for (ResourceChangeSet rcs : cs.getChanges().values()) {
     if (rcs.getChangeType() != ChangeType.REMOVED) {
       String resourceName = rcs.getResourceName();
       if (resourceName.endsWith(".class")) {
         modifiedClasses.add(resourceName);
       }
     }
   }
   return modifiedClasses;
 }
  private void updateKBase(
      InternalKnowledgeBase kBase,
      InternalKieModule currentKM,
      ReleaseId newReleaseId,
      InternalKieModule newKM,
      KieJarChangeSet cs,
      List<String> modifiedClasses,
      List<String> dslFiles,
      ResultsImpl results,
      KieBaseModel kieBaseModel) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kBase);
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl) kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();

    boolean shouldRebuild =
        applyResourceChanges(
            currentKM, newKM, cs, modifiedClasses, kBase, kieBaseModel, pkgbuilder, ckbuilder);
    // remove resources first
    for (ResourceChangeSet rcs : cs.getChanges().values()) {
      if (rcs.getChangeType() == ChangeType.REMOVED) {
        String resourceName = rcs.getResourceName();
        if (!resourceName.endsWith(".properties")
            && isFileInKBase(newKM, kieBaseModel, resourceName)) {
          pkgbuilder.removeObjectsGeneratedFromResource(currentKM.getResource(resourceName));
        }
      }
    }

    if (shouldRebuild) {
      // readd unchanged dsl files to the kbuilder
      for (String dslFile : dslFiles) {
        if (isFileInKBase(newKM, kieBaseModel, dslFile)) {
          newKM.addResourceToCompiler(ckbuilder, kieBaseModel, dslFile);
        }
      }
      rebuildAll(
          newReleaseId, results, newKM, modifiedClasses, kieBaseModel, pkgbuilder, ckbuilder);
    }

    for (StatefulKnowledgeSession session : kBase.getStatefulKnowledgeSessions()) {
      ((InternalWorkingMemory) session).notifyHalt();
    }
  }
 private ResourceDescr(
     ResourceConfiguration configuration, Resource resource, ResourceChangeSet changes) {
   this.configuration = configuration;
   this.resource = resource;
   this.changes = changes;
   if (changes != null) {
     changeMap = new HashMap<String, ResourceChange>();
     if (!changes.getChanges().isEmpty()) {
       for (ResourceChange c : changes.getChanges()) {
         changeMap.put(assetId(c.getType(), c.getName()), c);
       }
       globalChangeType = null;
     } else {
       globalChangeType = changes.getChangeType();
     }
   } else {
     changeMap = null;
     globalChangeType = null;
   }
 }