Search in sources :

Example 76 with WorkItem

use of org.kie.api.runtime.process.WorkItem 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 77 with WorkItem

use of org.kie.api.runtime.process.WorkItem 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 78 with WorkItem

use of org.kie.api.runtime.process.WorkItem 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 79 with WorkItem

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

the class BusinessRuleTaskHandler method handleDMN.

protected void handleDMN(WorkItem workItem, Map<String, Object> parameters, Map<String, Object> results) {
    String namespace = (String) parameters.remove("Namespace");
    String model = (String) parameters.remove("Model");
    String decision = (String) parameters.remove("Decision");
    DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    DMNModel dmnModel = runtime.getModel(namespace, model);
    if (dmnModel == null) {
        throw new IllegalArgumentException("DMN model '" + model + "' not found with namespace '" + namespace + "'");
    }
    DMNResult dmnResult = null;
    DMNContext context = runtime.newContext();
    for (Entry<String, Object> entry : parameters.entrySet()) {
        context.set(entry.getKey(), entry.getValue());
    }
    if (decision != null && !decision.isEmpty()) {
        dmnResult = runtime.evaluateDecisionByName(dmnModel, decision, context);
    } else {
        dmnResult = runtime.evaluateAll(dmnModel, context);
    }
    if (dmnResult.hasErrors()) {
        String errors = dmnResult.getMessages(Severity.ERROR).stream().map(message -> message.toString()).collect(Collectors.joining(", "));
        throw new RuntimeException("DMN result errors:: " + errors);
    }
    results.putAll(dmnResult.getContext().getAll());
}
Also used : Cacheable(org.kie.internal.runtime.Cacheable) DMNResult(org.kie.dmn.api.core.DMNResult) ExecutionResults(org.kie.api.runtime.ExecutionResults) LoggerFactory(org.slf4j.LoggerFactory) AbstractLogOrThrowWorkItemHandler(org.jbpm.process.workitem.core.AbstractLogOrThrowWorkItemHandler) KieCommands(org.kie.api.command.KieCommands) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DMNModel(org.kie.dmn.api.core.DMNModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Map(java.util.Map) KieServices(org.kie.api.KieServices) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieSession(org.kie.api.runtime.KieSession) Command(org.kie.api.command.Command) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Logger(org.slf4j.Logger) WorkItem(org.kie.api.runtime.process.WorkItem) KieContainer(org.kie.api.runtime.KieContainer) Collectors(java.util.stream.Collectors) Severity(org.kie.dmn.api.core.DMNMessage.Severity) FactHandle(org.kie.api.runtime.rule.FactHandle) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) List(java.util.List) Entry(java.util.Map.Entry) DMNContext(org.kie.dmn.api.core.DMNContext) KieScanner(org.kie.api.builder.KieScanner) DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 80 with WorkItem

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

the class TimerEventTest method testTimerAndGateway.

@Test
@BZ("1036761")
public void testTimerAndGateway() throws Exception {
    KieSession ksession = createKSession(TIMER_AND_GATEWAY);
    int sessionId = ksession.getId();
    TestAsyncWorkItemHandler handler1 = new TestAsyncWorkItemHandler();
    TestAsyncWorkItemHandler handler2 = new TestAsyncWorkItemHandler();
    ksession.getWorkItemManager().registerWorkItemHandler("task1", handler1);
    ksession.getWorkItemManager().registerWorkItemHandler("task2", handler2);
    ProcessInstance instance = ksession.createProcessInstance(TIMER_AND_GATEWAY_ID, new HashMap<String, Object>());
    ksession.startProcessInstance(instance.getId());
    WorkItem workItem1 = handler1.getWorkItem();
    Assertions.assertThat(workItem1).isNotNull();
    Assertions.assertThat(handler1.getWorkItem()).isNull();
    // first safe state: task1 completed
    ksession.getWorkItemManager().completeWorkItem(workItem1.getId(), null);
    ksession = restoreKSession(TIMER_AND_GATEWAY);
    ksession.getWorkItemManager().registerWorkItemHandler("task1", handler1);
    ksession.getWorkItemManager().registerWorkItemHandler("task2", handler2);
    // second safe state: timer completed, waiting on task2
    for (int i = 0; i < 7; i++) {
        Thread.sleep(1000);
    }
    WorkItem workItem2 = handler2.getWorkItem();
    // Both sides of the join are completed. But on the process instance, there are two JoinInstance for the same
    // Join, and since it is an AND join, it never reaches task2. It fails after the next assertion
    Assertions.assertThat(workItem2).isNotNull();
    Assertions.assertThat(handler1.getWorkItem()).isNull();
}
Also used : KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.kie.api.runtime.process.WorkItem) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Aggregations

WorkItem (org.kie.api.runtime.process.WorkItem)174 Test (org.junit.Test)139 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)127 KieBase (org.kie.api.KieBase)110 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)91 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)87 HashMap (java.util.HashMap)72 KieSession (org.kie.api.runtime.KieSession)48 WorkItemManager (org.kie.api.runtime.process.WorkItemManager)33 ArrayList (java.util.ArrayList)29 DefaultProcessEventListener (org.kie.api.event.process.DefaultProcessEventListener)27 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)26 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)22 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 NodeLeftCountDownProcessEventListener (org.jbpm.test.listener.NodeLeftCountDownProcessEventListener)14 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)13 ProcessEventListener (org.kie.api.event.process.ProcessEventListener)13 Map (java.util.Map)11 StringReader (java.io.StringReader)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10