public Void execute(Context context) {
    em.joinTransaction();
    em.persist(mainObject);

    if (subObject != null) {
      KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();

      // THe following 3 lines are the important ones! (See below for an explanation)
      KnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase();
      cleanKBase.addKnowledgePackages(
          ((KnowledgeBase) ksession.getKieBase()).getKnowledgePackages());
      StatefulKnowledgeSession commandKSession =
          JPAKnowledgeService.newStatefulKnowledgeSession(
              cleanKBase, null, initializeEnvironment());

      /**
       * Here's what's going on: If the SingleSessionCommandService (SSCS) & JtaTransactionManager
       * (JTM) were _not_ aware of transactions, -> then inserting the mainObject _before_ having
       * inserted the subObject would cause the operation/persist to fail and the transaction to
       * fail. - This is because the mainObject contains a foreign key referring to the subObject.
       *
       * <p>However, the SSCS & JTM check whether or not they're owners of the transaction when
       * starting and when committing the transaction they use. -> So that when we insert the
       * mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession), it does _not_ mess with
       * the transaction state and the operation succeeds.
       */
      TransactionTestCommand transactionTestSubCommand =
          new TransactionTestCommand(this.subObject, getPersistenceEnvironment());
      commandKSession.execute(transactionTestSubCommand);
    }

    return null;
  }
Пример #2
0
  @Test
  public void testHelloWorldWithPackages() throws Exception {
    String drl1 =
        "package org.drools\n"
            + "rule R1 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    String drl2 =
        "package org.drools\n"
            + "rule R2 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    KieServices ks = KieServices.Factory.get();

    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");

    KieFileSystem kfs =
        ks.newKieFileSystem()
            .generateAndWritePomXML(releaseId)
            .write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1)
            .write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2)
            .writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1").toXML());
    ks.newKieBuilder(kfs).buildAll();

    KieSession ksession = ks.newKieContainer(releaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));

    int count = ksession.fireAllRules();

    assertEquals(1, count);
  }
Пример #3
0
  @Test
  public void testHelloWorldWithEmptyFile() throws Exception {
    String drl =
        "package org.drools\n"
            + "rule R1 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs =
        ks.newKieFileSystem()
            .write("src/main/resources/r1.drl", drl)
            .write(
                "src/main/resources/empty.drl",
                ks.getResources().newInputStreamResource(new ByteArrayInputStream(new byte[0])));
    ks.newKieBuilder(kfs).buildAll();

    KieSession ksession =
        ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    ksession.insert(new Message("Hello World"));

    int count = ksession.fireAllRules();

    assertEquals(1, count);
  }
  @Override
  public void setProcessVariable(long processInstanceId, String variableId, Object value) {
    ProcessInstanceDesc piDesc = dataService.getProcessInstanceById(processInstanceId);
    KieSession ksession = domainService.getSessionById(piDesc.getSessionId());
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);

    ((WorkflowProcessInstance) processInstance).setVariable(variableId, value);
  }
  private void checkKSession(KieSession ksession, Object... results) {
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();

    assertEquals(results.length, list.size());
    for (Object result : results) {
      assertTrue(list.contains(result));
    }
  }
  public void go(PrintStream out) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();

    KieModule kModule =
        kr.addKieModule(ks.getResources().newFileSystemResource(getFile("named-kiesession")));

    KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());

    KieSession kSession = kContainer.newKieSession("ksession1");
    kSession.setGlobal("out", out);

    Object msg1 = createMessage(kContainer, "Dave", "Hello, HAL. Do you read me, HAL?");
    kSession.insert(msg1);
    kSession.fireAllRules();
  }
  @Override
  public Collection<String> getAvailableSignals(String businessKey, long processInstanceId) {
    ProcessInstanceDesc piDesc = dataService.getProcessInstanceById(processInstanceId);
    KieSession ksession = domainService.getSessionById(piDesc.getSessionId());
    ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId);
    Collection<String> activeSignals = new ArrayList<String>();

    if (processInstance != null) {
      ((ProcessInstanceImpl) processInstance)
          .setProcess(ksession.getKieBase().getProcess(processInstance.getProcessId()));
      Collection<NodeInstance> activeNodes =
          ((WorkflowProcessInstance) processInstance).getNodeInstances();

      activeSignals.addAll(ProcessInstanceHelper.collectActiveSignals(activeNodes));
    }

    return activeSignals;
  }
  public void initStartDisposeAndLoadSession() {
    try {
      EntityManager em = txm.getEntityManagerFactory().createEntityManager();
      // create new ksession with kstore
      KieSession ksession = kstore.newKieSession(kbase, null, getEnvironment());
      sessionId = ksession.getId();

      logger.info("\n\tSession id: " + sessionId + "\n");

      ksession.getWorkItemManager().registerWorkItemHandler("testWorkItemHandler", workItemHandler);

      ksession.startProcess("timer-flow", null);
      Thread.sleep(4000);
      ksession.dispose();
    } catch (Exception ex) {
      throw new IllegalStateException("The endTheProcess method has been interrupted", ex);
    }
  }
  public void endTheProcess() {
    try {
      KieSession ksession = kstore.loadKieSession(sessionId, kbase, null, getEnvironment());

      // Sleep to check if the timer continues executing.
      logger.info("\n\nSleeping to check that the timer is still running");
      Thread.sleep(5000);

      ksession
          .getWorkItemManager()
          .completeWorkItem(TestWorkItemHandler.getWorkItem().getId(), null);

      logger.info("\n\nSleeping to check that the timer is no longer running");
      Thread.sleep(3000);
      logger.info("Ok");

      ksession.dispose();

    } catch (InterruptedException ex) {
      throw new IllegalStateException("The endTheProcess method has been interrupted", ex);
    }
  }
Пример #10
0
 public Object execute(Context context) {
   KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();
   ksession.getEntryPoint(handle.getEntryPointId()).retract(handle);
   return null;
 }
Пример #11
0
 public Void execute(Context context) {
   KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();
   ksession.getWorkItemManager().abortWorkItem(workItemId);
   return null;
 }
Пример #12
0
  @Test
  public void testHelloWorldOnVersionRange() throws Exception {
    KieServices ks = KieServices.Factory.get();

    buildVersion(ks, "Hello World", "1.0");
    buildVersion(ks, "Aloha Earth", "1.1");
    buildVersion(ks, "Hi Universe", "1.2");

    ReleaseId latestReleaseId = ks.newReleaseId("org.kie", "hello-world", "LATEST");

    KieSession ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());

    ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());

    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "hello-world", "1.0");

    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());

    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());

    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "hello-world", "[1.0,1.2)");

    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());

    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
  }