Search in sources :

Example 46 with WorkItemManager

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

the class CustomWorkItemHandlerTest method testRegisterHandlerWithKsessionUsingConfiguration.

@Test
public void testRegisterHandlerWithKsessionUsingConfiguration() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    Properties props = new Properties();
    props.setProperty("drools.workItemHandlers", "CustomWorkItemHandlers.conf");
    KieSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(props);
    KieSession ksession = kbase.newKieSession(config, EnvironmentFactory.newEnvironment());
    assertNotNull(ksession);
    // this test would fail on creation of the work item manager if injecting session is not supported
    WorkItemManager manager = ksession.getWorkItemManager();
    assertNotNull(manager);
    Map<String, WorkItemHandler> handlers = ((SessionConfiguration) config).getWorkItemHandlers();
    assertNotNull(handlers);
    assertEquals(1, handlers.size());
    assertTrue(handlers.containsKey("Custom"));
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Example 47 with WorkItemManager

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

the class ProcessForEachTest method testForEachLargeList.

@Test
public void testForEachLargeList() {
    Reader source = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "         type=\"RuleFlow\" name=\"ForEach\" id=\"org.drools.ForEach\" package-name=\"org.drools\" >\n" + "  <header>\n" + "    <variables>\n" + "      <variable name=\"collection\" >\n" + "        <type name=\"org.jbpm.process.core.datatype.impl.type.ObjectDataType\" className=\"java.util.List\" />\n" + "      </variable>\n" + "    </variables>\n" + "  </header>\n" + "\n" + "  <nodes>\n" + "    <forEach id=\"2\" name=\"ForEach\" variableName=\"item\" collectionExpression=\"collection\" >\n" + "      <nodes>\n" + "        <workItem id=\"1\" name=\"Log\" >\n" + "          <work name=\"Log\" >\n" + "            <parameter name=\"Message\" >\n" + "              <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + "            </parameter>\n" + "          </work>\n" + "          <mapping type=\"in\" from=\"item\" to=\"Message\" />" + "        </workItem>\n" + "      </nodes>\n" + "      <connections>\n" + "      </connections>\n" + "      <in-ports>\n" + "        <in-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeInType=\"DROOLS_DEFAULT\" />\n" + "      </in-ports>\n" + "      <out-ports>\n" + "        <out-port type=\"DROOLS_DEFAULT\" nodeId=\"1\" nodeOutType=\"DROOLS_DEFAULT\" />\n" + "      </out-ports>\n" + "    </forEach>\n" + "    <start id=\"1\" name=\"Start\" />\n" + "    <end id=\"3\" name=\"End\" />\n" + "  </nodes>\n" + "\n" + "  <connections>\n" + "    <connection from=\"1\" to=\"2\" />\n" + "    <connection from=\"2\" to=\"3\" />\n" + "  </connections>\n" + "</process>");
    builder.addRuleFlow(source);
    KieSession workingMemory = createKieSession(builder.getPackages());
    final List<String> myList = new ArrayList<String>();
    workingMemory.getWorkItemManager().registerWorkItemHandler("Log", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            String message = (String) workItem.getParameter("Message");
            myList.add(message);
            manager.completeWorkItem(workItem.getId(), null);
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    List<String> collection = new ArrayList<String>();
    for (int i = 0; i < 10000; i++) {
        collection.add(i + "");
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("collection", collection);
    ProcessInstance processInstance = (ProcessInstance) workingMemory.startProcess("org.drools.ForEach", params);
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
    assertEquals(10000, myList.size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) StringReader(java.io.StringReader) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 48 with WorkItemManager

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

the class SubProcessTest method testAsynchronousSubProcess.

@Test
public void testAsynchronousSubProcess() {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId("org.drools.core.process.process");
    process.setName("Process");
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    process.addNode(startNode);
    EndNode endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    process.addNode(endNode);
    SubProcessNode subProcessNode = new SubProcessNode();
    subProcessNode.setName("SubProcessNode");
    subProcessNode.setId(3);
    subProcessNode.setProcessId("org.drools.core.process.subprocess");
    process.addNode(subProcessNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, subProcessNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(subProcessNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    RuleFlowProcess subProcess = new RuleFlowProcess();
    subProcess.setId("org.drools.core.process.subprocess");
    subProcess.setName("SubProcess");
    startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    subProcess.addNode(startNode);
    endNode = new EndNode();
    endNode.setName("EndNode");
    endNode.setId(2);
    subProcess.addNode(endNode);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setName("WorkItem");
    Work work = new WorkImpl();
    work.setName("MyWork");
    workItemNode.setWork(work);
    workItemNode.setId(4);
    subProcess.addNode(workItemNode);
    new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, workItemNode, Node.CONNECTION_DEFAULT_TYPE);
    new ConnectionImpl(workItemNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
    KieSession ksession = createKieSession(process, subProcess);
    ksession.getWorkItemManager().registerWorkItemHandler("MyWork", new WorkItemHandler() {

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            logger.info("Executing work item");
            SubProcessTest.this.workItem = workItem;
        }

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
        }
    });
    ksession.startProcess("org.drools.core.process.process");
    TestProcessEventListener procEventListener = new TestProcessEventListener();
    ksession.addEventListener(procEventListener);
    assertNotNull(workItem);
    assertEquals(2, ksession.getProcessInstances().size());
    ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
    assertEquals(0, ksession.getProcessInstances().size());
    verifyEventHistory(asyncEventOrder, procEventListener.getEventHistory());
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) ConnectionImpl(org.jbpm.workflow.core.impl.ConnectionImpl) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) EndNode(org.jbpm.workflow.core.node.EndNode) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) KieSession(org.kie.api.runtime.KieSession) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) TestProcessEventListener(org.jbpm.process.test.TestProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 49 with WorkItemManager

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

the class AbstractAuditLogServiceTest method runTestLoggerWithCustomVariableLogLength.

public static void runTestLoggerWithCustomVariableLogLength(KieSession session, AuditLogService auditLogService) throws Exception {
    System.setProperty("org.jbpm.var.log.length", "15");
    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();
    processInstances = auditLogService.findActiveProcessInstances();
    int initialActiveProcessInstanceSize = processInstances.size();
    // prepare variable value
    String variableValue = "very short value that should be trimmed by custom variable log length";
    // 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);
    params.put("s", variableValue);
    long processInstanceId = session.startProcess("com.sample.ruleflow3", params).getId();
    int numActiveProcesses = auditLogService.findActiveProcessInstances().size();
    Assertions.assertThat(numActiveProcesses).isEqualTo(initialActiveProcessInstanceSize + 1).withFailMessage("find active processes did not work");
    // Test findVariableInstancesByName* methods: check for variables (only) in active processes
    List<VariableInstanceLog> varLogs = auditLogService.findVariableInstancesByName("s", true);
    varLogs = varLogs.stream().sorted((o1, o2) -> Long.compare(o1.getId(), o2.getId())).collect(Collectors.toList());
    Assertions.assertThat(varLogs).isNotEmpty();
    Assertions.assertThat(varLogs.size()).isEqualTo(2);
    Assertions.assertThat(varLogs).flatExtracting(VariableInstanceLog::getValue).containsExactly("InitialValue", variableValue.substring(0, 15));
    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(12);
    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 50 with WorkItemManager

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

the class UIWorkItemHandler method complete.

public void complete(WorkItem workItem, Map<String, Object> results) {
    WorkItemManager manager = workItems.get(workItem);
    if (manager != null) {
        manager.completeWorkItem(workItem.getId(), results);
        workItems.remove(workItem);
        reloadWorkItemsList();
    }
    selectButton.setEnabled(getSelectedWorkItem() != null);
}
Also used : WorkItemManager(org.kie.api.runtime.process.WorkItemManager)

Aggregations

WorkItemManager (org.kie.api.runtime.process.WorkItemManager)100 Test (org.junit.Test)84 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)55 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)23 KieBase (org.kie.api.KieBase)21 SystemOutWorkItemHandler (org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler)20 KieSession (org.kie.api.runtime.KieSession)14 Map (java.util.Map)9 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)8 DefaultWorkItemManager (org.drools.core.process.instance.impl.DefaultWorkItemManager)7 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