private KieSessionConfiguration getKnowledgeSessionConfiguration(
     KieSessionModelImpl kSessionModel) {
   KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
   ksConf.setOption(kSessionModel.getClockType());
   ksConf.setOption(kSessionModel.getBeliefSystem());
   return ksConf;
 }
  public StatelessKieSession newStatelessKieSession(
      String kSessionName, KieSessionConfiguration conf) {
    KieSessionModelImpl kSessionModel =
        (KieSessionModelImpl) kProject.getKieSessionModel(kSessionName);
    if (kSessionModel == null) {
      log.error("Unknown KieSession name: " + kSessionName);
      return null;
    }
    if (kSessionModel.getType() == KieSessionModel.KieSessionType.STATEFUL) {
      throw new RuntimeException(
          "Trying to create a stateless KieSession from a stateful KieSessionModel: "
              + kSessionName);
    }
    KieBase kBase = getKieBase(kSessionModel.getKieBaseModel().getName());
    if (kBase == null) {
      log.error("Unknown KieBase name: " + kSessionModel.getKieBaseModel().getName());
      return null;
    }

    StatelessKieSession statelessKieSession =
        kBase.newStatelessKieSession(
            conf != null ? conf : getKnowledgeSessionConfiguration(kSessionModel));
    wireListnersAndWIHs(kSessionModel, statelessKieSession);
    registerLoggers(kSessionModel, statelessKieSession);
    statelessKSessions.put(kSessionName, statelessKieSession);
    return statelessKieSession;
  }
 private void registerLoggers(KieSessionModelImpl kSessionModel, KieRuntimeEventManager kSession) {
   KieLoggers kieLoggers = KieServices.Factory.get().getLoggers();
   if (kSessionModel.getConsoleLogger() != null) {
     kieLoggers.newConsoleLogger(kSession);
   }
   FileLoggerModel fileLogger = kSessionModel.getFileLogger();
   if (fileLogger != null) {
     if (fileLogger.isThreaded()) {
       kieLoggers.newThreadedFileLogger(kSession, fileLogger.getFile(), fileLogger.getInterval());
     } else {
       kieLoggers.newFileLogger(kSession, fileLogger.getFile());
     }
   }
 }
  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 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());
          }
        }
      }
    }
  }
Beispiel #6
0
    public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) {
      final KieSessionModelImpl kSession = new KieSessionModelImpl();
      kSession.name = reader.getAttribute("name");
      kSession.setDefault("true".equals(reader.getAttribute("default")));

      String kSessionType = reader.getAttribute("type");
      kSession.setType(
          kSessionType != null
              ? KieSessionType.valueOf(kSessionType.toUpperCase())
              : KieSessionType.STATEFUL);

      String clockType = reader.getAttribute("clockType");
      if (clockType != null) {
        kSession.setClockType(ClockTypeOption.get(clockType));
      }

      String beliefSystem = reader.getAttribute("beliefSystem");
      if (beliefSystem != null) {
        kSession.setBeliefSystem(BeliefSystemTypeOption.get(beliefSystem));
      }

      String scope = reader.getAttribute("scope");
      if (scope != null) {
        kSession.setScope(scope);
      }

      readNodes(
          reader,
          new AbstractXStreamConverter.NodeReader() {
            public void onNode(HierarchicalStreamReader reader, String name, String value) {
              if ("listeners".equals(name)) {
                while (reader.hasMoreChildren()) {
                  reader.moveDown();
                  String nodeName = reader.getNodeName();
                  ListenerModelImpl listener = readObject(reader, context, ListenerModelImpl.class);
                  listener.setKSession(kSession);
                  listener.setKind(ListenerModel.Kind.fromString(nodeName));
                  kSession.addListenerModel(listener);
                  reader.moveUp();
                }
              } else if ("workItemHandlers".equals(name)) {
                List<WorkItemHandlerModelImpl> wihs =
                    readObjectList(reader, context, WorkItemHandlerModelImpl.class);
                for (WorkItemHandlerModelImpl wih : wihs) {
                  wih.setKSession(kSession);
                  kSession.addWorkItemHandelerModel(wih);
                }
              } else if ("consoleLogger".equals(name)) {
                String consoleLogger = reader.getAttribute("name");
                kSession.setConsoleLogger(consoleLogger == null ? "" : consoleLogger);
              } else if ("fileLogger".equals(name)) {
                FileLoggerModelImpl fileLoggerModel =
                    new FileLoggerModelImpl(reader.getAttribute("file"));
                try {
                  fileLoggerModel.setInterval(Integer.parseInt(reader.getAttribute("interval")));
                } catch (Exception e) {
                }
                try {
                  fileLoggerModel.setThreaded(
                      Boolean.parseBoolean(reader.getAttribute("threaded")));
                } catch (Exception e) {
                }
                kSession.fileLogger = fileLoggerModel;
              }
            }
          });
      return kSession;
    }
Beispiel #7
0
    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
      KieSessionModelImpl kSession = (KieSessionModelImpl) value;
      writer.addAttribute("name", kSession.getName());
      writer.addAttribute("type", kSession.getType().toString().toLowerCase());
      writer.addAttribute("default", Boolean.toString(kSession.isDefault()));
      if (kSession.getClockType() != null) {
        writer.addAttribute("clockType", kSession.getClockType().getClockType());
      }
      if (kSession.getBeliefSystem() != null) {
        writer.addAttribute(
            "beliefSystem", kSession.getBeliefSystem().getBeliefSystemType().toLowerCase());
      }
      if (kSession.getScope() != null) {
        writer.addAttribute("scope", kSession.getScope());
      }
      if (kSession.getConsoleLogger() != null) {
        writer.startNode("consoleLogger");
        if (kSession.getConsoleLogger().length() > 0) {
          writer.addAttribute("name", kSession.getConsoleLogger());
        }
        writer.endNode();
      }
      if (kSession.getFileLogger() != null) {
        writer.startNode("fileLogger");
        writer.addAttribute("file", kSession.getFileLogger().getFile());
        writer.addAttribute("threaded", "" + kSession.getFileLogger().isThreaded());
        writer.addAttribute("interval", "" + kSession.getFileLogger().getInterval());
        writer.endNode();
      }

      writeObjectList(
          writer,
          context,
          "workItemHandlers",
          "workItemHandler",
          kSession.getWorkItemHandlerModels());

      if (!kSession.getListenerModels().isEmpty()) {
        writer.startNode("listeners");
        for (ListenerModel listener :
            kSession.getListenerModels(ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER)) {
          writeObject(writer, context, listener.getKind().toString(), listener);
        }
        for (ListenerModel listener :
            kSession.getListenerModels(ListenerModel.Kind.AGENDA_EVENT_LISTENER)) {
          writeObject(writer, context, listener.getKind().toString(), listener);
        }
        for (ListenerModel listener :
            kSession.getListenerModels(ListenerModel.Kind.PROCESS_EVENT_LISTENER)) {
          writeObject(writer, context, listener.getKind().toString(), listener);
        }
        writer.endNode();
      }
    }