use of io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl in project automatiko-engine by automatiko-io.
the class WorkItemNodeInstance method handleWorkItemHandlerException.
/*
* Work item handler exception handling
*/
private void handleWorkItemHandlerException(ProcessWorkItemHandlerException handlerException, WorkItem workItem) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("ProcessInstanceId", workItem.getProcessInstanceId());
parameters.put("WorkItemId", workItem.getId());
parameters.put("NodeInstanceId", this.getId());
parameters.put("ErrorMessage", handlerException.getMessage());
parameters.put("Error", handlerException);
// add all parameters of the work item to the newly started process instance
parameters.putAll(workItem.getParameters());
ProcessInstance processInstance = (ProcessInstance) getProcessInstance().getProcessRuntime().createProcessInstance(handlerException.getProcessId(), parameters);
this.exceptionHandlingProcessInstanceId = processInstance.getId();
((ProcessInstanceImpl) processInstance).setMetaData("ParentProcessInstanceId", getProcessInstance().getId());
((ProcessInstanceImpl) processInstance).setMetaData("ParentNodeInstanceId", getUniqueId());
processInstance.setParentProcessInstanceId(getProcessInstance().getId());
processInstance.setSignalCompletion(true);
getProcessInstance().getProcessRuntime().startProcessInstance(processInstance.getId());
if (processInstance.getState() == STATE_COMPLETED || processInstance.getState() == STATE_ABORTED) {
exceptionHandlingCompleted(processInstance, handlerException);
} else {
addExceptionProcessListener();
}
}
use of io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl in project automatiko-engine by automatiko-io.
the class ProcessInstanceResolverStrategy method connectProcessInstanceToRuntimeAndProcess.
/**
* Fill the process instance .kruntime and .process fields with the appropriate
* values.
*
* @param processInstance
* @param streamContext
*/
private void connectProcessInstanceToRuntimeAndProcess(ProcessInstance processInstance, Object streamContext) {
ProcessInstanceImpl processInstanceImpl = (ProcessInstanceImpl) processInstance;
InternalProcessRuntime runtime = processInstanceImpl.getProcessRuntime();
// Attach the process if not present
if (processInstance.getProcess() == null) {
String processId = processInstance.getProcessId();
if (processId != null) {
Process process = runtime.getProcess(processId);
if (process != null) {
processInstanceImpl.setProcess(process);
}
}
}
}
use of io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl in project automatiko-engine by automatiko-io.
the class SubProcessNodeInstance method internalTrigger.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void internalTrigger(final NodeInstance from, String type) {
super.internalTrigger(from, type);
// if node instance was cancelled, abort
if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
return;
}
if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A SubProcess node only accepts default incoming connections!");
}
Map<String, Object> parameters = new HashMap<String, Object>();
for (Iterator<DataAssociation> iterator = getSubProcessNode().getInAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation mapping = iterator.next();
Object parameterValue = null;
if (mapping.getTransformation() != null) {
Transformation transformation = mapping.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(mapping));
}
} else {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(mapping.getSources().get(0));
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
parameterValue = evaluator.evaluate(mapping.getSources().get(0), new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
parameterValue = VariableUtil.resolveVariable(mapping.getSources().get(0), this);
if (parameterValue != null) {
parameters.put(mapping.getTarget(), parameterValue);
} else {
logger.error("Could not find variable scope for variable {}", mapping.getSources().get(0));
logger.error("when trying to execute SubProcess node {}", getSubProcessNode().getName());
logger.error("Continuing without setting parameter.");
}
}
}
}
if (parameterValue != null) {
parameters.put(mapping.getTarget(), parameterValue);
}
}
String processId = getSubProcessNode().getProcessId();
if (processId == null) {
// if process id is not given try with process name
processId = getSubProcessNode().getProcessName();
}
// resolve processId if necessary
Map<String, String> replacements = new HashMap<String, String>();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(processId);
while (matcher.find()) {
String paramName = matcher.group(1);
String replacementKey = paramName;
String defaultValue = "";
if (paramName.contains(":")) {
String[] items = paramName.split(":");
paramName = items[0];
defaultValue = items[1];
}
if (replacements.get(replacementKey) == null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
if (variableScopeInstance != null) {
Object variableValue = variableScopeInstance.getVariable(paramName);
String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
replacements.put(replacementKey, variableValueString);
} else {
try {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
Object variableValue = evaluator.evaluate(paramName, new NodeInstanceResolverFactory(this));
String variableValueString = variableValue == null ? defaultValue : variableValue.toString();
replacements.put(replacementKey, variableValueString);
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
logger.error("when trying to replace variable in processId for sub process {}", getNodeName());
logger.error("Continuing without setting process id.");
}
}
}
}
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
processId = processId.replace("#{" + replacement.getKey() + "}", replacement.getValue());
}
// start process instance
Process process = getProcessInstance().getProcessRuntime().getProcess(processId);
if (process == null) {
logger.error("Could not find process {}", processId);
logger.error("Aborting process");
((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED);
throw new RuntimeException("Could not find process " + processId);
} else {
ProcessRuntime kruntime = ((ProcessInstance) getProcessInstance()).getProcessRuntime();
if (getSubProcessNode().getMetaData("MICollectionInput") != null) {
// remove foreach input variable to avoid problems when running in variable
// strict mode
parameters.remove(getSubProcessNode().getMetaData("MICollectionInput"));
}
ProcessInstance processInstance = null;
if (((WorkflowProcessInstanceImpl) getProcessInstance()).getCorrelationKey() != null) {
// in case there is correlation key on parent instance pass it along to child so
// it can be easily correlated
// since correlation key must be unique for active instances it appends
// processId and timestamp
List<String> businessKeys = new ArrayList<String>();
businessKeys.add(((WorkflowProcessInstanceImpl) getProcessInstance()).getCorrelationKey());
businessKeys.add(processId);
businessKeys.add(String.valueOf(System.currentTimeMillis()));
CorrelationKey subProcessCorrelationKey = new StringCorrelationKey(businessKeys.stream().collect(Collectors.joining(":")));
processInstance = (ProcessInstance) ((CorrelationAwareProcessRuntime) kruntime).createProcessInstance(processId, subProcessCorrelationKey, parameters);
} else {
processInstance = (ProcessInstance) kruntime.createProcessInstance(processId, parameters);
}
this.processInstanceId = processInstance.getId();
((ProcessInstanceImpl) processInstance).setMetaData("ParentProcessInstanceId", getProcessInstance().getId());
((ProcessInstanceImpl) processInstance).setMetaData("ParentNodeInstanceId", getUniqueId());
((ProcessInstanceImpl) processInstance).setMetaData("ParentNodeId", getSubProcessNode().getUniqueId());
((ProcessInstanceImpl) processInstance).setParentProcessInstanceId(getProcessInstance().getId());
((ProcessInstanceImpl) processInstance).setRootProcessInstanceId(StringUtils.isEmpty(getProcessInstance().getRootProcessInstanceId()) ? getProcessInstance().getId() : getProcessInstance().getRootProcessInstanceId());
((ProcessInstanceImpl) processInstance).setRootProcessId(StringUtils.isEmpty(getProcessInstance().getRootProcessId()) ? getProcessInstance().getProcessId() : getProcessInstance().getRootProcessId());
((ProcessInstanceImpl) processInstance).setSignalCompletion(getSubProcessNode().isWaitForCompletion());
((ProcessInstanceImpl) processInstance).addChild(processInstance.getProcessId(), processInstance.getId());
kruntime.startProcessInstance(processInstance.getId());
if (!getSubProcessNode().isWaitForCompletion()) {
triggerCompleted();
} else if (processInstance.getState() == ProcessInstance.STATE_COMPLETED || processInstance.getState() == ProcessInstance.STATE_ABORTED) {
processInstanceCompleted(processInstance);
} else {
addProcessListener();
}
}
}
use of io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl in project automatiko-engine by automatiko-io.
the class LambdaSubProcessNodeInstance method processInstanceCompleted.
public void processInstanceCompleted(ProcessInstance processInstance) {
removeEventListeners();
String parentInstanceId = getProcessInstance().getId();
if (getProcessInstance().getParentProcessInstanceId() != null && !getProcessInstance().getParentProcessInstanceId().isEmpty()) {
parentInstanceId = getProcessInstance().getParentProcessInstanceId() + ":" + parentInstanceId;
}
((ProcessInstanceImpl) getProcessInstance()).removeChild(processInstance.getProcess().getId(), parentInstanceId + ":" + processInstance.getId());
handleOutMappings(processInstance);
if (processInstance.getState() == ProcessInstance.STATE_ABORTED) {
String faultName = processInstance.getOutcome() == null ? "" : processInstance.getOutcome();
// handle exception as sub process failed with error code
ExceptionScopeInstance exceptionScopeInstance = (ExceptionScopeInstance) resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, faultName);
if (exceptionScopeInstance != null) {
exceptionScopeInstance.handleException(this, faultName, processInstance.getFaultData());
if (getSubProcessNode() != null && !getSubProcessNode().isIndependent() && getSubProcessNode().isAbortParent()) {
cancel();
}
return;
} else if (getSubProcessNode() != null && !getSubProcessNode().isIndependent() && getSubProcessNode().isAbortParent()) {
getProcessInstance().setState(ProcessInstance.STATE_ABORTED, faultName);
return;
}
}
// handle dynamic subprocess
if (getNode() == null) {
setMetaData("NodeType", "SubProcessNode");
}
// if there were no exception proceed normally
triggerCompleted();
if (getProcessInstance().getProcess().getType().equals(Process.FUNCTION_FLOW_TYPE)) {
processInstance.getMetaData().put("ATK_FUNC_FLOW_NEXT", getProcessInstance().getMetaData().get("ATK_FUNC_FLOW_NEXT"));
processInstance.getMetaData().put("ATK_FUNC_FLOW_ID", getProcessInstance().getId());
}
}
use of io.automatiko.engine.workflow.base.instance.impl.ProcessInstanceImpl in project automatiko-engine by automatiko-io.
the class LambdaSubProcessNodeInstance method internalTrigger.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void internalTrigger(final NodeInstance from, String type) {
super.internalTrigger(from, type);
// if node instance was cancelled, abort
if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
return;
}
if (!io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A SubProcess node only accepts default incoming connections!");
}
ProcessContext context = new ProcessContext(getProcessInstance().getProcessRuntime());
context.setNodeInstance(this);
context.setProcessInstance(getProcessInstance());
SubProcessFactory subProcessFactory = getSubProcessNode().getSubProcessFactory();
Object o = subProcessFactory.bind(context);
io.automatiko.engine.api.workflow.ProcessInstance<?> processInstance = subProcessFactory.createInstance(o);
io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
String parentInstanceId = getProcessInstance().getId();
if (getProcessInstance().getParentProcessInstanceId() != null && !getProcessInstance().getParentProcessInstanceId().isEmpty()) {
parentInstanceId = getProcessInstance().getParentProcessInstanceId() + ":" + parentInstanceId;
}
((ProcessInstanceImpl) pi).setMetaData("ParentProcessInstanceId", parentInstanceId);
((ProcessInstanceImpl) pi).setMetaData("ParentNodeInstanceId", getUniqueId());
((ProcessInstanceImpl) pi).setMetaData("ParentNodeId", getSubProcessNode().getUniqueId());
((ProcessInstanceImpl) pi).setParentProcessInstanceId(parentInstanceId);
((ProcessInstanceImpl) pi).setRootProcessInstanceId(StringUtils.isEmpty(getProcessInstance().getRootProcessInstanceId()) ? getProcessInstance().getId() : getProcessInstance().getRootProcessInstanceId());
((ProcessInstanceImpl) pi).setRootProcessId(StringUtils.isEmpty(getProcessInstance().getRootProcessId()) ? getProcessInstance().getProcessId() : getProcessInstance().getRootProcessId());
((ProcessInstanceImpl) pi).setSignalCompletion(getSubProcessNode().isWaitForCompletion());
((ProcessInstanceImpl) pi).setReferenceFromRoot(getProcessInstance().getReferenceFromRoot());
processInstance.start();
this.processInstanceId = processInstance.id();
this.processInstanceName = processInstance.description();
subProcessFactory.unbind(context, processInstance.variables());
if (!getSubProcessNode().isWaitForCompletion()) {
triggerCompleted();
} else if (processInstance.status() == ProcessInstance.STATE_COMPLETED || processInstance.status() == ProcessInstance.STATE_ABORTED) {
triggerCompleted();
} else {
String subprocessInstanceId = parentInstanceId + ":" + processInstance.id();
((ProcessInstanceImpl) getProcessInstance()).addChild(processInstance.process().id(), subprocessInstanceId);
addProcessListener();
}
}
Aggregations