private KieBaseConfiguration getKnowledgeBaseConfiguration(
     KieBaseModelImpl kBaseModel, ClassLoader cl) {
   KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, cl);
   kbConf.setOption(kBaseModel.getEqualsBehavior());
   kbConf.setOption(kBaseModel.getEventProcessingMode());
   kbConf.setOption(kBaseModel.getDeclarativeAgenda());
   return kbConf;
 }
  private KieBase createKieBase(
      KieBaseModelImpl kBaseModel,
      KieProject kieProject,
      ResultsImpl messages,
      KieBaseConfiguration conf) {
    ClassLoader cl = kieProject.getClassLoader();
    InternalKieModule kModule = kieProject.getKieModuleForKBase(kBaseModel.getName());

    Collection<KnowledgePackage> pkgs =
        kModule.getKnowledgePackagesForKieBase(kBaseModel.getName());

    if (pkgs == null) {
      KnowledgeBuilder kbuilder = buildKnowledgePackages(kBaseModel, kieProject, messages);
      if (kbuilder.hasErrors()) {
        // Messages already populated by the buildKnowlegePackages
        return null;
      }
    }

    // if we get to here, then we know the pkgs is now cached
    pkgs = kModule.getKnowledgePackagesForKieBase(kBaseModel.getName());

    if (kBaseModel.getEventProcessingMode() == EventProcessingOption.CLOUD
        && (conf == null
            || conf.getOption(EventProcessingOption.class) == EventProcessingOption.CLOUD)) {
      for (KnowledgePackage kpkg : pkgs) {
        if (((KnowledgePackageImpl) kpkg).needsStreamMode()) {
          throw new RuntimeException(
              "The requested KieBase \""
                  + kBaseModel.getName()
                  + "\" has been set to run in CLOUD mode but requires features only available in STREAM mode");
        }
      }
    }

    if (conf == null) {
      conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
    } else if (conf instanceof RuleBaseConfiguration) {
      ((RuleBaseConfiguration) conf).setClassLoader(cl);
    }
    InternalKnowledgeBase kBase =
        (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(conf);

    kBase.addKnowledgePackages(pkgs);
    return kBase;
  }
  private void addKieSessionModels(
      ConfigurableListableBeanFactory beanFactory, KieBaseModelImpl kBase) {
    for (String beanDef : beanFactory.getBeanDefinitionNames()) {
      BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDef);
      if (beanDefinition.getBeanClassName().equalsIgnoreCase(KSessionFactoryBean.class.getName())) {
        String kBaseName = getPropertyValue(beanDefinition, "kBaseName");
        if (kBase.getName().equalsIgnoreCase(kBaseName)) {
          String name = getPropertyValue(beanDefinition, "name");
          String type = getPropertyValue(beanDefinition, "type");
          KieSessionModelImpl kSession = new KieSessionModelImpl(kBase, name);

          kSession.setType(
              !type.isEmpty()
                  ? KieSessionModel.KieSessionType.valueOf(type.toUpperCase())
                  : KieSessionModel.KieSessionType.STATEFUL);
          Map<String, KieSessionModel> rawKieSessionModels = kBase.getRawKieSessionModels();
          rawKieSessionModels.put(kSession.getName(), kSession);
          beanDefinition
              .getPropertyValues()
              .addPropertyValue(new PropertyValue("releaseId", releaseId));

          kSession.setDefault("true".equals(getPropertyValue(beanDefinition, "def")));

          String clockType = getPropertyValue(beanDefinition, "clockType");
          if (!clockType.isEmpty()) {
            kSession.setClockType(ClockTypeOption.get(clockType));
          }

          String scope = getPropertyValue(beanDefinition, "scope");
          if (!scope.isEmpty()) {
            kSession.setScope(scope.trim());
          }
        }
      }
    }
  }
  protected void indexParts(
      Collection<InternalKieModule> kieModules, Map<String, InternalKieModule> kJarFromKBaseName) {
    for (InternalKieModule kJar : kieModules) {
      KieModuleModel kieProject = kJar.getKieModuleModel();
      for (KieBaseModel kieBaseModel : kieProject.getKieBaseModels().values()) {
        if (kieBaseModel.isDefault()) {
          if (defaultKieBase == null) {
            defaultKieBase = kieBaseModel;
          } else {
            defaultKieBase = null;
            log.warn(
                "Found more than one default KieBase: disabling all. KieBases will be accessible only by name");
          }
        }

        kBaseModels.put(kieBaseModel.getName(), kieBaseModel);
        ((KieBaseModelImpl) kieBaseModel)
            .setKModule(kieProject); // should already be set, but just in case

        kJarFromKBaseName.put(kieBaseModel.getName(), kJar);
        for (KieSessionModel kieSessionModel : kieBaseModel.getKieSessionModels().values()) {
          if (kieSessionModel.isDefault()) {
            if (kieSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
              if (defaultKieSession == null) {
                defaultKieSession = kieSessionModel;
              } else {
                defaultKieSession = null;
                log.warn(
                    "Found more than one default KieSession: disabling all. KieSessions will be accessible only by name");
              }
            } else {
              if (defaultStatelessKieSession == null) {
                defaultStatelessKieSession = kieSessionModel;
              } else {
                defaultStatelessKieSession = null;
                log.warn(
                    "Found more than one default StatelessKieSession: disabling all. StatelessKieSessions will be accessible only by name");
              }
            }
          }

          ((KieSessionModelImpl) kieSessionModel)
              .setKBase(kieBaseModel); // should already be set, but just in case
          kSessionModels.put(kieSessionModel.getName(), kieSessionModel);
        }
      }
    }
  }
  private void addKieBaseModels(
      ConfigurableListableBeanFactory beanFactory, KieModuleModelImpl kieModuleModel) {
    for (String beanDef : beanFactory.getBeanDefinitionNames()) {
      BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanDef);
      if (beanDefinition.getBeanClassName().equalsIgnoreCase(KBaseFactoryBean.class.getName())) {
        KieBaseModelImpl kBase = new KieBaseModelImpl();
        kBase.setKModule(kieModuleModel);

        kBase.setName(getPropertyValue(beanDefinition, "kBaseName"));
        kBase.setDefault("true".equals(getPropertyValue(beanDefinition, "def")));

        String packages = getPropertyValue(beanDefinition, "packages");
        if (!packages.isEmpty()) {
          for (String pkg : packages.split(",")) {
            kBase.addPackage(pkg.trim());
          }
        }

        String includes = getPropertyValue(beanDefinition, "includes");
        if (!includes.isEmpty()) {
          for (String include : includes.split(",")) {
            kBase.addInclude(include.trim());
          }
        }

        String eventMode = getPropertyValue(beanDefinition, "eventProcessingMode");
        if (!eventMode.isEmpty()) {
          kBase.setEventProcessingMode(
              EventProcessingOption.determineEventProcessingMode(eventMode));
        }

        String equalsBehavior = getPropertyValue(beanDefinition, "equalsBehavior");
        if (!equalsBehavior.isEmpty()) {
          kBase.setEqualsBehavior(EqualityBehaviorOption.determineEqualityBehavior(equalsBehavior));
        }

        String declarativeAgenda = getPropertyValue(beanDefinition, "declarativeAgenda");
        if (!declarativeAgenda.isEmpty()) {
          kBase.setDeclarativeAgenda(
              DeclarativeAgendaOption.determineDeclarativeAgenda(declarativeAgenda));
        }

        String scope = getPropertyValue(beanDefinition, "scope");
        if (!scope.isEmpty()) {
          kBase.setScope(scope.trim());
        }

        kieModuleModel.getRawKieBaseModels().put(kBase.getName(), kBase);
        beanDefinition
            .getPropertyValues()
            .addPropertyValue(new PropertyValue("releaseId", releaseId));
        addKieSessionModels(beanFactory, kBase);
      }
    }
  }