Search in sources :

Example 1 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class DefaultExceptionScopeInstance method handleException.

public void handleException(ExceptionHandler handler, String exception, Object params) {
    if (handler instanceof ActionExceptionHandler) {
        ActionExceptionHandler exceptionHandler = (ActionExceptionHandler) handler;
        Action action = (Action) exceptionHandler.getAction().getMetaData("Action");
        try {
            ProcessInstance processInstance = getProcessInstance();
            ProcessContext processContext = new ProcessContext(processInstance.getKnowledgeRuntime());
            ContextInstanceContainer contextInstanceContainer = getContextInstanceContainer();
            if (contextInstanceContainer instanceof NodeInstance) {
                processContext.setNodeInstance((NodeInstance) contextInstanceContainer);
            } else {
                processContext.setProcessInstance(processInstance);
            }
            String faultVariable = exceptionHandler.getFaultVariable();
            if (faultVariable != null) {
                processContext.setVariable(faultVariable, params);
            }
            action.execute(processContext);
        } catch (Exception e) {
            throw new RuntimeException("unable to execute Action", e);
        }
    } else {
        throw new IllegalArgumentException("Unknown exception handler " + handler);
    }
}
Also used : Action(org.jbpm.process.instance.impl.Action) ProcessInstance(org.jbpm.process.instance.ProcessInstance) ContextInstanceContainer(org.jbpm.process.instance.ContextInstanceContainer) ActionExceptionHandler(org.jbpm.process.core.context.exception.ActionExceptionHandler) NodeInstance(org.jbpm.workflow.instance.NodeInstance) ProcessContext(org.drools.core.spi.ProcessContext)

Example 2 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method cancel.

public void cancel() {
    WorkItem workItem = getWorkItem();
    if (workItem != null && workItem.getState() != WorkItem.COMPLETED && workItem.getState() != WorkItem.ABORTED) {
        try {
            ((WorkItemManager) ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().getWorkItemManager()).internalAbortWorkItem(workItemId);
        } catch (WorkItemHandlerNotFoundException wihnfe) {
            getProcessInstance().setState(ProcessInstance.STATE_ABORTED);
            throw wihnfe;
        }
    }
    super.cancel();
}
Also used : WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.drools.core.process.instance.WorkItemManager)

Example 3 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method getContextInstance.

@Override
public ContextInstance getContextInstance(Context context) {
    ContextInstanceFactory conf = ContextInstanceFactoryRegistry.INSTANCE.getContextInstanceFactory(context);
    if (conf == null) {
        throw new IllegalArgumentException("Illegal context type (registry not found): " + context.getClass());
    }
    ContextInstance contextInstance = (ContextInstance) conf.getContextInstance(context, this, (ProcessInstance) getProcessInstance());
    if (contextInstance == null) {
        throw new IllegalArgumentException("Illegal context type (instance not found): " + context.getClass());
    }
    return contextInstance;
}
Also used : ContextInstance(org.jbpm.process.instance.ContextInstance) ProcessInstance(org.jbpm.process.instance.ProcessInstance) ContextInstanceFactory(org.jbpm.process.instance.impl.ContextInstanceFactory)

Example 4 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    super.internalTrigger(from, type);
    // if node instance was cancelled, abort
    if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
        return;
    }
    // TODO this should be included for ruleflow only, not for BPEL
    // if (!Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
    // throw new IllegalArgumentException(
    // "A WorkItemNode only accepts default incoming connections!");
    // }
    WorkItemNode workItemNode = getWorkItemNode();
    createWorkItem(workItemNode);
    if (workItemNode.isWaitForCompletion()) {
        addWorkItemListener();
    }
    String deploymentId = (String) getProcessInstance().getKnowledgeRuntime().getEnvironment().get(EnvironmentName.DEPLOYMENT_ID);
    ((WorkItem) workItem).setDeploymentId(deploymentId);
    ((WorkItem) workItem).setNodeInstanceId(this.getId());
    ((WorkItem) workItem).setNodeId(getNodeId());
    if (isInversionOfControl()) {
        ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().update(((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().getFactHandle(this), this);
    } else {
        try {
            ((WorkItemManager) ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime().getWorkItemManager()).internalExecuteWorkItem((org.drools.core.process.instance.WorkItem) workItem);
        } catch (WorkItemHandlerNotFoundException wihnfe) {
            getProcessInstance().setState(ProcessInstance.STATE_ABORTED);
            throw wihnfe;
        } catch (Exception e) {
            String exceptionName = e.getClass().getName();
            ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, exceptionName);
            if (exceptionScopeInstance == null) {
                throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
            }
            // workItemId must be set otherwise cancel activity will not find the right work item
            this.workItemId = workItem.getId();
            exceptionScopeInstance.handleException(exceptionName, e);
        }
    }
    if (!workItemNode.isWaitForCompletion()) {
        triggerCompleted();
    }
    this.workItemId = workItem.getId();
}
Also used : WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ProcessInstance(org.jbpm.process.instance.ProcessInstance) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemManager(org.drools.core.process.instance.WorkItemManager) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Example 5 with ProcessInstance

use of org.jbpm.process.instance.ProcessInstance in project jbpm by kiegroup.

the class WorkItemNodeInstance method triggerCompleted.

public void triggerCompleted(WorkItem workItem) {
    this.workItem = workItem;
    WorkItemNode workItemNode = getWorkItemNode();
    if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
        validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
        for (Iterator<DataAssociation> iterator = getWorkItemNode().getOutAssociations().iterator(); iterator.hasNext(); ) {
            DataAssociation association = iterator.next();
            if (association.getTransformation() != null) {
                Transformation transformation = association.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), workItem.getResults());
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("when trying to complete Work Item {}", workItem.getName());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        ((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = workItem.getResult(association.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new WorkItemResolverFactory(workItem));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                    DataType dataType = varDef.getType();
                    // exclude java.lang.Object as it is considered unknown type
                    if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    } else {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), value);
                    }
                    variableScopeInstance.setVariable(association.getTarget(), value);
                } else {
                    logger.warn("Could not find variable scope for variable {}", association.getTarget());
                    logger.warn("when trying to complete Work Item {}", workItem.getName());
                    logger.warn("Continuing without setting variable.");
                }
            } else {
                try {
                    for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
                        handleAssignment(it.next());
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    // handle dynamic nodes
    if (getNode() == null) {
        setMetaData("NodeType", workItem.getName());
        mapDynamicOutputData(workItem.getResults());
    }
    if (isInversionOfControl()) {
        KieRuntime kruntime = ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime();
        kruntime.update(kruntime.getFactHandle(this), this);
    } else {
        triggerCompleted();
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) Variable(org.jbpm.process.core.context.variable.Variable) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) KieRuntime(org.kie.api.runtime.KieRuntime) WorkItem(org.drools.core.process.instance.WorkItem) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) WorkItemResolverFactory(org.jbpm.workflow.instance.impl.WorkItemResolverFactory) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Iterator(java.util.Iterator) DataType(org.jbpm.process.core.datatype.DataType) ProcessInstance(org.jbpm.process.instance.ProcessInstance)

Aggregations

ProcessInstance (org.jbpm.process.instance.ProcessInstance)49 KieSession (org.kie.api.runtime.KieSession)34 StringReader (java.io.StringReader)33 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)33 Test (org.junit.Test)33 Reader (java.io.Reader)30 ArrayList (java.util.ArrayList)26 HashMap (java.util.HashMap)16 TestWorkItemHandler (org.jbpm.integrationtests.handler.TestWorkItemHandler)10 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 DroolsError (org.drools.compiler.compiler.DroolsError)6 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)6 Person (org.jbpm.integrationtests.test.Person)5 WorkItem (org.kie.api.runtime.process.WorkItem)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 KieRuntime (org.kie.api.runtime.KieRuntime)4 WorkItemHandlerNotFoundException (org.drools.core.WorkItemHandlerNotFoundException)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 WorkItem (org.drools.core.process.instance.WorkItem)3 Message (org.jbpm.integrationtests.test.Message)3