Search in sources :

Example 41 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.

the class AsyncTaskTransactionTest method registerAsyncHandler.

private KieSession registerAsyncHandler(KieSession ksession) {
    WorkItemManager wim = ksession.getWorkItemManager();
    wim.registerWorkItemHandler("async", new AsyncWorkItemHandler(getExecutorService()));
    return ksession;
}
Also used : AsyncWorkItemHandler(org.jbpm.executor.impl.wih.AsyncWorkItemHandler) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 42 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.

the class AbstractAuditLogServiceTest method runTestLogger4WithCustomVariableIndexer.

public static void runTestLogger4WithCustomVariableIndexer(KieSession session, AuditLogService auditLogService) throws Exception {
    final List<Long> workItemIds = new ArrayList<Long>();
    session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            workItemIds.add(workItem.getId());
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    // record the initial count to compare to later
    List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow");
    int initialProcessInstanceSize = processInstances.size();
    // start process instance
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> list = new LinkedList<String>();
    list.add("One");
    list.add("Two");
    list.add("Three");
    params.put("list", list);
    long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
    // Test findVariableInstancesByName* methods: check for variables (only) in active processes
    List<VariableInstanceLog> varLogs = auditLogService.findVariableInstancesByName("s", true);
    Assertions.assertThat(varLogs).isNotEmpty();
    Assertions.assertThat(varLogs.size()).isEqualTo(1);
    for (Long workItemId : workItemIds) {
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("Result", "ResultValue");
        session.getWorkItemManager().completeWorkItem(workItemId, results);
    }
    logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances.size()).isEqualTo(initialProcessInstanceSize + 1);
    ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
    logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
    Assertions.assertThat(processInstance.getStart()).isNotNull();
    // ProcessInstanceLog does not contain end date.
    Assertions.assertThat(processInstance.getEnd()).isNotNull();
    Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
    Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
    List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
    Assertions.assertThat(variableInstances.size()).isEqualTo(13);
    for (VariableInstanceLog variableInstance : variableInstances) {
        logger.debug(variableInstance.toString());
        Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
        Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
        Assertions.assertThat(variableInstance.getDate()).isNotNull();
    }
    List<VariableInstanceLog> listVariables = new ArrayList<VariableInstanceLog>();
    // collect only those that are related to list process variable
    for (VariableInstanceLog v : variableInstances) {
        if (v.getVariableInstanceId().equals("list")) {
            listVariables.add(v);
        }
    }
    Assertions.assertThat(listVariables.size()).isEqualTo(3);
    List<String> variableValues = new ArrayList<String>();
    List<String> variableIds = new ArrayList<String>();
    for (VariableInstanceLog var : listVariables) {
        variableValues.add(var.getValue());
        variableIds.add(var.getVariableId());
        // Various DBs return various empty values. (E.g. Oracle returns null.)
        Assertions.assertThat(var.getOldValue()).isIn("", " ", null);
        Assertions.assertThat(var.getProcessInstanceId()).isEqualTo(processInstance.getProcessInstanceId());
        Assertions.assertThat(var.getProcessId()).isEqualTo(processInstance.getProcessId());
        Assertions.assertThat(var.getVariableInstanceId()).isEqualTo("list");
    }
    Assertions.assertThat(variableValues).contains("One", "Two", "Three");
    Assertions.assertThat(variableIds).contains("list[0]", "list[1]", "list[2]");
    // Test findVariableInstancesByName* methods
    List<VariableInstanceLog> emptyVarLogs = auditLogService.findVariableInstancesByName("s", true);
    Assertions.assertThat(emptyVarLogs).isEmpty();
    for (VariableInstanceLog origVarLog : variableInstances) {
        varLogs = auditLogService.findVariableInstancesByName(origVarLog.getVariableId(), false);
        for (VariableInstanceLog varLog : varLogs) {
            Assertions.assertThat(varLog.getVariableId()).isEqualTo(origVarLog.getVariableId());
        }
    }
    emptyVarLogs = auditLogService.findVariableInstancesByNameAndValue("s", "InitialValue", true);
    Assertions.assertThat(emptyVarLogs).isEmpty();
    String varId = "s";
    String varValue = "ResultValue";
    variableInstances = auditLogService.findVariableInstancesByNameAndValue(varId, varValue, false);
    Assertions.assertThat(variableInstances.size()).isEqualTo(3);
    VariableInstanceLog varLog = variableInstances.get(0);
    Assertions.assertThat(varLog.getVariableId()).isEqualTo(varId);
    Assertions.assertThat(varLog.getValue()).isEqualTo(varValue);
    auditLogService.clear();
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances).isEmpty();
}
Also used : WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 43 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.

the class AbstractAuditLogServiceTest method runTestLogger4LargeVariable.

public static void runTestLogger4LargeVariable(KieSession session, AuditLogService auditLogService) throws Exception {
    session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            Map<String, Object> results = new HashMap<String, Object>();
            results.put("Result", "ResultValue");
            manager.completeWorkItem(workItem.getId(), results);
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    // record the initial count to compare to later
    List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    int initialProcessInstanceSize = processInstances.size();
    // start process instance
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> list = new ArrayList<String>();
    list.add("One");
    list.add("Two");
    String three = "";
    for (int i = 0; i < 1024; i++) {
        three += "*";
    }
    list.add(three);
    params.put("list", list);
    long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
    logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    int expected = initialProcessInstanceSize + 1;
    Assertions.assertThat(processInstances.size()).isEqualTo(expected).withFailMessage(String.format("Expected %d ProcessInstanceLog instances, not %d", expected, processInstances.size()));
    ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
    logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
    Assertions.assertThat(processInstance.getStart()).isNotNull();
    Assertions.assertThat(processInstance.getEnd()).isNotNull().withFailMessage("ProcessInstanceLog does not contain end date.");
    Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
    Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
    List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
    Assertions.assertThat(variableInstances.size()).isEqualTo(8);
    for (VariableInstanceLog variableInstance : variableInstances) {
        logger.debug(variableInstance.toString());
        Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
        Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
        Assertions.assertThat(variableInstance.getDate()).isNotNull();
    }
    auditLogService.clear();
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances).isNullOrEmpty();
}
Also used : WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 44 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.

the class AbstractAuditLogServiceTest method runTestLogger4.

public static void runTestLogger4(KieSession session, AuditLogService auditLogService) throws Exception {
    final List<Long> workItemIds = new ArrayList<Long>();
    session.getWorkItemManager().registerWorkItemHandler("Human Task", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            workItemIds.add(workItem.getId());
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    // record the initial count to compare to later
    List<ProcessInstanceLog> processInstances = auditLogService.findProcessInstances("com.sample.ruleflow");
    int initialProcessInstanceSize = processInstances.size();
    // start process instance
    Map<String, Object> params = new HashMap<String, Object>();
    List<String> list = new ArrayList<String>();
    list.add("One");
    list.add("Two");
    list.add("Three");
    params.put("list", list);
    long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
    // Test findVariableInstancesByName* methods: check for variables (only) in active processes
    List<VariableInstanceLog> varLogs = auditLogService.findVariableInstancesByName("s", true);
    Assertions.assertThat(varLogs).isNotEmpty();
    Assertions.assertThat(varLogs.size()).isEqualTo(1);
    for (Long workItemId : workItemIds) {
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("Result", "ResultValue");
        session.getWorkItemManager().completeWorkItem(workItemId, results);
    }
    logger.debug("Checking process instances for process 'com.sample.ruleflow3'");
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances.size()).isEqualTo(initialProcessInstanceSize + 1);
    ProcessInstanceLog processInstance = processInstances.get(initialProcessInstanceSize);
    logger.debug("{} -> {} - {}", processInstance.toString(), processInstance.getStart(), processInstance.getEnd());
    Assertions.assertThat(processInstance.getStart()).isNotNull();
    Assertions.assertThat(processInstance.getEnd()).isNotNull().withFailMessage("ProcessInstanceLog does not contain end date.");
    Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
    Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
    List<VariableInstanceLog> variableInstances = auditLogService.findVariableInstances(processInstanceId);
    Assertions.assertThat(variableInstances.size()).isEqualTo(11);
    for (VariableInstanceLog variableInstance : variableInstances) {
        logger.debug(variableInstance.toString());
        Assertions.assertThat(processInstance.getProcessInstanceId().longValue()).isEqualTo(processInstanceId);
        Assertions.assertThat(processInstance.getProcessId()).isEqualTo("com.sample.ruleflow3");
        Assertions.assertThat(variableInstance.getDate()).isNotNull();
    }
    // Test findVariableInstancesByName* methods
    List<VariableInstanceLog> emptyVarLogs = auditLogService.findVariableInstancesByName("s", true);
    Assertions.assertThat(emptyVarLogs).isEmpty();
    for (VariableInstanceLog origVarLog : variableInstances) {
        varLogs = auditLogService.findVariableInstancesByName(origVarLog.getVariableId(), false);
        for (VariableInstanceLog varLog : varLogs) {
            Assertions.assertThat(varLog.getVariableId()).isEqualTo(origVarLog.getVariableId());
        }
    }
    emptyVarLogs = auditLogService.findVariableInstancesByNameAndValue("s", "InitialValue", true);
    Assertions.assertThat(emptyVarLogs).isEmpty();
    String varId = "s";
    String varValue = "ResultValue";
    variableInstances = auditLogService.findVariableInstancesByNameAndValue(varId, varValue, false);
    Assertions.assertThat(variableInstances.size()).isEqualTo(3);
    VariableInstanceLog varLog = variableInstances.get(0);
    Assertions.assertThat(varLog.getVariableId()).isEqualTo(varId);
    Assertions.assertThat(varLog.getValue()).isEqualTo(varValue);
    auditLogService.clear();
    processInstances = auditLogService.findProcessInstances("com.sample.ruleflow3");
    Assertions.assertThat(processInstances).isEmpty();
}
Also used : WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) SystemOutWorkItemHandler(org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler) WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Example 45 with WorkItemManager

use of org.kie.api.runtime.process.WorkItemManager in project jbpm by kiegroup.

the class HumanResourcesHiringTest method testHiringProcess.

@SuppressWarnings("unchecked")
private void testHiringProcess(RuntimeManager manager, Context<?> context) {
    RuntimeEngine runtime = manager.getRuntimeEngine(context);
    KieSession ksession = runtime.getKieSession();
    TaskService taskService = runtime.getTaskService();
    assertNotNull(runtime);
    assertNotNull(ksession);
    ksession.getWorkItemManager().registerWorkItemHandler("EmailService", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            manager.completeWorkItem(workItem.getId(), null);
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            // To change body of generated methods, choose Tools | Templates.
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
    ksession.getWorkItemManager().registerWorkItemHandler("TwitterService", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            manager.completeWorkItem(workItem.getId(), null);
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            // To change body of generated methods, choose Tools | Templates.
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
    ProcessInstance processInstance = ksession.startProcess("hiring");
    Collection<NodeInstanceDesc> activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    Collection<NodeInstanceDesc> completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(1, completedNodes.size());
    List<TaskSummary> tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("HR");
    TaskSummary HRInterview = tasks.get(0);
    taskService.claim(HRInterview.getId(), "katy");
    taskService.start(HRInterview.getId(), "katy");
    Map<String, Object> hrOutput = new HashMap<String, Object>();
    hrOutput.put("out_name", "salaboy");
    hrOutput.put("out_age", 29);
    hrOutput.put("out_mail", "salaboy@gmail.com");
    hrOutput.put("out_score", 8);
    taskService.complete(HRInterview.getId(), "katy", hrOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(2, completedNodes.size());
    assertNotNull(processInstance);
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("IT");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary techInterview = tasks.get(0);
    Task techInterviewTask = taskService.getTaskById(techInterview.getId());
    Content contentById = taskService.getContentById(techInterviewTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    Map<String, Object> taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(7, taskContent.size());
    assertEquals("salaboy@gmail.com", taskContent.get("in_mail"));
    assertEquals(29, taskContent.get("in_age"));
    assertEquals("salaboy", taskContent.get("in_name"));
    taskService.claim(techInterview.getId(), "salaboy");
    taskService.start(techInterview.getId(), "salaboy");
    Map<String, Object> techOutput = new HashMap<String, Object>();
    techOutput.put("out_skills", "java, jbpm, drools");
    techOutput.put("out_twitter", "@salaboy");
    techOutput.put("out_score", 8);
    taskService.complete(techInterview.getId(), "salaboy", techOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(3, completedNodes.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("Accounting");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary createProposal = tasks.get(0);
    Task createProposalTask = taskService.getTaskById(createProposal.getId());
    contentById = taskService.getContentById(createProposalTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(6, taskContent.size());
    assertEquals(8, taskContent.get("in_tech_score"));
    assertEquals(8, taskContent.get("in_hr_score"));
    taskService.claim(createProposal.getId(), "john");
    taskService.start(createProposal.getId(), "john");
    Map<String, Object> proposalOutput = new HashMap<String, Object>();
    proposalOutput.put("out_offering", 10000);
    taskService.complete(createProposal.getId(), "john", proposalOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(5, completedNodes.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("HR");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary signContract = tasks.get(0);
    Task signContractTask = taskService.getTaskById(signContract.getId());
    contentById = taskService.getContentById(signContractTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(6, taskContent.size());
    assertEquals(10000, taskContent.get("in_offering"));
    assertEquals("salaboy", taskContent.get("in_name"));
    taskService.claim(signContract.getId(), "katy");
    taskService.start(signContract.getId(), "katy");
    Map<String, Object> signOutput = new HashMap<String, Object>();
    signOutput.put("out_signed", true);
    taskService.complete(signContract.getId(), "katy", signOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(0, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(8, completedNodes.size());
    int removeAllTasks = ((InternalTaskService) taskService).removeAllTasks();
    logger.debug(">>> Removed Tasks > {}", removeAllTasks);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) InternalTaskService(org.kie.internal.task.api.InternalTaskService) QueryContext(org.kie.api.runtime.query.QueryContext) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) Content(org.kie.api.task.model.Content) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

WorkItemManager (org.kie.api.runtime.process.WorkItemManager)102 Test (org.junit.Test)86 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)56 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)36 WorkItem (org.kie.api.runtime.process.WorkItem)31 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)27 HashMap (java.util.HashMap)25 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)25 KieBase (org.kie.api.KieBase)23 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 KieSession (org.kie.api.runtime.KieSession)15 Map (java.util.Map)9 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)8 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)8 AsyncWorkItemHandler (org.jbpm.executor.impl.wih.AsyncWorkItemHandler)7 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)7 QueryContext (org.kie.api.runtime.query.QueryContext)6 Document (org.w3c.dom.Document)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ArrayList (java.util.ArrayList)5