コード例 #1
0
ファイル: SessionTest.java プロジェクト: psiroky/jbpm
 private void testStartProcess(SessionManagerFactory factory) throws Exception {
   SessionManager sessionManager = factory.getSessionManager();
   long taskId;
   synchronized (
       (SingleSessionCommandService)
           ((CommandBasedStatefulKnowledgeSession) sessionManager.getKnowledgeSession())
               .getCommandService()) {
     UserTransaction ut =
         (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
     ut.begin();
     System.out.println(
         "Starting process on ksession " + sessionManager.getKnowledgeSession().getId());
     ProcessInstance processInstance =
         sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null);
     System.out.println(
         "Started process instance "
             + processInstance.getId()
             + " on ksession "
             + sessionManager.getKnowledgeSession().getId());
     long workItemId =
         ((HumanTaskNodeInstance)
                 ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next())
             .getWorkItemId();
     taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId();
     System.out.println("Created task " + taskId);
     ut.commit();
   }
   sessionManager.getTaskService().claim(taskId, "mary");
   sessionManager.dispose();
 }
コード例 #2
0
ファイル: SessionTest.java プロジェクト: psiroky/jbpm
  public void testNewSessionFailBefore() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("sampleFailBefore.bpmn"), ResourceType.BPMN2);
    KnowledgeBase kbase = kbuilder.newKnowledgeBase();
    SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase);
    SessionManager sessionManager = factory.getSessionManager();

    try {
      ProcessInstance processInstance =
          sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null);
      fail("Started process instance " + processInstance.getId());
    } catch (RuntimeException e) {
      // do nothing
    }

    // TODO: whenever transaction fails, do we need to dispose? can we?
    // sessionManager.dispose();

    List<Status> statusses = new ArrayList<Status>();
    statusses.add(Status.Reserved);

    sessionManager = factory.getSessionManager();
    List<TaskSummary> tasks =
        sessionManager.getTaskService().getTasksAssignedAsPotentialOwner("mary", "en-UK");
    assertEquals(0, tasks.size());

    sessionManager.dispose();
    factory.dispose();
  }
コード例 #3
0
ファイル: SignalEventsTest.java プロジェクト: hustmilan/jbpm
  public void testSignalBoundaryEvent() {
    KnowledgeBase kbase =
        createKnowledgeBase(
            "BPMN2-BoundarySignalEventOnTaskbpmn2.bpmn",
            "BPMN2-IntermediateThrowEventSignal.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    ProcessInstance processInstance = ksession.startProcess("BoundarySignalOnTask");

    ProcessInstance processInstance2 = ksession.startProcess("SignalIntermediateEvent");
    assertProcessInstanceCompleted(processInstance2.getId(), ksession);

    assertProcessInstanceCompleted(processInstance.getId(), ksession);
  }
コード例 #4
0
ファイル: SignalEventsTest.java プロジェクト: hustmilan/jbpm
  public void testSignalBetweenProcesses() {
    KnowledgeBase kbase =
        createKnowledgeBase(
            "BPMN2-IntermediateCatchSignalSingle.bpmn2",
            "BPMN2-IntermediateThrowEventSignal.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);

    ProcessInstance processInstance = ksession.startProcess("BPMN2-IntermediateCatchSignalSingle");
    ksession.getWorkItemManager().completeWorkItem(handler.getWorkItem().getId(), null);

    ProcessInstance processInstance2 = ksession.startProcess("SignalIntermediateEvent");
    assertProcessInstanceCompleted(processInstance2.getId(), ksession);

    assertProcessInstanceCompleted(processInstance.getId(), ksession);
  }
  @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;
  }
コード例 #6
0
ファイル: SignalEventsTest.java プロジェクト: hustmilan/jbpm
  public void testSignalToStartProcess() {
    KnowledgeBase kbase =
        createKnowledgeBase("BPMN2-SignalStart.bpmn2", "BPMN2-IntermediateThrowEventSignal.bpmn2");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    TestWorkItemHandler handler = new TestWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("Human Task", handler);
    final List<String> startedProcesses = new ArrayList<String>();
    ksession.addEventListener(
        new DefaultProcessEventListener() {

          @Override
          public void beforeProcessStarted(ProcessStartedEvent event) {
            startedProcesses.add(event.getProcessInstance().getProcessId());
          }
        });

    ProcessInstance processInstance2 = ksession.startProcess("SignalIntermediateEvent");
    assertProcessInstanceCompleted(processInstance2.getId(), ksession);
    assertEquals(2, startedProcesses.size());
  }
コード例 #7
0
ファイル: SessionTest.java プロジェクト: psiroky/jbpm
  public void testNewSessionFail() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2);
    KnowledgeBase kbase = kbuilder.newKnowledgeBase();
    SessionManagerFactory factory = new NewSessionSessionManagerFactory(kbase);
    SessionManager sessionManager = factory.getSessionManager();
    UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    ProcessInstance processInstance =
        sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null);
    System.out.println("Started process instance " + processInstance.getId());
    long workItemId =
        ((HumanTaskNodeInstance)
                ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next())
            .getWorkItemId();
    long taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId();
    sessionManager.getTaskService().claim(taskId, "mary");
    ut.rollback();
    System.out.println("Rolled back");
    // TODO: whenever transaction fails, do we need to dispose? can we?
    // sessionManager.dispose();

    List<Status> statusses = new ArrayList<Status>();
    statusses.add(Status.Reserved);

    sessionManager = factory.getSessionManager();
    assertNull(sessionManager.getKnowledgeSession().getProcessInstance(processInstance.getId()));
    List<TaskSummary> tasks =
        sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK");
    assertEquals(0, tasks.size());

    ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    processInstance =
        sessionManager.getKnowledgeSession().startProcess("com.sample.bpmn.hello", null);
    workItemId =
        ((HumanTaskNodeInstance)
                ((WorkflowProcessInstance) processInstance).getNodeInstances().iterator().next())
            .getWorkItemId();
    taskId = sessionManager.getTaskService().getTaskByWorkItemId(workItemId).getId();
    sessionManager.getTaskService().claim(taskId, "mary");
    System.out.println("Started process instance " + processInstance.getId());
    ut.commit();

    assertNotNull(sessionManager.getKnowledgeSession().getProcessInstance(processInstance.getId()));
    tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK");
    assertEquals(1, tasks.size());

    taskId = tasks.get(0).getId();
    ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    sessionManager.getTaskService().start(taskId, "mary");
    sessionManager.getTaskService().complete(taskId, "mary", null);
    ut.rollback();
    sessionManager.dispose();

    sessionManager = factory.getSessionManager();
    assertNotNull(sessionManager.getKnowledgeSession().getProcessInstance(processInstance.getId()));
    tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK");
    assertEquals(1, tasks.size());

    taskId = tasks.get(0).getId();
    ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
    ut.begin();
    sessionManager.getTaskService().start(taskId, "mary");
    sessionManager.getTaskService().complete(taskId, "mary", null);
    ut.commit();

    assertNull(sessionManager.getKnowledgeSession().getProcessInstance(processInstance.getId()));
    tasks = sessionManager.getTaskService().getTasksOwned("mary", statusses, "en-UK");
    assertEquals(0, tasks.size());
    sessionManager.dispose();

    factory.dispose();
  }
コード例 #8
0
  @Test
  public void testReleaseProcessWithRules() throws FileException, InterruptedException {
    Domain myDomain = new SimpleDomainImpl("myDomain");
    sessionManager.setDomain(myDomain);

    //        Iterable<Path> processFiles = null;
    //        Iterable<Path> rulesFiles = null;
    //        try {
    //            processFiles = fs.loadFilesByType("examples/release/", "bpmn");
    //            rulesFiles = fs.loadFilesByType("examples/release/", "drl");
    //        } catch (FileException ex) {
    //            Logger.getLogger(KnowledgeDomainServiceImpl.class.getName()).log(Level.SEVERE,
    // null, ex);
    //        }
    //        String kSessionName = "myKsession";
    //        for (Path p : processFiles) {
    //
    //
    //            String processString = new String(fs.loadFile(p));
    //            String processId = bpmn2Service.findProcessId(processString);
    //            if(!processId.equals("")){
    //              System.out.println(" >>> Loading Path -> " + p.toString());
    //              myDomain.addProcessDefinitionToKsession(kSessionName, p);
    //              myDomain.addProcessBPMN2ContentToKsession(kSessionName, processId ,
    // processString);
    //            }
    //        }
    //        for (Path p : rulesFiles) {
    //            System.out.println(" >>> Loading Path -> " + p.toString());
    //            myDomain.addRulesDefinitionToKsession(kSessionName, p);
    //        }
    //
    //        sessionManager.buildSessions(true);

    sessionManager.buildSession("myKsession", "examples/release/", true);

    sessionManager.addKsessionHandler(
        "myKsession", "MoveToStagingArea", new DoNothingWorkItemHandler());
    sessionManager.addKsessionHandler("myKsession", "MoveToTest", new DoNothingWorkItemHandler());
    sessionManager.addKsessionHandler("myKsession", "TriggerTests", triggerTestsWorkItemHandler);
    sessionManager.addKsessionHandler(
        "myKsession", "MoveBackToStaging", new DoNothingWorkItemHandler());
    sessionManager.addKsessionHandler(
        "myKsession", "MoveToProduction", new DoNothingWorkItemHandler());

    sessionManager.addKsessionHandler("myKsession", "Email", notificationWorkItemHandler);

    sessionManager.registerHandlersForSession("myKsession", 1);

    sessionManager.registerRuleListenerForSession("myKsession", 1);

    sessionManager
        .getKsessionsByName("myKsession")
        .get(1)
        .setGlobal("rulesNotificationService", rulesNotificationService);

    sessionManager.getKsessionsByName("myKsession").get(1).setGlobal("taskService", taskService);

    // Let's start a couple of processes
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("release_name", "first release");
    params.put("release_path", "/releasePath/");

    ProcessInstance firstPI =
        sessionManager
            .getKsessionsByName("myKsession")
            .get(1)
            .startProcess("org.jbpm.release.process", params);

    params = new HashMap<String, Object>();
    params.put("release_name", "second release");
    params.put("release_path", "/releasePath2/");

    ProcessInstance secondPI =
        sessionManager
            .getKsessionsByName("myKsession")
            .get(1)
            .startProcess("org.jbpm.release.process", params);

    QueryResults queryResults =
        sessionManager
            .getKsessionsByName("myKsession")
            .get(1)
            .getQueryResults("getProcessInstances", new Object[] {});

    assertEquals(2, queryResults.size());

    params = new HashMap<String, Object>();
    params.put("release_name", "third release");
    params.put("release_path", "/releasePath/");

    // This process must be automatically aborted because it's using the same release path than the
    // first process.
    ProcessInstance thirdPI =
        sessionManager
            .getKsessionsByName("myKsession")
            .get(1)
            .startProcess("org.jbpm.release.process", params);

    assertEquals(ProcessInstance.STATE_ABORTED, thirdPI.getState());

    Collection<RuleNotificationInstanceDesc> allNotificationInstance =
        rulesNotificationService.getAllNotificationInstance();
    assertEquals(1, allNotificationInstance.size());

    Collection<RuleNotificationInstanceDesc> notificationsBySessionId =
        rulesNotificationService.getAllNotificationInstanceBySessionId(1);
    assertEquals(1, notificationsBySessionId.size());
  }
コード例 #9
0
ファイル: EventTest.java プロジェクト: hustmilan/jbpm
  public void testEvent1() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.process.event");
    process.setName("Event Process");

    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("event");
    ObjectDataType personDataType = new ObjectDataType();
    personDataType.setClassName("org.drools.Person");
    variable.setType(personDataType);
    variables.add(variable);
    process.getVariableScope().setVariables(variables);

    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);

    MilestoneNode milestoneNode = new MilestoneNode();
    milestoneNode.setName("Milestone");
    milestoneNode.setConstraint("eval(false)");
    milestoneNode.setId(2);
    process.addNode(milestoneNode);
    new ConnectionImpl(
        startNode, Node.CONNECTION_DEFAULT_TYPE,
        milestoneNode, Node.CONNECTION_DEFAULT_TYPE);

    EventNode eventNode = new EventNode();
    EventTypeFilter eventFilter = new EventTypeFilter();
    eventFilter.setType("myEvent");
    eventNode.addEventFilter(eventFilter);
    eventNode.setVariableName("event");
    eventNode.setId(3);
    process.addNode(eventNode);

    final List<String> myList = new ArrayList<String>();
    ActionNode actionNode = new ActionNode();
    actionNode.setName("Print");
    DroolsAction action = new DroolsConsequenceAction("java", null);
    action.setMetaData(
        "Action",
        new Action() {
          public void execute(ProcessContext context) throws Exception {
            System.out.println(
                "Detected event for person " + ((Person) context.getVariable("event")).getName());
            myList.add("Executed action");
          }
        });
    actionNode.setAction(action);
    actionNode.setId(4);
    process.addNode(actionNode);
    new ConnectionImpl(
        eventNode, Node.CONNECTION_DEFAULT_TYPE,
        actionNode, Node.CONNECTION_DEFAULT_TYPE);

    Join join = new Join();
    join.setName("XOR Join");
    join.setType(Join.TYPE_XOR);
    join.setId(5);
    process.addNode(join);
    new ConnectionImpl(
        milestoneNode, Node.CONNECTION_DEFAULT_TYPE,
        join, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(
        actionNode, Node.CONNECTION_DEFAULT_TYPE,
        join, Node.CONNECTION_DEFAULT_TYPE);

    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(6);
    process.addNode(endNode);
    new ConnectionImpl(
        join, Node.CONNECTION_DEFAULT_TYPE,
        endNode, Node.CONNECTION_DEFAULT_TYPE);

    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    ((AbstractRuleBase) ((InternalKnowledgeBase) kbase).getRuleBase()).addProcess(process);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    ProcessInstance processInstance = ksession.startProcess("org.drools.process.event");
    assertEquals(0, myList.size());
    Person jack = new Person();
    jack.setName("Jack");
    processInstance.signalEvent("myEvent", jack);
    assertEquals(1, myList.size());
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
  }