use of io.automatiko.engine.workflow.base.instance.context.exclusive.ExclusiveGroupInstance in project automatiko-engine by automatiko-io.
the class AbstractProtobufProcessInstanceMarshaller method writeNodeInstanceContent.
protected AutomatikoMessages.ProcessInstance.NodeInstanceContent writeNodeInstanceContent(AutomatikoMessages.ProcessInstance.NodeInstance.Builder _node, NodeInstance nodeInstance, MarshallerWriteContext context) throws IOException {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.Builder _content = null;
if (nodeInstance instanceof RuleSetNodeInstance) {
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.RULE_SET_NODE);
List<String> timerInstances = ((RuleSetNodeInstance) nodeInstance).getTimerInstances();
AutomatikoMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.Builder _ruleSet = AutomatikoMessages.ProcessInstance.NodeInstanceContent.RuleSetNode.newBuilder();
if (timerInstances != null) {
for (String id : timerInstances) {
_ruleSet.addTimerInstanceId(id);
}
}
_content.setRuleSet(_ruleSet.build());
} else if (nodeInstance instanceof HumanTaskNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.HumanTaskNode.Builder _task = AutomatikoMessages.ProcessInstance.NodeInstanceContent.HumanTaskNode.newBuilder().setWorkItemId(((HumanTaskNodeInstance) nodeInstance).getWorkItemId()).setWorkitem(writeHumanTaskWorkItem(context, (HumanTaskWorkItem) ((HumanTaskNodeInstance) nodeInstance).getWorkItem()));
List<String> timerInstances = ((HumanTaskNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_task.addTimerInstanceId(id);
}
}
if (((WorkItemNodeInstance) nodeInstance).getExceptionHandlingProcessInstanceId() != null) {
_task.setErrorHandlingProcessInstanceId(((HumanTaskNodeInstance) nodeInstance).getExceptionHandlingProcessInstanceId());
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.HUMAN_TASK_NODE).setHumanTask(_task.build());
} else if (nodeInstance instanceof WorkItemNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.WorkItemNode.Builder _wi = AutomatikoMessages.ProcessInstance.NodeInstanceContent.WorkItemNode.newBuilder().setWorkItemId(((WorkItemNodeInstance) nodeInstance).getWorkItemId()).setWorkitem(writeWorkItem(context, ((WorkItemNodeInstance) nodeInstance).getWorkItem()));
List<String> timerInstances = ((WorkItemNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_wi.addTimerInstanceId(id);
}
}
if (((WorkItemNodeInstance) nodeInstance).getExceptionHandlingProcessInstanceId() != null) {
_wi.setErrorHandlingProcessInstanceId(((WorkItemNodeInstance) nodeInstance).getExceptionHandlingProcessInstanceId());
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.WORK_ITEM_NODE).setWorkItem(_wi.build());
} else if (nodeInstance instanceof LambdaSubProcessNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.Builder _sp = AutomatikoMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.newBuilder().setProcessInstanceId(((LambdaSubProcessNodeInstance) nodeInstance).getProcessInstanceId()).setProcessInstanceName(((LambdaSubProcessNodeInstance) nodeInstance).getProcessInstanceName());
List<String> timerInstances = ((LambdaSubProcessNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_sp.addTimerInstanceId(id);
}
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.SUB_PROCESS_NODE).setSubProcess(_sp.build());
} else if (nodeInstance instanceof SubProcessNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.Builder _sp = AutomatikoMessages.ProcessInstance.NodeInstanceContent.SubProcessNode.newBuilder().setProcessInstanceId(((SubProcessNodeInstance) nodeInstance).getProcessInstanceId());
List<String> timerInstances = ((SubProcessNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_sp.addTimerInstanceId(id);
}
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.SUBPROCESS_NODE).setSubProcess(_sp.build());
} else if (nodeInstance instanceof MilestoneNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.MilestoneNode.Builder _ms = AutomatikoMessages.ProcessInstance.NodeInstanceContent.MilestoneNode.newBuilder();
List<String> timerInstances = ((MilestoneNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_ms.addTimerInstanceId(id);
}
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.MILESTONE_NODE).setMilestone(_ms.build());
} else if (nodeInstance instanceof EventNodeInstance) {
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.EVENT_NODE);
} else if (nodeInstance instanceof TimerNodeInstance) {
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.TIMER_NODE).setTimer(AutomatikoMessages.ProcessInstance.NodeInstanceContent.TimerNode.newBuilder().setTimerId(((TimerNodeInstance) nodeInstance).getTimerId()).build());
} else if (nodeInstance instanceof JoinInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.JoinNode.Builder _join = AutomatikoMessages.ProcessInstance.NodeInstanceContent.JoinNode.newBuilder();
Map<Long, Integer> triggers = ((JoinInstance) nodeInstance).getTriggers();
List<Long> keys = new ArrayList<Long>(triggers.keySet());
Collections.sort(keys, new Comparator<Long>() {
public int compare(Long o1, Long o2) {
return o1.compareTo(o2);
}
});
for (Long key : keys) {
_join.addTrigger(AutomatikoMessages.ProcessInstance.NodeInstanceContent.JoinNode.JoinTrigger.newBuilder().setNodeId(key).setCounter(triggers.get(key)).build());
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.JOIN_NODE).setJoin(_join.build());
} else if (nodeInstance instanceof StateNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.StateNode.Builder _state = AutomatikoMessages.ProcessInstance.NodeInstanceContent.StateNode.newBuilder();
List<String> timerInstances = ((StateNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_state.addTimerInstanceId(id);
}
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.STATE_NODE).setState(_state.build());
} else if (nodeInstance instanceof ForEachNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.ForEachNode.Builder _foreach = AutomatikoMessages.ProcessInstance.NodeInstanceContent.ForEachNode.newBuilder();
ForEachNodeInstance forEachNodeInstance = (ForEachNodeInstance) nodeInstance;
List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(forEachNodeInstance.getNodeInstances());
Collections.sort(nodeInstances, new Comparator<NodeInstance>() {
public int compare(NodeInstance o1, NodeInstance o2) {
return (int) (o1.getId().compareTo(o2.getId()));
}
});
for (NodeInstance subNodeInstance : nodeInstances) {
if (subNodeInstance instanceof CompositeContextNodeInstance) {
_foreach.addNodeInstance(writeNodeInstance(context, subNodeInstance));
}
}
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) forEachNodeInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScopeInstance != null) {
List<Map.Entry<String, Object>> variables = new ArrayList<Map.Entry<String, Object>>(variableScopeInstance.getVariables().entrySet());
Collections.sort(variables, new Comparator<Map.Entry<String, Object>>() {
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, Object> variable : variables) {
_foreach.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue(), false));
}
}
List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(forEachNodeInstance.getIterationLevels().entrySet());
Collections.sort(iterationlevels, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, Integer> level : iterationlevels) {
if (level.getKey() != null && level.getValue() != null) {
_foreach.addIterationLevels(AutomatikoMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
}
}
_foreach.setSequentialCounter(forEachNodeInstance.getSequentialCounter());
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(NodeInstanceType.FOR_EACH_NODE).setForEach(_foreach.build());
} else if (nodeInstance instanceof CompositeContextNodeInstance) {
AutomatikoMessages.ProcessInstance.NodeInstanceContent.CompositeContextNode.Builder _composite = AutomatikoMessages.ProcessInstance.NodeInstanceContent.CompositeContextNode.newBuilder();
AutomatikoMessages.ProcessInstance.NodeInstanceType _type = null;
if (nodeInstance instanceof DynamicNodeInstance) {
_type = AutomatikoMessages.ProcessInstance.NodeInstanceType.DYNAMIC_NODE;
} else if (nodeInstance instanceof EventSubProcessNodeInstance) {
_type = AutomatikoMessages.ProcessInstance.NodeInstanceType.EVENT_SUBPROCESS_NODE;
} else {
_type = AutomatikoMessages.ProcessInstance.NodeInstanceType.COMPOSITE_CONTEXT_NODE;
}
CompositeContextNodeInstance compositeNodeInstance = (CompositeContextNodeInstance) nodeInstance;
List<String> timerInstances = ((CompositeContextNodeInstance) nodeInstance).getTimerInstances();
if (timerInstances != null) {
for (String id : timerInstances) {
_composite.addTimerInstanceId(id);
}
}
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) compositeNodeInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScopeInstance != null) {
List<Map.Entry<String, Object>> variables = new ArrayList<Map.Entry<String, Object>>(variableScopeInstance.getVariables().entrySet());
Collections.sort(variables, new Comparator<Map.Entry<String, Object>>() {
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, Object> variable : variables) {
_composite.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue(), true));
}
}
List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(compositeNodeInstance.getIterationLevels().entrySet());
Collections.sort(iterationlevels, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, Integer> level : iterationlevels) {
if (level.getKey() != null && level.getValue() != null) {
_composite.addIterationLevels(AutomatikoMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
}
}
List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(compositeNodeInstance.getNodeInstances());
Collections.sort(nodeInstances, new Comparator<NodeInstance>() {
public int compare(NodeInstance o1, NodeInstance o2) {
return (int) (o1.getId().compareTo(o2.getId()));
}
});
for (NodeInstance subNodeInstance : nodeInstances) {
_composite.addNodeInstance(writeNodeInstance(context, subNodeInstance));
}
List<ContextInstance> exclusiveGroupInstances = compositeNodeInstance.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
if (exclusiveGroupInstances != null) {
for (ContextInstance contextInstance : exclusiveGroupInstances) {
AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance.Builder _excl = AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance.newBuilder();
ExclusiveGroupInstance exclusiveGroupInstance = (ExclusiveGroupInstance) contextInstance;
Collection<NodeInstance> groupNodeInstances = exclusiveGroupInstance.getNodeInstances();
for (NodeInstance groupNodeInstance : groupNodeInstances) {
_excl.addGroupNodeInstanceId(groupNodeInstance.getId());
}
_composite.addExclusiveGroup(_excl.build());
}
}
_content = AutomatikoMessages.ProcessInstance.NodeInstanceContent.newBuilder().setType(_type).setComposite(_composite.build());
} else {
throw new IllegalArgumentException("Unknown node instance type: " + nodeInstance);
}
return _content.build();
}
use of io.automatiko.engine.workflow.base.instance.context.exclusive.ExclusiveGroupInstance in project automatiko-engine by automatiko-io.
the class AbstractProtobufProcessInstanceMarshaller method readProcessInstance.
// Input methods
public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
AutomatikoMessages.ProcessInstance _instance = (io.automatiko.engine.workflow.marshalling.impl.AutomatikoMessages.ProcessInstance) context.parameterObject;
if (_instance == null) {
// try to parse from the stream
ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
Header _header;
try {
_header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
} catch (ClassNotFoundException e) {
// Java 5 does not accept [new IOException(String, Throwable)]
IOException ioe = new IOException("Error deserializing process instance.");
ioe.initCause(e);
throw ioe;
}
_instance = AutomatikoMessages.ProcessInstance.parseFrom(_header.getPayload(), registry);
}
WorkflowProcessInstanceImpl processInstance = createProcessInstance();
processInstance.setId(_instance.getId());
String processId = _instance.getProcessId();
processInstance.setProcessId(processId);
String processXml = _instance.getProcessXml();
Process process = null;
if (processXml != null && processXml.trim().length() > 0) {
processInstance.setProcessXml(processXml);
process = processInstance.getProcess();
} else {
process = context.processes.get(processId);
if (process == null) {
throw new RuntimeException("Could not find process " + processId + " when restoring process instance " + processInstance.getId());
}
processInstance.setProcess(process);
}
processInstance.setDescription(_instance.getDescription());
processInstance.internalSetState(_instance.getState());
processInstance.setParentProcessInstanceId(_instance.getParentProcessInstanceId());
processInstance.setRootProcessInstanceId(_instance.getRootProcessInstanceId());
processInstance.setRootProcessId(_instance.getRootProcessId());
processInstance.setSignalCompletion(_instance.getSignalCompletion());
processInstance.setInitiator(_instance.getInitiator());
processInstance.setCorrelationKey(_instance.getCorrelationKey());
processInstance.setStartDate(new Date(_instance.getStartDate()));
processInstance.internalSetSlaCompliance(_instance.getSlaCompliance());
if (_instance.getSlaDueDate() > 0) {
processInstance.internalSetSlaDueDate(new Date(_instance.getSlaDueDate()));
}
processInstance.internalSetSlaTimerId(_instance.getSlaTimerId());
processInstance.setProcessRuntime(context.getProcessRuntime());
List<ExecutionsErrorInfo> errors = new ArrayList<>();
for (io.automatiko.engine.workflow.marshalling.impl.AutomatikoMessages.ProcessInstance.Error error : _instance.getErrorsList()) {
errors.add(new ExecutionsErrorInfo(error.getErrorNodeId(), error.getErrorId(), error.getErrorMessage(), error.getErrorDetails()));
}
processInstance.internalSetExecutionErrors(errors);
processInstance.setReferenceId(_instance.getReferenceId());
processInstance.internalSetReferenceFromRoot(_instance.getReferenceFromRoot());
for (String completedNodeId : _instance.getCompletedNodeIdsList()) {
processInstance.addCompletedNodeId(completedNodeId);
}
if (_instance.getChildrenCount() > 0) {
_instance.getChildrenList().forEach(child -> processInstance.addChildren(child.getProcessId(), child.getIdsList()));
}
if (_instance.getTagsCount() > 0) {
_instance.getTagsList().forEach(tag -> processInstance.internalAddTag(tag.getId(), tag.getValue()));
}
if (_instance.getSwimlaneContextCount() > 0) {
Context swimlaneContext = ((io.automatiko.engine.workflow.base.core.Process) process).getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
for (AutomatikoMessages.ProcessInstance.SwimlaneContextInstance _swimlane : _instance.getSwimlaneContextList()) {
swimlaneContextInstance.setActorId(_swimlane.getSwimlane(), _swimlane.getActorId());
}
}
for (AutomatikoMessages.ProcessInstance.NodeInstance _node : _instance.getNodeInstanceList()) {
context.parameterObject = _node;
readNodeInstance(context, processInstance, processInstance);
}
if (processInstance.getState() == ProcessInstance.STATE_ACTIVE || processInstance.getState() == ProcessInstance.STATE_ERROR) {
for (AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance _excl : _instance.getExclusiveGroupList()) {
ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
for (String nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
NodeInstance nodeInstance = ((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) processInstance).getNodeInstance(nodeInstanceId, true);
if (nodeInstance == null) {
throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
}
exclusiveGroupInstance.addNodeInstance(nodeInstance);
}
}
}
readVariableScope(context, process, processInstance, _instance);
if (_instance.getIterationLevelsCount() > 0) {
for (AutomatikoMessages.IterationLevel _level : _instance.getIterationLevelsList()) {
processInstance.getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
return processInstance;
}
use of io.automatiko.engine.workflow.base.instance.context.exclusive.ExclusiveGroupInstance 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.base.instance.context.exclusive.ExclusiveGroupInstance in project automatiko-engine by automatiko-io.
the class SplitInstance method executeStrategy.
protected void executeStrategy(Split split, String type) {
// TODO make different strategies for each type
String uniqueId = (String) getNode().getMetaData().get(UNIQUE_ID);
if (uniqueId == null) {
uniqueId = ((NodeImpl) getNode()).getUniqueId();
}
switch(split.getType()) {
case Split.TYPE_AND:
triggerCompleted(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, true);
break;
case Split.TYPE_XOR:
List<Connection> outgoing = split.getDefaultOutgoingConnections();
int priority = Integer.MAX_VALUE;
Connection selected = null;
for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
final Connection connection = (Connection) iterator.next();
ConstraintEvaluator constraint = (ConstraintEvaluator) split.getConstraint(connection);
if (constraint != null && constraint.getPriority() < priority && !constraint.isDefault()) {
try {
if (constraint.evaluate(this, connection, constraint)) {
selected = connection;
priority = constraint.getPriority();
}
} catch (RuntimeException e) {
throw new RuntimeException("Exception when trying to evaluate constraint " + constraint.getName() + " in split " + split.getName(), e);
}
}
}
((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
if (selected == null) {
for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
final Connection connection = (Connection) iterator.next();
if (split.isDefault(connection)) {
selected = connection;
break;
}
}
}
if (selected == null) {
throw new IllegalArgumentException("XOR split could not find at least one valid outgoing connection for split " + getSplit().getName());
}
if (!hasLoop(selected.getTo(), split)) {
setLevel(1);
((NodeInstanceContainer) getNodeInstanceContainer()).setCurrentLevel(1);
}
triggerConnection(selected);
((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
break;
case Split.TYPE_OR:
((NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
outgoing = split.getDefaultOutgoingConnections();
boolean found = false;
List<NodeInstanceTrigger> nodeInstances = new ArrayList<NodeInstanceTrigger>();
List<Connection> outgoingCopy = new ArrayList<Connection>(outgoing);
while (!outgoingCopy.isEmpty()) {
priority = Integer.MAX_VALUE;
Connection selectedConnection = null;
ConstraintEvaluator selectedConstraint = null;
for (final Iterator<Connection> iterator = outgoingCopy.iterator(); iterator.hasNext(); ) {
final Connection connection = (Connection) iterator.next();
ConstraintEvaluator constraint = (ConstraintEvaluator) split.getConstraint(connection);
if (constraint != null && constraint.getPriority() < priority && !constraint.isDefault()) {
priority = constraint.getPriority();
selectedConnection = connection;
selectedConstraint = constraint;
}
}
if (selectedConstraint == null) {
break;
}
if (selectedConstraint.evaluate(this, selectedConnection, selectedConstraint)) {
nodeInstances.add(new NodeInstanceTrigger(followConnection(selectedConnection), selectedConnection.getToType()));
found = true;
}
outgoingCopy.remove(selectedConnection);
}
for (NodeInstanceTrigger nodeInstance : nodeInstances) {
// stop if this process instance has been aborted / completed
if (getProcessInstance().getState() == STATE_COMPLETED || getProcessInstance().getState() == STATE_ABORTED) {
return;
}
triggerNodeInstance(nodeInstance.getNodeInstance(), nodeInstance.getToType());
}
if (!found) {
for (final Iterator<Connection> iterator = outgoing.iterator(); iterator.hasNext(); ) {
final Connection connection = (Connection) iterator.next();
ConstraintEvaluator constraint = (ConstraintEvaluator) split.getConstraint(connection);
if (constraint != null && constraint.isDefault() || split.isDefault(connection)) {
triggerConnection(connection);
found = true;
break;
}
}
}
if (!found) {
throw new IllegalArgumentException("OR split could not find at least one valid outgoing connection for split " + getSplit().getName());
}
((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
break;
case Split.TYPE_XAND:
((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
Node node = getNode();
List<Connection> connections = null;
if (node != null) {
connections = node.getOutgoingConnections(type);
}
if (connections == null || connections.isEmpty()) {
((io.automatiko.engine.workflow.process.instance.NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, type);
} else {
ExclusiveGroupInstance groupInstance = new ExclusiveGroupInstance();
io.automatiko.engine.api.runtime.process.NodeInstanceContainer parent = getNodeInstanceContainer();
if (parent instanceof ContextInstanceContainer) {
((ContextInstanceContainer) parent).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, groupInstance);
} else {
throw new IllegalArgumentException("An Exclusive AND is only possible if the parent is a context instance container");
}
Map<io.automatiko.engine.workflow.process.instance.NodeInstance, String> nodeInstancesMap = new HashMap<io.automatiko.engine.workflow.process.instance.NodeInstance, String>();
for (Connection connection : connections) {
nodeInstancesMap.put(followConnection(connection), connection.getToType());
}
for (NodeInstance nodeInstance : nodeInstancesMap.keySet()) {
groupInstance.addNodeInstance(nodeInstance);
}
for (Map.Entry<io.automatiko.engine.workflow.process.instance.NodeInstance, String> entry : nodeInstancesMap.entrySet()) {
// stop if this process instance has been aborted / completed
if (getProcessInstance().getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
boolean hidden = false;
if (getNode().getMetaData().get("hidden") != null) {
hidden = true;
}
InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
if (!hidden) {
runtime.getProcessEventSupport().fireBeforeNodeLeft(this, runtime);
}
((io.automatiko.engine.workflow.process.instance.NodeInstance) entry.getKey()).trigger(this, entry.getValue());
if (!hidden) {
runtime.getProcessEventSupport().fireAfterNodeLeft(this, runtime);
}
}
}
((WorkflowProcessInstanceImpl) getProcessInstance()).addCompletedNodeId(uniqueId);
break;
default:
throw new IllegalArgumentException("Illegal split type " + split.getType());
}
}
use of io.automatiko.engine.workflow.base.instance.context.exclusive.ExclusiveGroupInstance in project automatiko-engine by automatiko-io.
the class AbstractProtobufProcessInstanceMarshaller method writeProcessInstance.
// Output methods
public AutomatikoMessages.ProcessInstance writeProcessInstance(MarshallerWriteContext context, ProcessInstance processInstance) throws IOException {
WorkflowProcessInstanceImpl workFlow = (WorkflowProcessInstanceImpl) processInstance;
AutomatikoMessages.ProcessInstance.Builder _instance = AutomatikoMessages.ProcessInstance.newBuilder().setId(workFlow.getId()).setProcessId(workFlow.getProcessId()).setState(workFlow.getState()).setProcessType(workFlow.getProcess().getType()).setSignalCompletion(workFlow.isSignalCompletion()).setSlaCompliance(workFlow.getSlaCompliance()).setStartDate(workFlow.getStartDate().getTime());
if (workFlow.getProcessXml() != null) {
_instance.setProcessXml(workFlow.getProcessXml());
}
if (workFlow.getDescription() != null) {
_instance.setDescription(workFlow.getDescription());
}
if (workFlow.getInitiator() != null) {
_instance.setInitiator(workFlow.getInitiator());
}
_instance.addAllCompletedNodeIds(workFlow.getCompletedNodeIds());
if (workFlow.getCorrelationKey() != null) {
_instance.setCorrelationKey(workFlow.getCorrelationKey());
}
if (workFlow.getSlaDueDate() != null) {
_instance.setSlaDueDate(workFlow.getSlaDueDate().getTime());
}
if (workFlow.getSlaTimerId() != null) {
_instance.setSlaTimerId(workFlow.getSlaTimerId());
}
if (workFlow.getParentProcessInstanceId() != null) {
_instance.setParentProcessInstanceId(workFlow.getParentProcessInstanceId());
}
if (workFlow.getRootProcessInstanceId() != null) {
_instance.setRootProcessInstanceId(workFlow.getRootProcessInstanceId());
}
if (workFlow.getRootProcessId() != null) {
_instance.setRootProcessId(workFlow.getRootProcessId());
}
if (workFlow.getReferenceFromRoot() != null) {
_instance.setReferenceFromRoot(workFlow.getReferenceFromRoot());
}
if (workFlow.getProcess().getVersion() != null) {
_instance.setProcessVersion(workFlow.getProcess().getVersion());
}
List<ExecutionsErrorInfo> errors = workFlow.errors();
if (errors != null) {
for (ExecutionsErrorInfo error : errors) {
_instance.addErrors(AutomatikoMessages.ProcessInstance.Error.newBuilder().setErrorNodeId(error.getFailedNodeId()).setErrorId(error.getErrorId()).setErrorMessage(error.getErrorMessage() == null ? "" : error.getErrorMessage()).setErrorDetails(error.getErrorDetails() == null ? "" : error.getErrorDetails()));
}
}
if (workFlow.getReferenceId() != null) {
_instance.setReferenceId(workFlow.getReferenceId());
}
Map<String, List<String>> children = workFlow.getChildren();
if (children != null) {
for (Entry<String, List<String>> entry : children.entrySet()) {
_instance.addChildren(AutomatikoMessages.ProcessInstance.ProcessInstanchChildren.newBuilder().setProcessId(entry.getKey()).addAllIds(entry.getValue()).build());
}
}
Collection<Tag> tags = workFlow.getTags();
if (tags != null) {
for (Tag tag : tags) {
_instance.addTags(AutomatikoMessages.ProcessInstance.Tag.newBuilder().setId(tag.getId()).setValue(tag.getValue()));
}
}
SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) workFlow.getContextInstance(SwimlaneContext.SWIMLANE_SCOPE);
if (swimlaneContextInstance != null) {
Map<String, String> swimlaneActors = swimlaneContextInstance.getSwimlaneActors();
for (Map.Entry<String, String> entry : swimlaneActors.entrySet()) {
_instance.addSwimlaneContext(AutomatikoMessages.ProcessInstance.SwimlaneContextInstance.newBuilder().setSwimlane(entry.getKey()).setActorId(entry.getValue()).build());
}
}
List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>(workFlow.getNodeInstances());
Collections.sort(nodeInstances, new Comparator<NodeInstance>() {
public int compare(NodeInstance o1, NodeInstance o2) {
return (int) (o1.getId().compareTo(o2.getId()));
}
});
for (NodeInstance nodeInstance : nodeInstances) {
_instance.addNodeInstance(writeNodeInstance(context, nodeInstance));
}
List<ContextInstance> exclusiveGroupInstances = workFlow.getContextInstances(ExclusiveGroup.EXCLUSIVE_GROUP);
if (exclusiveGroupInstances != null) {
for (ContextInstance contextInstance : exclusiveGroupInstances) {
AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance.Builder _exclusive = AutomatikoMessages.ProcessInstance.ExclusiveGroupInstance.newBuilder();
ExclusiveGroupInstance exclusiveGroupInstance = (ExclusiveGroupInstance) contextInstance;
Collection<NodeInstance> groupNodeInstances = exclusiveGroupInstance.getNodeInstances();
for (NodeInstance nodeInstance : groupNodeInstances) {
_exclusive.addGroupNodeInstanceId(nodeInstance.getId());
}
_instance.addExclusiveGroup(_exclusive.build());
}
}
if (!(boolean) context.env.getOrDefault("_ignore_vars_", false)) {
writeVariableScope(context, workFlow, _instance);
}
List<Map.Entry<String, Integer>> iterationlevels = new ArrayList<Map.Entry<String, Integer>>(workFlow.getIterationLevels().entrySet());
Collections.sort(iterationlevels, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
for (Map.Entry<String, Integer> level : iterationlevels) {
if (level.getValue() != null) {
_instance.addIterationLevels(AutomatikoMessages.IterationLevel.newBuilder().setId(level.getKey()).setLevel(level.getValue()));
}
}
return _instance.build();
}
Aggregations