use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class EventSubProcessTest method testEventSignalSubProcessWithVersionedData.
@SuppressWarnings("unchecked")
@Test
public void testEventSignalSubProcessWithVersionedData() throws Exception {
Application app = generateCodeProcessesOnly("event-subprocess/EventSubprocessSignalWithVersionedData.bpmn2");
assertThat(app).isNotNull();
Process<? extends Model> p = app.processes().processById("EventSubprocessSignal_1");
Model m = p.createModel();
Map<String, Object> parameters = new HashMap<>();
m.fromMap(parameters);
ProcessInstance<?> processInstance = p.createInstance(m);
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
processInstance.send(Sig.of("MySignal", new Person("john", 20)));
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ACTIVE);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKeys("person");
Person person = (Person) result.toMap().get("person");
assertThat(person).isNotNull();
assertThat(person.getName()).isEqualTo("john");
assertThat(person.getAge()).isEqualTo(20);
processInstance.send(Sig.of("MySignal", new Person("john", 21)));
result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKeys("person");
person = (Person) result.toMap().get("person");
assertThat(person).isNotNull();
assertThat(person.getName()).isEqualTo("john");
assertThat(person.getAge()).isEqualTo(21);
processInstance.send(Sig.of("MySignal", new Person("john", 25)));
result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKeys("person");
person = (Person) result.toMap().get("person");
assertThat(person).isNotNull();
assertThat(person.getName()).isEqualTo("john");
assertThat(person.getAge()).isEqualTo(25);
Map<String, List<Object>> versions = (Map<String, List<Object>>) ((AbstractProcessInstance<?>) processInstance).processInstance().getVariable(VariableScope.VERSIONED_VARIABLES);
List<Object> personVersions = (List<Object>) versions.get("person");
assertThat(personVersions).hasSize(2);
processInstance.send(Sig.of("MySignal", new Person("john", 25)));
processInstance.abort();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED);
}
use of io.automatiko.engine.workflow.AbstractProcessInstance in project automatiko-engine by automatiko-io.
the class ProcessInstanceMarshaller method exportProcessInstance.
public ExportedProcessInstance<?> exportProcessInstance(ProcessInstance<?> processInstance) {
io.automatiko.engine.api.runtime.process.ProcessInstance pi = ((AbstractProcessInstance<?>) processInstance).internalGetProcessInstance();
if (pi == null) {
return null;
}
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Map<String, Object> localEnv = new HashMap<String, Object>(env);
localEnv.put("_export_", true);
ProcessMarshallerWriteContext context = new ProcessMarshallerWriteContext(baos, ((io.automatiko.engine.workflow.base.instance.ProcessInstance) pi).getProcessRuntime(), null, localEnv);
context.setProcessInstanceId(pi.getId());
context.setState(pi.getState());
String processType = pi.getProcess().getType();
context.stream.writeUTF(processType);
io.automatiko.engine.workflow.marshalling.impl.ProcessInstanceMarshaller marshaller = ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType);
Object result = marshaller.writeProcessInstance(context, pi);
AutomatikoMessages.Header.Builder _header = AutomatikoMessages.Header.newBuilder();
_header.setVersion(AutomatikoMessages.Version.newBuilder().setVersionMajor(1).setVersionMinor(0).setVersionRevision(0).build());
PersisterHelper.writeStrategiesIndex(context, _header);
String header = JsonFormat.printer().print(_header);
context.close();
// collect all information about timers
Collection<io.automatiko.engine.workflow.process.instance.NodeInstance> nodes = ((WorkflowProcessInstanceImpl) pi).getNodeInstances(true);
StringBuilder timers = new StringBuilder("[");
for (io.automatiko.engine.workflow.process.instance.NodeInstance ni : nodes) {
String timerId = null;
if (ni instanceof TimerNodeInstance) {
timerId = ((TimerNodeInstance) ni).getTimerId();
} else if (ni instanceof StateBasedNodeInstance) {
if (((StateBasedNodeInstance) ni).getTimerInstances() != null) {
((StateBasedNodeInstance) ni).getTimerInstances().forEach(timer -> {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timer);
timers.append("{\"timerId\":\"").append(timer).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
});
}
}
if (timerId != null) {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(timerId);
timers.append("{\"timerId\":\"").append(timerId).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
}
if (((NodeInstanceImpl) ni).getRetryJobId() != null && !((NodeInstanceImpl) ni).getRetryJobId().isEmpty()) {
ZonedDateTime scheduledTime = context.processRuntime.getJobsService().getScheduledTime(((NodeInstanceImpl) ni).getRetryJobId());
timers.append("{\"timerId\":\"").append(((NodeInstanceImpl) ni).getRetryJobId()).append("\",\"scheduledAt\":\"").append(scheduledTime.toString()).append("\",\"nodeInstanceId\":\"").append(ni.getId()).append("\"}");
}
}
timers.append("]");
return StringExportedProcessInstance.of(header, JsonFormat.printer().print((MessageOrBuilder) result), timers.toString(), null);
} catch (Exception e) {
throw new RuntimeException("Error while marshalling process instance", e);
}
}
use of io.automatiko.engine.workflow.AbstractProcessInstance 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