Search in sources :

Example 1 with ProcessContext

use of org.drools.core.spi.ProcessContext in project jbpm-work-items by kiegroup.

the class JavaHandlerWorkItemHandler method executeWorkItem.

@SuppressWarnings("unchecked")
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String className = (String) workItem.getParameter("Class");
    try {
        Class<JavaHandler> c = (Class<JavaHandler>) Class.forName(className);
        JavaHandler handler = c.newInstance();
        ProcessContext kcontext = new ProcessContext(ksession);
        WorkflowProcessInstance processInstance = (WorkflowProcessInstance) ksession.getProcessInstance(workItem.getProcessInstanceId());
        kcontext.setProcessInstance(processInstance);
        WorkItemNodeInstance nodeInstance = findNodeInstance(workItem.getId(), processInstance);
        kcontext.setNodeInstance(nodeInstance);
        Map<String, Object> results = handler.execute(kcontext);
        manager.completeWorkItem(workItem.getId(), results);
        return;
    } catch (ClassNotFoundException cnfe) {
        handleException(cnfe);
    } catch (InstantiationException ie) {
        handleException(ie);
    } catch (IllegalAccessException iae) {
        handleException(iae);
    }
}
Also used : WorkItemNodeInstance(org.jbpm.workflow.instance.node.WorkItemNodeInstance) ProcessContext(org.drools.core.spi.ProcessContext) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance)

Example 2 with ProcessContext

use of org.drools.core.spi.ProcessContext in project jbpm by kiegroup.

the class ReturnValueConstraintEvaluator method evaluate.

public boolean evaluate(NodeInstance instance, Connection connection, Constraint constraint) {
    Object value;
    try {
        ProcessContext context = new ProcessContext(((ProcessInstance) instance.getProcessInstance()).getKnowledgeRuntime());
        context.setNodeInstance(instance);
        value = this.evaluator.evaluate(context);
    } catch (Exception e) {
        throw new RuntimeException("unable to execute ReturnValueEvaluator: ", e);
    }
    if (!(value instanceof Boolean)) {
        throw new RuntimeException("Constraints must return boolean values: " + value + " for expression " + constraint);
    }
    return ((Boolean) value).booleanValue();
}
Also used : ProcessContext(org.drools.core.spi.ProcessContext) IOException(java.io.IOException)

Example 3 with ProcessContext

use of org.drools.core.spi.ProcessContext 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 4 with ProcessContext

use of org.drools.core.spi.ProcessContext in project jbpm by kiegroup.

the class ActionNodeInstance method internalTrigger.

public void internalTrigger(final NodeInstance from, String type) {
    if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
        throw new IllegalArgumentException("An ActionNode only accepts default incoming connections!");
    }
    Action action = (Action) getActionNode().getAction().getMetaData("Action");
    try {
        ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
        context.setNodeInstance(this);
        executeAction(action);
    } catch (WorkflowRuntimeException wre) {
        throw wre;
    } catch (Exception e) {
        // - or context.setNodeInstance(this)
        throw new WorkflowRuntimeException(this, getProcessInstance(), "Unable to execute Action: " + e.getMessage(), e);
    }
    triggerCompleted();
}
Also used : Action(org.jbpm.process.instance.impl.Action) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ProcessContext(org.drools.core.spi.ProcessContext) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException)

Example 5 with ProcessContext

use of org.drools.core.spi.ProcessContext in project jbpm by kiegroup.

the class NodeInstanceImpl method executeAction.

/**
 * This method is used in both instances of the {@link ExtendedNodeInstanceImpl}
 * and {@link ActionNodeInstance} instances in order to handle
 * exceptions thrown when executing actions.
 *
 * @param action An {@link Action} instance.
 */
protected void executeAction(Action action) {
    ProcessContext context = new ProcessContext(getProcessInstance().getKnowledgeRuntime());
    context.setNodeInstance(this);
    try {
        action.execute(context);
    } 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);
        }
        exceptionScopeInstance.handleException(exceptionName, e);
    }
}
Also used : WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ProcessContext(org.drools.core.spi.ProcessContext) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) ExceptionScopeInstance(org.jbpm.process.instance.context.exception.ExceptionScopeInstance)

Aggregations

ProcessContext (org.drools.core.spi.ProcessContext)10 Action (org.jbpm.process.instance.impl.Action)6 StringReader (java.io.StringReader)4 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 ActionDescr (org.drools.compiler.lang.descr.ActionDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)4 DroolsAction (org.jbpm.workflow.core.DroolsAction)4 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)4 ActionNode (org.jbpm.workflow.core.node.ActionNode)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 ArrayList (java.util.ArrayList)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)3 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)2 PackageBuildContext (org.drools.compiler.rule.builder.PackageBuildContext)2 MVELDialect (org.drools.compiler.rule.builder.dialect.mvel.MVELDialect)2