use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceEventBatchTest method testMilestones.
@Test
public void testMilestones() {
ProcessInstanceEventBatch batch = new ProcessInstanceEventBatch(null, null);
WorkflowProcessInstance pi = mock(WorkflowProcessInstance.class);
when(pi.milestones()).thenReturn(null);
assertThat(batch.createMilestones(pi)).isNull();
when(pi.milestones()).thenReturn(emptyList());
assertThat(batch.createMilestones(pi)).isEmpty();
Milestone milestone = Milestone.builder().withId("id").withName("name").withStatus(Status.AVAILABLE).build();
when(pi.milestones()).thenReturn(singleton(milestone));
MilestoneEventBody milestoneEventBody = MilestoneEventBody.create().id("id").name("name").status(Status.AVAILABLE.name()).build();
assertThat(batch.createMilestones(pi)).containsOnly(milestoneEventBody);
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method unmarshallReadOnlyProcessInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
public ProcessInstance unmarshallReadOnlyProcessInstance(byte[] data, Process process) {
WorkflowProcessInstance wpi = unmarshallWorkflowProcessInstance(data, process);
Model model = ((AbstractProcess) process).createModel();
model.fromMap(wpi.getVariables());
return ((AbstractProcess) process).createReadOnlyInstance(wpi, model);
}
use of io.automatiko.engine.api.runtime.process.WorkflowProcessInstance 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.api.runtime.process.WorkflowProcessInstance 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.api.runtime.process.WorkflowProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method importWorkflowProcessInstance.
public WorkflowProcessInstance importWorkflowProcessInstance(String header, String data, List<Map<String, String>> timers, Process<?> process) {
Map<String, io.automatiko.engine.api.definition.process.Process> processes = new HashMap<String, io.automatiko.engine.api.definition.process.Process>();
io.automatiko.engine.api.definition.process.Process p = ((AbstractProcess<?>) process).process();
// this can include version number in the id
processes.put(process.id(), p);
// this is raw process id as defined in bpmn or so
processes.put(p.getId(), p);
try (ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0])) {
Map<String, Object> localEnv = new HashMap<String, Object>(env);
localEnv.put("_import_", true);
localEnv.put("_services_", ((AbstractProcess<?>) process).services());
AutomatikoMessages.ProcessInstance.Builder builder = AutomatikoMessages.ProcessInstance.newBuilder();
JsonFormat.parser().merge(data, builder);
AutomatikoMessages.Header.Builder headerBuilder = AutomatikoMessages.Header.newBuilder();
JsonFormat.parser().merge(header, headerBuilder);
MarshallerReaderContext context = new MarshallerReaderContext(bais, null, processes, localEnv) {
@Override
protected void readStreamHeader() throws IOException, StreamCorruptedException {
}
};
context.parameterObject = builder.build();
PersisterHelper.loadStrategiesIndex(context, headerBuilder.build());
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(builder.getProcessType());
WorkflowProcessInstance pi = (WorkflowProcessInstance) marshaller.readProcessInstance(context);
context.close();
return pi;
} catch (Exception e) {
throw new RuntimeException("Error while unmarshalling process instance", e);
}
}
Aggregations