use of io.automatiko.engine.workflow.base.core.context.variable.VariableScope in project automatiko-engine by automatiko-io.
the class ActivityTest method testMinimalProcessMetaData.
@Test
public void testMinimalProcessMetaData() throws Exception {
final List<String> list1 = new ArrayList<String>();
final List<String> list2 = new ArrayList<String>();
final List<String> list3 = new ArrayList<String>();
final List<String> list4 = new ArrayList<String>();
DefaultProcessEventListener listener = new DefaultProcessEventListener() {
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
logger.debug("before node");
Map<String, Object> metaData = event.getNodeInstance().getNode().getMetaData();
for (Map.Entry<String, Object> entry : metaData.entrySet()) {
logger.debug(entry.getKey() + " " + entry.getValue());
}
String customTag = (String) metaData.get("customTag");
if (customTag != null) {
list1.add(customTag);
}
String customTag2 = (String) metaData.get("customTag2");
if (customTag2 != null) {
list2.add(customTag2);
}
}
public void afterVariableChanged(ProcessVariableChangedEvent event) {
logger.debug("after variable");
VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.base.core.impl.ProcessImpl) event.getProcessInstance().getProcess()).resolveContext(VariableScope.VARIABLE_SCOPE, event.getVariableId());
if (variableScope == null) {
return;
}
Map<String, Object> metaData = variableScope.findVariable(event.getVariableId()).getMetaData();
for (Map.Entry<String, Object> entry : metaData.entrySet()) {
logger.debug(entry.getKey() + " " + entry.getValue());
}
String customTag = (String) metaData.get("customTagVar");
if (customTag != null) {
list3.add(customTag);
}
}
public void afterProcessStarted(ProcessStartedEvent event) {
logger.debug("after process");
Map<String, Object> metaData = event.getProcessInstance().getProcess().getMetaData();
for (Map.Entry<String, Object> entry : metaData.entrySet()) {
logger.debug(entry.getKey() + " " + entry.getValue());
}
String customTag = (String) metaData.get("customTagProcess");
if (customTag != null) {
list4.add(customTag);
}
}
};
ProcessConfig config = config(listener);
BpmnProcess process = create(config, "BPMN2-MinimalProcessMetaData.bpmn2");
Map<String, Object> params = new HashMap<String, Object>();
params.put("x", "krisv");
ProcessInstance<BpmnVariables> instance = process.createInstance(BpmnVariables.create(params));
instance.start();
assertEquals(STATE_COMPLETED, instance.status());
assertEquals(3, list1.size());
assertEquals(2, list2.size());
assertEquals(1, list3.size());
assertEquals(1, list4.size());
}
use of io.automatiko.engine.workflow.base.core.context.variable.VariableScope in project automatiko-engine by automatiko-io.
the class ProcessInstanceManagementResource method getInstance.
@APIResponses(value = { @APIResponse(responseCode = "404", description = "In case of instance with given id was not found", content = @Content(mediaType = "application/json")), @APIResponse(responseCode = "200", description = "Process instance details", content = @Content(mediaType = "application/json")) })
@Operation(summary = "Returns process instance details for given instance id")
@SuppressWarnings("unchecked")
@GET
@Path("/{processId}/instances/{instanceId}")
@Produces(MediaType.APPLICATION_JSON)
public ProcessInstanceDetailsDTO getInstance(@Context UriInfo uriInfo, @Parameter(description = "Unique identifier of the process", required = true) @PathParam("processId") String processId, @Parameter(description = "Unique identifier of the instance", required = true) @PathParam("instanceId") String instanceId, @Parameter(description = "Status of the process instance", required = false, schema = @Schema(enumeration = { "active", "completed", "aborted", "error" })) @QueryParam("status") @DefaultValue("active") final String status, @Parameter(description = "User identifier as alternative autroization info", required = false, hidden = true) @QueryParam("user") final String user, @Parameter(description = "Groups as alternative autroization info", required = false, hidden = true) @QueryParam("group") final List<String> groups) {
try {
identitySupplier.buildIdentityProvider(user, groups);
return UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
Process<?> process = processData.get(processId);
Optional<ProcessInstance<?>> instance = (Optional<ProcessInstance<?>>) process.instances().findById(instanceId, mapStatus(status), ProcessInstanceReadMode.READ_ONLY);
if (instance.isEmpty()) {
throw new ProcessInstanceNotFoundException(instanceId);
}
ProcessInstance<?> pi = instance.get();
ProcessInstanceDetailsDTO details = new ProcessInstanceDetailsDTO();
String id = pi.id();
if (pi.parentProcessInstanceId() != null) {
id = pi.parentProcessInstanceId() + ":" + id;
}
details.setId(id);
details.setProcessId(processId);
details.setBusinessKey(pi.businessKey() == null ? "" : pi.businessKey());
details.setDescription(pi.description());
details.setState(pi.status());
details.setFailed(pi.errors().isPresent());
if (pi.errors().isPresent()) {
details.setErrors(pi.errors().get().errors().stream().map(e -> new ErrorInfoDTO(e.failedNodeId(), e.errorId(), e.errorMessage(), e.errorDetails())).collect(Collectors.toList()));
}
details.setImage(uriInfo.getBaseUri().toString() + "management/processes/" + processId + "/instances/" + instanceId + "/image?status=" + reverseMapStatus(pi.status()));
details.setTags(pi.tags().values());
details.setVariables(pi.variables());
details.setSubprocesses(pi.subprocesses().stream().map(spi -> new ProcessInstanceDTO(spi.id(), spi.businessKey(), spi.description(), spi.tags().values(), spi.errors().isPresent(), spi.process().id(), spi.status())).collect(Collectors.toList()));
VariableScope variableScope = (VariableScope) ((ContextContainer) ((AbstractProcess<?>) process).process()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
details.setVersionedVariables(variableScope.getVariables().stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(v -> v.getName()).collect(Collectors.toList()));
return details;
});
} finally {
IdentityProvider.set(null);
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.VariableScope in project automatiko-engine by automatiko-io.
the class StartNodeInstance method signalEvent.
public void signalEvent(String type, Object event) {
boolean hidden = false;
if (getNode().getMetaData().get(HIDDEN) != null) {
hidden = true;
}
InternalProcessRuntime runtime = getProcessInstance().getProcessRuntime();
if (!hidden) {
runtime.getProcessEventSupport().fireBeforeNodeTriggered(this, runtime);
}
if (event != null) {
String variableName = (String) getStartNode().getMetaData("TriggerMapping");
if (!getStartNode().getOutAssociations().isEmpty()) {
for (DataAssociation association : getStartNode().getOutAssociations()) {
// } else
if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
DataType dataType = varDef.getType();
// exclude java.lang.Object as it is considered unknown type
if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && event instanceof String) {
event = dataType.readValue((String) event);
} else {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), event);
}
variableScopeInstance.setVariable(this, association.getTarget(), event);
} else {
String output = association.getSources().get(0);
String target = association.getTarget();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
if (matcher.find()) {
String paramName = matcher.group(1);
String expression = VariableUtil.transformDotNotation(paramName, output);
NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
resolver.addExtraParameters(Collections.singletonMap(association.getSources().get(0), event));
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, resolver);
String varName = VariableUtil.nameFromDotNotation(paramName);
variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete start node {}", getStartNode().getName());
logger.warn("Continuing without setting variable.");
}
}
} else {
Object data = event;
association.getAssignments().stream().forEach(assignment -> handleAssignment(assignment, data));
}
}
} else {
if (variableName != null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
if (variableScopeInstance == null) {
throw new IllegalArgumentException("Could not find variable for start node: " + variableName);
}
EventTransformer transformer = getStartNode().getEventTransformer();
if (transformer != null) {
event = transformer.transformEvent(event);
}
variableScopeInstance.setVariable(this, variableName, event);
}
}
}
VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
for (Variable var : variableScope.getVariables()) {
if (var.getMetaData(Variable.DEFAULT_VALUE) != null && variableScopeInstance.getVariable(var.getName()) == null) {
Object value = runtime.getVariableInitializer().initialize(getProcessInstance().getProcess(), var, variableScopeInstance.getVariables());
variableScope.validateVariable(getProcessInstance().getProcess().getName(), var.getName(), value);
variableScopeInstance.setVariable(var.getName(), value);
}
}
triggerCompleted();
if (!hidden) {
runtime.getProcessEventSupport().fireAfterNodeTriggered(this, runtime);
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.VariableScope in project automatiko-engine by automatiko-io.
the class StateBasedNodeInstance method mapDynamicOutputData.
protected void mapDynamicOutputData(Map<String, Object> results) {
if (results != null && !results.isEmpty()) {
VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
for (Entry<String, Object> result : results.entrySet()) {
String variableName = result.getKey();
Variable variable = variableScope.findVariable(variableName);
if (variable != null) {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), variableName, result.getValue());
variableScopeInstance.setVariable(this, variableName, result.getValue());
}
}
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.VariableScope in project automatiko-engine by automatiko-io.
the class EventNodeInstance method getEventDescriptions.
@Override
public Set<EventDescription<?>> getEventDescriptions() {
NamedDataType dataType = null;
if (getEventNode().getVariableName() != null) {
Map<String, Object> dataOutputs = (Map<String, Object>) getEventNode().getMetaData().get("DataOutputs");
if (dataOutputs != null) {
for (Entry<String, Object> dOut : dataOutputs.entrySet()) {
dataType = new NamedDataType(dOut.getKey(), dOut.getValue());
}
} else {
VariableScope variableScope = (VariableScope) getEventNode().getContext(VariableScope.VARIABLE_SCOPE);
Variable variable = variableScope.findVariable(getEventNode().getVariableName());
dataType = new NamedDataType(variable.getName(), variable.getType());
}
}
return Collections.singleton(new BaseEventDescription(getEventType(), getNodeDefinitionId(), getNodeName(), "signal", getId(), getProcessInstance().getId(), dataType));
}
Aggregations