use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl in project automatiko-engine by automatiko-io.
the class AbstractProtobufProcessInstanceMarshaller method readNodeInstanceContent.
protected NodeInstanceImpl readNodeInstanceContent(AutomatikoMessages.ProcessInstance.NodeInstance _node, MarshallerReaderContext context, WorkflowProcessInstance processInstance) throws IOException {
NodeInstanceImpl nodeInstance = null;
NodeInstanceContent _content = _node.getContent();
switch(_content.getType()) {
case RULE_SET_NODE:
nodeInstance = new RuleSetNodeInstance();
if (_content.getRuleSet().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getRuleSet().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((RuleSetNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case HUMAN_TASK_NODE:
nodeInstance = new HumanTaskNodeInstance();
((HumanTaskNodeInstance) nodeInstance).internalSetWorkItemId(_content.getHumanTask().getWorkItemId());
((HumanTaskNodeInstance) nodeInstance).internalSetWorkItem((WorkItemImpl) readHumanTaskWorkItem(processInstance, context, _content.getHumanTask().getWorkitem()));
if (_content.getHumanTask().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getHumanTask().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((HumanTaskNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
((WorkItemNodeInstance) nodeInstance).internalSetProcessInstanceId(_content.getHumanTask().getErrorHandlingProcessInstanceId());
break;
case WORK_ITEM_NODE:
nodeInstance = new WorkItemNodeInstance();
((WorkItemNodeInstance) nodeInstance).internalSetWorkItemId(_content.getWorkItem().getWorkItemId());
((WorkItemNodeInstance) nodeInstance).internalSetWorkItem((WorkItemImpl) readWorkItem(processInstance, context, _content.getWorkItem().getWorkitem()));
if (_content.getWorkItem().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getWorkItem().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((WorkItemNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
((WorkItemNodeInstance) nodeInstance).internalSetProcessInstanceId(_content.getWorkItem().getErrorHandlingProcessInstanceId());
break;
case SUBPROCESS_NODE:
nodeInstance = new SubProcessNodeInstance();
((SubProcessNodeInstance) nodeInstance).internalSetProcessInstanceId(_content.getSubProcess().getProcessInstanceId());
if (_content.getSubProcess().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getSubProcess().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((SubProcessNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case SUB_PROCESS_NODE:
nodeInstance = new LambdaSubProcessNodeInstance();
((LambdaSubProcessNodeInstance) nodeInstance).internalSetProcessInstanceId(_content.getSubProcess().getProcessInstanceId());
((LambdaSubProcessNodeInstance) nodeInstance).internalSetProcessInstanceName(_content.getSubProcess().getProcessInstanceName());
if (_content.getSubProcess().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getSubProcess().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((LambdaSubProcessNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case MILESTONE_NODE:
nodeInstance = new MilestoneNodeInstance();
if (_content.getMilestone().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getMilestone().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((MilestoneNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case TIMER_NODE:
nodeInstance = new TimerNodeInstance();
((TimerNodeInstance) nodeInstance).internalSetTimerId(_content.getTimer().getTimerId());
break;
case EVENT_NODE:
nodeInstance = new EventNodeInstance();
break;
case JOIN_NODE:
nodeInstance = new JoinInstance();
if (_content.getJoin().getTriggerCount() > 0) {
Map<Long, Integer> triggers = new HashMap<Long, Integer>();
for (AutomatikoMessages.ProcessInstance.NodeInstanceContent.JoinNode.JoinTrigger _join : _content.getJoin().getTriggerList()) {
triggers.put(_join.getNodeId(), _join.getCounter());
}
((JoinInstance) nodeInstance).internalSetTriggers(triggers);
}
break;
case FOR_EACH_NODE:
nodeInstance = new ForEachNodeInstance();
((ForEachNodeInstance) nodeInstance).setInternalSequentialCounter(_content.getForEach().getSequentialCounter());
break;
case COMPOSITE_CONTEXT_NODE:
nodeInstance = new CompositeContextNodeInstance();
if (_content.getComposite().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getComposite().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case DYNAMIC_NODE:
nodeInstance = new DynamicNodeInstance();
if (_content.getComposite().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getComposite().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case STATE_NODE:
nodeInstance = new StateNodeInstance();
if (_content.getState().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getState().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
case EVENT_SUBPROCESS_NODE:
nodeInstance = new EventSubProcessNodeInstance();
if (_content.getComposite().getTimerInstanceIdCount() > 0) {
List<String> timerInstances = new ArrayList<>();
for (String _timerId : _content.getComposite().getTimerInstanceIdList()) {
timerInstances.add(_timerId);
}
((CompositeContextNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
}
break;
default:
throw new IllegalArgumentException("Unknown node type: " + _content.getType());
}
return nodeInstance;
}
use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl in project automatiko-engine by automatiko-io.
the class AbstractProtobufProcessInstanceMarshaller method readNodeInstance.
public NodeInstance readNodeInstance(MarshallerReaderContext context, NodeInstanceContainer nodeInstanceContainer, WorkflowProcessInstance processInstance) throws IOException {
AutomatikoMessages.ProcessInstance.NodeInstance _node = (AutomatikoMessages.ProcessInstance.NodeInstance) context.parameterObject;
NodeInstanceImpl nodeInstance = readNodeInstanceContent(_node, context, processInstance);
nodeInstance.setNodeId(_node.getNodeId());
nodeInstance.setId(_node.getId());
nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
nodeInstance.setProcessInstance((io.automatiko.engine.workflow.process.instance.WorkflowProcessInstance) processInstance);
nodeInstance.setLevel(_node.getLevel() == 0 ? 1 : _node.getLevel());
nodeInstance.internalSetTriggerTime(new Date(_node.getTriggerDate()));
nodeInstance.internalSetSlaCompliance(_node.getSlaCompliance());
if (_node.getSlaDueDate() > 0) {
nodeInstance.internalSetSlaDueDate(new Date(_node.getSlaDueDate()));
}
nodeInstance.internalSetSlaTimerId(_node.getSlaTimerId());
nodeInstance.internalSetRetryJobId(_node.getRetryJobId());
nodeInstance.internalSetRetryAttempts(_node.getRetryAttempts());
switch(_node.getContent().getType()) {
case COMPOSITE_CONTEXT_NODE:
case DYNAMIC_NODE:
if (_node.getContent().getComposite().getVariableCount() > 0) {
Context variableScope = ((io.automatiko.engine.workflow.base.core.Process) ((io.automatiko.engine.workflow.base.instance.ProcessInstance) processInstance).getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) nodeInstance).getContextInstance(variableScope);
for (AutomatikoMessages.Variable _variable : _node.getContent().getComposite().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
}
if (_node.getContent().getComposite().getIterationLevelsCount() > 0) {
for (AutomatikoMessages.IterationLevel _level : _node.getContent().getComposite().getIterationLevelsList()) {
((CompositeContextNodeInstance) nodeInstance).getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
for (AutomatikoMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getComposite().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (CompositeContextNodeInstance) nodeInstance, processInstance);
}
for (AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance _excl : _node.getContent().getComposite().getExclusiveGroupList()) {
ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
((CompositeContextNodeInstance) nodeInstance).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
for (String nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
NodeInstance groupNodeInstance = ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) processInstance).getNodeInstance(nodeInstanceId, true);
if (groupNodeInstance == null) {
throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
}
exclusiveGroupInstance.addNodeInstance(groupNodeInstance);
}
}
break;
case FOR_EACH_NODE:
for (AutomatikoMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getForEach().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (ForEachNodeInstance) nodeInstance, processInstance);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ForEachNodeInstance) nodeInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
for (AutomatikoMessages.Variable _variable : _node.getContent().getForEach().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
if (_node.getContent().getForEach().getIterationLevelsCount() > 0) {
for (AutomatikoMessages.IterationLevel _level : _node.getContent().getForEach().getIterationLevelsList()) {
((ForEachNodeInstance) nodeInstance).getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
}
break;
case EVENT_SUBPROCESS_NODE:
for (AutomatikoMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getComposite().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (EventSubProcessNodeInstance) nodeInstance, processInstance);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((EventSubProcessNodeInstance) nodeInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
for (AutomatikoMessages.Variable _variable : _node.getContent().getComposite().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
}
break;
default:
}
return nodeInstance;
}
use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method importProcessInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance importProcessInstance(ExportedProcessInstance<?> instance, Process process) {
List<Map<String, String>> timers = instance.convertTimers();
WorkflowProcessInstance wpi = importWorkflowProcessInstance((String) instance.getHeader(), (String) instance.getInstance(), timers, process);
Model model = ((AbstractProcess) process).createModel();
model.fromMap(wpi.getVariables());
ProcessInstance processInstance = ((AbstractProcess) process).createInstance(wpi, model, 1);
if (timers != null && !timers.isEmpty()) {
String parentProcessInstanceId = wpi.getParentProcessInstanceId();
if (parentProcessInstanceId != null && !parentProcessInstanceId.isEmpty()) {
parentProcessInstanceId += ":";
} else {
parentProcessInstanceId = "";
}
JobsService jobService = ((WorkflowProcessInstanceImpl) wpi).getProcessRuntime().getJobsService();
Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) wpi).getNodeInstances(true);
// keeps iterator for statebased node instance timers
Map<String, Iterator<Timer>> nodeInstanceTimers = new LinkedHashMap<>();
for (Map<String, String> timer : timers) {
String nodeInstanceId = timer.get("nodeInstanceId");
for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
if (ni.getId().equals(nodeInstanceId)) {
ExpirationTime expirationTime = null;
long timerNodeId = 0;
if (((NodeInstanceImpl) ni).getRetryJobId() != null && ((NodeInstanceImpl) ni).getRetryJobId().equals(timer.get("timerId"))) {
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), ni.getNodeId(), "retry:" + ni.getId(), expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), ni.getProcessInstance().getRootProcessInstanceId(), ni.getProcessInstance().getProcessId(), ni.getProcessInstance().getProcess().getVersion(), ni.getProcessInstance().getRootProcessId()));
break;
} else if (ni instanceof TimerNodeInstance) {
TimerNodeInstance nodeInstance = (TimerNodeInstance) ni;
timerNodeId = nodeInstance.getTimerNode().getTimer().getId();
expirationTime = nodeInstance.createTimerInstance(nodeInstance.getTimerNode().getTimer());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
} else if (ni instanceof StateBasedNodeInstance) {
StateBasedNodeInstance nodeInstance = (StateBasedNodeInstance) ni;
if (nodeInstance.getTimerInstances().contains(timer.get("timerId"))) {
Iterator<Timer> it = nodeInstanceTimers.computeIfAbsent(nodeInstanceId, k -> nodeInstance.getEventBasedNode().getTimers().keySet().iterator());
expirationTime = nodeInstance.createTimerInstance(it.next());
expirationTime.reset(ZonedDateTime.parse(timer.get("scheduledAt")));
}
}
// lastly schedule timer on calculated expiration time
jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(timer.get("timerId"), timerNodeId, expirationTime, ((NodeInstanceImpl) ni).getProcessInstanceIdWithParent(), wpi.getRootProcessInstanceId(), wpi.getProcessId(), wpi.getProcess().getVersion(), wpi.getRootProcessId()));
break;
}
}
}
}
return processInstance;
}
use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl in project automatiko-engine by automatiko-io.
the class DefaultExceptionScopeInstance method handleException.
public void handleException(io.automatiko.engine.api.runtime.process.NodeInstance nodeInstance, ExceptionHandler handler, String exception, Object params) {
if (handler instanceof ActionExceptionHandler) {
ActionExceptionHandler exceptionHandler = (ActionExceptionHandler) handler;
if (retryAvailable(nodeInstance, exceptionHandler)) {
Integer retryAttempts = ((NodeInstanceImpl) nodeInstance).getRetryAttempts();
if (retryAttempts == null) {
retryAttempts = 1;
} else {
retryAttempts = retryAttempts + 1;
}
long delay = calculateDelay(exceptionHandler.getRetryAfter().longValue(), retryAttempts, exceptionHandler.getRetryIncrement(), exceptionHandler.getRetryIncrementMultiplier());
DurationExpirationTime expirationTime = DurationExpirationTime.after(delay);
JobsService jobService = getProcessInstance().getProcessRuntime().getJobsService();
String jobId = jobService.scheduleProcessInstanceJob(ProcessInstanceJobDescription.of(nodeInstance.getNodeId(), "retry:" + nodeInstance.getId(), expirationTime, ((NodeInstanceImpl) nodeInstance).getProcessInstanceIdWithParent(), getProcessInstance().getRootProcessInstanceId(), getProcessInstance().getProcessId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getRootProcessId()));
((NodeInstanceImpl) nodeInstance).internalSetRetryJobId(jobId);
((NodeInstanceImpl) nodeInstance).internalSetRetryAttempts(retryAttempts);
((NodeInstanceImpl) nodeInstance).registerRetryEventListener();
if (nodeInstance instanceof WorkItemNodeInstance) {
((WorkItemImpl) ((WorkItemNodeInstance) nodeInstance).getWorkItem()).setState(WorkItem.RETRYING);
}
} else {
Action action = (Action) exceptionHandler.getAction().getMetaData("Action");
try {
ProcessInstance processInstance = getProcessInstance();
ProcessContext processContext = new ProcessContext(processInstance.getProcessRuntime());
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);
}
}
use of io.automatiko.engine.workflow.process.instance.impl.NodeInstanceImpl in project automatiko-engine by automatiko-io.
the class JoinInstance method cancelRemainingDirectFlows.
private void cancelRemainingDirectFlows(NodeInstanceContainer nodeInstanceContainer, final Node lookFor) {
Collection<NodeInstance> activeNodeInstancesOrig = nodeInstanceContainer.getNodeInstances();
List<NodeInstance> activeNodeInstances = new ArrayList<NodeInstance>(activeNodeInstancesOrig);
// sort active instances in the way that lookFor nodeInstance will be last to
// not finish too early
Collections.sort(activeNodeInstances, new Comparator<NodeInstance>() {
@Override
public int compare(NodeInstance o1, NodeInstance o2) {
if (o1.getNodeId() == lookFor.getId()) {
return 1;
} else if (o2.getNodeId() == lookFor.getId()) {
return -1;
}
return 0;
}
});
for (NodeInstance nodeInstance : activeNodeInstances) {
// black box
if (((io.automatiko.engine.workflow.process.instance.NodeInstance) nodeInstance).getLevel() != getLevel()) {
continue;
}
Node node = nodeInstance.getNode();
Set<Long> vistedNodes = new HashSet<Long>();
checkNodes(vistedNodes, node, node, lookFor);
if (vistedNodes.contains(lookFor.getId()) && !vistedNodes.contains(node.getId())) {
((NodeInstanceImpl) nodeInstance).cancel();
}
}
}
Aggregations