use of io.automatiko.engine.workflow.base.core.Process in project automatiko-engine by automatiko-io.
the class ProcessHandler method postProcessNodes.
private void postProcessNodes(ExecutableProcess process, NodeContainer container) {
List<String> eventSubProcessHandlers = new ArrayList<String>();
for (Node node : container.getNodes()) {
if (node instanceof StartNode) {
List<DataAssociation> associations = ((StartNode) node).getOutAssociations();
if (associations != null) {
for (DataAssociation da : associations) {
VariableScope scope = (VariableScope) process.getDefaultContext(VariableScope.VARIABLE_SCOPE);
Variable variable = scope.findVariable(da.getTarget());
if (variable != null) {
da.setTarget(variable.getName());
}
}
}
} else if (node instanceof StateNode) {
StateNode stateNode = (StateNode) node;
String condition = (String) stateNode.getMetaData("Condition");
stateNode.setCondition(context -> {
return (boolean) MVEL.executeExpression(condition, context.getProcessInstance().getVariables());
});
} else if (node instanceof NodeContainer) {
// prepare event sub process
if (node instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
Node[] nodes = eventSubProcessNode.getNodes();
for (Node subNode : nodes) {
// avoids cyclomatic complexity
if (subNode == null || !(subNode instanceof StartNode)) {
continue;
}
List<Trigger> triggers = ((StartNode) subNode).getTriggers();
if (triggers == null) {
continue;
}
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
eventSubProcessNode.addEvent((EventTypeFilter) filter);
String type = ((EventTypeFilter) filter).getType();
if (type.startsWith("Error-") || type.startsWith("Escalation")) {
String faultCode = (String) subNode.getMetaData().get("FaultCode");
String replaceRegExp = "Error-|Escalation-";
final String signalType = type;
ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getParentContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
((ContextContainer) eventSubProcessNode.getParentContainer()).addContext(exceptionScope);
((ContextContainer) eventSubProcessNode.getParentContainer()).setDefaultContext(exceptionScope);
}
String faultVariable = null;
if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
faultVariable = findVariable(trigger.getInAssociations().get(0).getSources().get(0), process.getVariableScope());
}
ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
ConsequenceAction action = new ConsequenceAction("java", "");
action.setMetaData("Action", new SignalProcessInstanceAction(signalType, faultVariable, SignalProcessInstanceAction.PROCESS_INSTANCE_SCOPE));
exceptionHandler.setAction(action);
exceptionHandler.setFaultVariable(faultVariable);
exceptionHandler.setRetryAfter((Integer) subNode.getMetaData().get("ErrorRetry"));
exceptionHandler.setRetryIncrement((Integer) subNode.getMetaData().get("ErrorRetryIncrement"));
if (subNode.getMetaData().get("ErrorRetryIncrementMultiplier") != null) {
exceptionHandler.setRetryIncrementMultiplier(((Number) subNode.getMetaData().get("ErrorRetryIncrementMultiplier")).floatValue());
}
exceptionHandler.setRetryLimit((Integer) subNode.getMetaData().get("ErrorRetryLimit"));
if (faultCode != null) {
String trimmedType = type.replaceFirst(replaceRegExp, "");
for (String error : trimmedType.split(",")) {
exceptionScope.setExceptionHandler(error, exceptionHandler);
eventSubProcessHandlers.add(error);
}
} else {
exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
}
} else if (type.equals("Compensation")) {
// 1. Find the parent sub-process to this event sub-process
NodeContainer parentSubProcess;
NodeContainer subProcess = eventSubProcessNode.getParentContainer();
Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
if (isForCompensationObj == null) {
eventSubProcessNode.setMetaData("isForCompensation", true);
logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
}
if (subProcess instanceof ExecutableProcess) {
// (instance)?!?
throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
}
parentSubProcess = ((Node) subProcess).getParentContainer();
// 2. The event filter (never fires, purely for dumping purposes) has
// already been added
// 3. Add compensation scope
String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
}
}
}
} else if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getConstraint() != null) {
String processId = ((ExecutableProcess) container).getId();
String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
EventTypeFilter eventTypeFilter = new EventTypeFilter();
eventTypeFilter.setType(type);
eventSubProcessNode.addEvent(eventTypeFilter);
eventSubProcessNode.addEvent("variableChanged");
((StartNode) subNode).setCondition(context -> {
return (boolean) MVEL.executeExpression(constraintTrigger.getConstraint(), context.getProcessInstance().getVariables());
});
}
}
}
}
// for( Node subNode : nodes)
}
postProcessNodes(process, (NodeContainer) node);
} else if (node instanceof EndNode) {
handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
} else if (node instanceof ActionNode) {
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
throw new IllegalArgumentException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
}
// handler
for (Node node : container.getNodes()) {
if (node instanceof FaultNode) {
FaultNode faultNode = (FaultNode) node;
if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
faultNode.setTerminateParent(false);
}
}
}
}
use of io.automatiko.engine.workflow.base.core.Process in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowFactory method createProcess.
public ExecutableProcess createProcess(Workflow workflow) {
ExecutableProcess process = new ServerlessExecutableProcess();
if (workflow.getId() != null && !workflow.getId().isEmpty()) {
process.setId(workflow.getId());
} else {
LOGGER.info("setting default id {}", DEFAULT_WORKFLOW_ID);
process.setId(DEFAULT_WORKFLOW_ID);
}
if (workflow.getName() != null && !workflow.getName().isEmpty()) {
process.setName(workflow.getName());
} else {
LOGGER.info("setting default name {}", DEFAULT_WORKFLOW_NAME);
process.setName(DEFAULT_WORKFLOW_NAME);
}
if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) {
process.setVersion(workflow.getVersion());
} else {
LOGGER.info("no workflow version found.");
}
if (workflow.getMetadata() != null && workflow.getMetadata().get("package") != null) {
process.setPackageName(workflow.getMetadata().get("package"));
} else {
process.setPackageName(DEFAULT_PACKAGE_NAME);
}
if (workflow.isKeepActive()) {
process.setAutoComplete(false);
process.setDynamic(true);
} else {
process.setAutoComplete(true);
}
process.setVisibility(DEFAULT_VISIBILITY);
if (workflow.getMetadata() != null) {
process.getMetaData().putAll(workflow.getMetadata());
}
if (workflow.getDescription() != null) {
process.setMetaData("Documentation", workflow.getDescription());
}
if (workflow.getConstants() != null) {
Constants constants = workflow.getConstants();
String value = constants.getConstantsDef().toString();
Variable constantsVariable = new Variable("contantsVariable", "$CONST", new JsonNodeDataType());
constantsVariable.setMetaData("value", value.replaceAll("\"", "\\\""));
process.getVariableScope().addVariable(constantsVariable);
}
if (workflow.getAnnotations() != null) {
List<TagDefinition> tagDefinitions = new ArrayList<TagDefinition>();
int counter = 0;
for (String tag : workflow.getAnnotations()) {
if (tag.startsWith("${")) {
tagDefinitions.add(new FunctionTagDefinition(String.valueOf(++counter), unwrapExpression(tag), (exp, vars) -> {
Object result = ServerlessFunctions.expression(vars, exp);
if (result instanceof TextNode) {
return ((TextNode) result).asText();
}
return result.toString();
}));
} else {
tagDefinitions.add(new StaticTagDefinition(String.valueOf(++counter), tag));
}
}
((Process) process).setTagDefinitions(tagDefinitions);
}
return process;
}
use of io.automatiko.engine.workflow.base.core.Process in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method evaluateTags.
public Collection<Tag> evaluateTags() {
if (this.tags == null) {
this.tags = new LinkedHashSet<Tag>();
}
Collection<Tag> evaluatedTags = new LinkedHashSet<Tag>();
Collection<TagDefinition> tagDefinitions = ((Process) getProcess()).getTagDefinitions();
for (TagDefinition def : tagDefinitions) {
String tag = def.get(this, getVariables());
if (tag != null) {
Tag tagInstance = new TagInstance(def.getId(), tag);
evaluatedTags.add(tagInstance);
this.tags.remove(tagInstance);
}
}
// append all remaining tasks that didn't have definition - added manually on the instance
evaluatedTags.addAll(this.tags);
// replace existing ones
this.tags = evaluatedTags;
return evaluatedTags;
}
use of io.automatiko.engine.workflow.base.core.Process in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method signalEvent.
@Override
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
synchronized (this) {
if (getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
InternalProcessRuntime processRuntime = getProcessRuntime();
processRuntime.getProcessEventSupport().fireBeforeProcessSignaled(type, event, this, processRuntime);
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId().equals(slaTimerId)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<>();
List<EventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
if (!type.startsWith("Compensation")) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface && ((EventNodeInterface) node).acceptsEvent(type, event, getResolver(node, currentView))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
if (node instanceof EventSubProcessNode && (resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
} else if (node instanceof StartNode && ((StartNode) node).getTriggers() != null) {
boolean accepted = ((StartNode) node).getTriggers().stream().filter(EventTrigger.class::isInstance).anyMatch(t -> ((EventTrigger) t).getEventFilters().stream().anyMatch(e -> e.acceptsEvent(type, event)));
if (accepted && node.getMetaData().get("acceptStartSignal") != null) {
StartNodeInstance startNodeInstance = (StartNodeInstance) getNodeInstance(node);
startNodeInstance.signalEvent(type, event);
}
}
}
if (((io.automatiko.engine.workflow.process.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node.hasMatchingEventListner(type) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (nodeInstance != null) {
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>(getVariables());
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else if (event instanceof WorkflowProcessInstance) {
// ignore variables of process instance type
} else {
dynamicParams.put("Data", event);
}
nodeInstance.setDynamicParameters(dynamicParams);
}
nodeInstance.trigger(null, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
}
} else if (this instanceof ExecutableProcessInstance && node instanceof CompositeNode) {
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> ni.getNodeId() == node.getId()).findFirst();
instance.ifPresent(n -> ((CompositeNodeInstance) n).signalEvent(type, event));
}
}
}
}
} finally {
processRuntime.getProcessEventSupport().fireAfterProcessSignaled(type, event, this, processRuntime);
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
use of io.automatiko.engine.workflow.base.core.Process in project automatiko-engine by automatiko-io.
the class ProcessHandler method processTags.
protected void processTags(WorkflowProcess process) {
String tags = (String) process.getMetaData().get("tags");
List<TagDefinition> tagDefinitions = new ArrayList<TagDefinition>();
if (tags != null) {
String[] tagList = tags.split(",");
int counter = 0;
for (String tag : tagList) {
boolean isExpression = PatternConstants.PARAMETER_MATCHER.matcher(tag).matches();
if (isExpression) {
tagDefinitions.add(new FunctionTagDefinition(String.valueOf(++counter), tag, (exp, vars) -> {
Map<String, Object> replacements = new HashMap<>();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(exp);
while (matcher.find()) {
String paramName = matcher.group(1);
Object value = MVEL.executeExpression(exp, vars.getVariables());
replacements.put(paramName, value);
}
for (Map.Entry<String, Object> replacement : replacements.entrySet()) {
exp = exp.replace("#{" + replacement.getKey() + "}", replacement.getValue().toString());
}
return exp;
}));
} else {
tagDefinitions.add(new StaticTagDefinition(String.valueOf(++counter), tag));
}
}
}
((Process) process).setTagDefinitions(tagDefinitions);
}
Aggregations