protected KieSession createKnowledgeSession(String processFile) throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    if (processFile != null) {
      Resource process = ResourceFactory.newClassPathResource(processFile);
      kfs.write(process);
    }

    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();

    if (kb.getResults().hasMessages(Level.ERROR)) {
      throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }

    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    KieBase kbase = kContainer.getKieBase();

    Environment env = EnvironmentFactory.newEnvironment();

    Properties defaultProps = new Properties();
    defaultProps.setProperty(
        "drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName());
    defaultProps.setProperty(
        "drools.processInstanceManagerFactory",
        DefaultProcessInstanceManagerFactory.class.getName());
    SessionConfiguration conf = new SessionConfiguration(defaultProps);

    KieSession ksession = (StatefulKnowledgeSession) kbase.newKieSession(conf, env);
    return ksession;
  }
Esempio n. 2
0
  @Test
  public void testSignalStartDynamic() throws Exception {

    KieBase kbase = createKnowledgeBase("BPMN2-SignalStart.bpmn2");
    ksession = createKnowledgeSession(kbase);
    // create KieContainer after session was created to make sure no runtime data
    // will be used during serialization (deep clone)
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    kContainer.getKieBase();

    final List<Long> list = new ArrayList<Long>();
    ksession.addEventListener(
        new DefaultProcessEventListener() {
          public void afterProcessStarted(ProcessStartedEvent event) {
            logger.info("{}", event.getProcessInstance().getId());
            list.add(event.getProcessInstance().getId());
          }
        });
    ksession.signalEvent("MySignal", "NewValue");
    Thread.sleep(500);
    assertEquals(1, getNumberOfProcessInstances("Minimal"));
    // now remove the process from kbase to make sure runtime based listeners are removed from
    // signal manager
    kbase.removeProcess("Minimal");
    ksession.signalEvent("MySignal", "NewValue");
    Thread.sleep(500);
    // must be still one as the process was removed
    assertEquals(1, getNumberOfProcessInstances("Minimal"));
  }
Esempio n. 3
0
  /**
   * This method loads the rule base associated with the Drools event processor.
   *
   * @return The knowledge base
   */
  private KieBase loadRuleBase() {
    String droolsRuleBase = getRuleName() + ".drl";

    try {
      KieServices ks = KieServices.Factory.get();
      KieRepository kr = ks.getRepository();

      KieModuleModel kmm = ks.newKieModuleModel();
      KieBaseModel kbm =
          kmm.newKieBaseModel(getRuleName())
              .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
              .setEventProcessingMode(EventProcessingOption.STREAM);
      kbm.setDefault(true);

      KieFileSystem kfs = ks.newKieFileSystem();
      kfs.write(
          "src/main/resources/" + kbm.getName() + "/rule1.drl",
          ks.getResources().newClassPathResource(droolsRuleBase));
      kfs.writeKModuleXML(kmm.toXML());

      KieBuilder kb = ks.newKieBuilder(kfs);
      kb.buildAll();

      KieContainer container = ks.newKieContainer(kr.getDefaultReleaseId());
      KieBase kbase = container.getKieBase();
      // TODO: hack it for now, this attribute should be supported in the following drools6 release.
      System.setProperty("kie.mbean", "enabled");
      return kbase;

    } catch (Throwable e) {
      LOG.log(
          Level.SEVERE,
          MessageFormat.format(
              java.util.PropertyResourceBundle.getBundle("ep-drools.Messages")
                  .getString("EP-DROOLS-1"),
              droolsRuleBase,
              getRuleName()),
          e);
    }

    return (null);
  }