use of io.automatiko.engine.workflow.process.core.node.EndNode in project automatiko-engine by automatiko-io.
the class FunctionFlowGenerator method generate.
public String generate() {
CompilationUnit clazz = parse(this.getClass().getResourceAsStream("/class-templates/FunctionFlowTemplate.java"));
clazz.setPackageDeclaration(process.getPackageName());
clazz.addImport(modelfqcn);
ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new IllegalStateException("Cannot find the class in FunctionFlowTemplate"));
template.setName(functionClazzName);
List<String> discoveredFunctions = new ArrayList<>();
// first to initiate the function flow
template.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("startTemplate")).ifPresent(md -> {
md.setName(processId.toLowerCase() + version);
md.getBody().get().findFirst(StringLiteralExpr.class, s -> s.getValue().equals("$TypePrefix$")).ifPresent(s -> s.setValue(process.getPackageName() + "." + processId + fversion));
if (useInjection()) {
String trigger = functionTrigger(process);
String filter = functionFilter(process);
if (filter != null) {
Matcher matcher = PARAMETER_MATCHER.matcher(filter);
while (matcher.find()) {
String paramName = matcher.group(1);
Optional<String> value = context.getApplicationProperty(paramName);
if (value.isPresent()) {
filter = filter.replaceAll("\\{" + paramName + "\\}", value.get());
} else {
throw new IllegalArgumentException("Missing argument declared in as function filter with name '" + paramName + "'. Define it in application.properties file");
}
}
}
annotator.withCloudEventMapping(md, trigger, filter);
}
});
MethodDeclaration callTemplate = template.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("callTemplate")).get();
discoveredFunctions.add(definedFunctionTrigger(process));
// for each "execution" node add new function
for (Node node : process.getNodesRecursively()) {
if (isExecutionNode(node)) {
discoveredFunctions.add(definedFunctionTrigger(node));
MethodDeclaration flowStepFunction = callTemplate.clone();
if (useInjection()) {
String trigger = functionTrigger(node);
String filter = functionFilter(node);
if (filter != null) {
Matcher matcher = PARAMETER_MATCHER.matcher(filter);
while (matcher.find()) {
String paramName = matcher.group(1);
Optional<String> value = context.getApplicationProperty(paramName);
if (value.isPresent() && !value.get().isEmpty()) {
filter = filter.replaceAll("\\{" + paramName + "\\}", value.get());
} else {
throw new IllegalArgumentException("Missing argument declared in as function filter with name '" + paramName + "'. Define it in application.properties file");
}
}
}
annotator.withCloudEventMapping(flowStepFunction, trigger, filter);
}
flowStepFunction.getBody().get().findFirst(StringLiteralExpr.class, s -> s.getValue().equals("$StartFromNode$")).ifPresent(s -> s.setValue((String) node.getMetaData().get("UniqueId")));
flowStepFunction.getBody().get().findFirst(StringLiteralExpr.class, s -> s.getValue().equals("$TypePrefix$")).ifPresent(s -> s.setValue(process.getPackageName() + "." + processId + fversion + "."));
flowStepFunction.getBody().get().findFirst(StringLiteralExpr.class, s -> s.getValue().equals("$ThisNode$")).ifPresent(s -> s.setValue(node.getName()));
flowStepFunction.setName(sanitizeIdentifier(node.getName() + version).toLowerCase());
template.addMember(flowStepFunction);
} else if (node instanceof EndNode || node instanceof FaultNode) {
discoveredFunctions.add(definedFunctionTrigger(node));
}
}
// remove the template method
callTemplate.removeForced();
// process signals
MethodDeclaration signalTemplate = template.findFirst(MethodDeclaration.class, md -> md.getNameAsString().equals("signalTemplate")).get();
Optional.ofNullable(signals).ifPresent(signalsMap -> {
AtomicInteger index = new AtomicInteger(0);
signalsMap.entrySet().stream().filter(e -> Objects.nonNull(e.getKey())).forEach(entry -> {
String signalName = entry.getKey();
String signalType = entry.getValue();
MethodDeclaration flowSignalFunction = produceSignalFunction("", signalName, signalType, signalTemplate, index, signalNodes.get(signalName));
template.addMember(flowSignalFunction);
});
});
if (triggers != null && !triggers.isEmpty()) {
AtomicInteger index = new AtomicInteger(0);
for (TriggerMetaData trigger : triggers) {
if (trigger.getType().equals(TriggerMetaData.TriggerType.ConsumeMessage)) {
String signalName = trigger.getName();
String signalType = trigger.getDataType();
MethodDeclaration flowSignalFunction = produceSignalFunction("Message-", signalName, signalType, signalTemplate, index, (Node) trigger.getContext("_node_"));
VariableDeclarationExpr correlationVar = flowSignalFunction.findFirst(VariableDeclarationExpr.class, vd -> vd.getVariable(0).getNameAsString().equals("correlation")).get();
if (trigger.getCorrelation() != null) {
correlationVar.getVariable(0).setInitializer(new StringLiteralExpr(trigger.getCorrelation()));
} else if (trigger.getCorrelationExpression() != null) {
correlationVar.getVariable(0).setInitializer(new NameExpr(trigger.getCorrelationExpression()));
}
template.addMember(flowSignalFunction);
}
}
}
// remove the template method
signalTemplate.removeForced();
Map<String, String> typeInterpolations = new HashMap<>();
typeInterpolations.put("$Clazz$", functionClazzName);
typeInterpolations.put("$Type$", dataClazzName);
template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, typeInterpolations));
if (useInjection()) {
template.findAll(FieldDeclaration.class, CodegenUtils::isProcessField).forEach(fd -> annotator.withNamedInjection(fd, processId + version));
template.findAll(FieldDeclaration.class, CodegenUtils::isApplicationField).forEach(fd -> annotator.withInjection(fd));
template.findAll(FieldDeclaration.class, CodegenUtils::isIdentitySupplierField).forEach(fd -> annotator.withInjection(fd));
template.findAll(FieldDeclaration.class, CodegenUtils::isEventSourceField).forEach(fd -> annotator.withInjection(fd));
template.findAll(MethodDeclaration.class, md -> md.isPublic()).forEach(md -> annotator.withFunction(md));
}
NodeList<Expression> items = NodeList.nodeList(discoveredFunctions.stream().map(s -> new StringLiteralExpr(s)).collect(Collectors.toList()));
template.addAnnotation(new NormalAnnotationExpr(new Name(Generated.class.getCanonicalName()), NodeList.nodeList(new MemberValuePair("value", new ArrayInitializerExpr(items)), new MemberValuePair("reference", new StringLiteralExpr(context.getApplicationProperty("quarkus.google.cloud.project-id").orElse("missing"))), new MemberValuePair("name", new StringLiteralExpr(StringUtils.capitalize(ProcessToExecModelGenerator.extractProcessId(processId, version)))), new MemberValuePair("hidden", new BooleanLiteralExpr(false)))));
template.getMembers().sort(new BodyDeclarationComparator());
ImportsOrganizer.organize(clazz);
return clazz.toString();
}
use of io.automatiko.engine.workflow.process.core.node.EndNode in project automatiko-engine by automatiko-io.
the class DmnDecisionInProcessTest method createProcess.
private ExecutableProcess createProcess(String namespace, String modelName, String decisionName) {
DMNRuntime dmnRuntime = DmnRuntimeProvider.fromClassPath("PersonDecisions.dmn");
DmnDecisionModel dmnDecisionModel = new DmnDecisionModel(dmnRuntime, namespace, modelName);
ExecutableProcess process = new ExecutableProcess();
process.setId("process");
process.setName("Process");
List<Variable> variables = new ArrayList<Variable>();
Variable variable1 = new Variable();
variable1.setName("person");
variable1.setType(new ObjectDataType(Person.class));
variables.add(variable1);
Variable variable2 = new Variable();
variable2.setName("isAdult");
variable2.setType(new BooleanDataType());
variables.add(variable2);
process.getVariableScope().setVariables(variables);
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
RuleSetNode ruleSetNode = new RuleSetNode();
ruleSetNode.setName("RuleSetNode");
ruleSetNode.setId(2);
ruleSetNode.setRuleType(RuleSetNode.RuleType.decision(namespace, modelName, null));
ruleSetNode.setLanguage(RuleSetNode.DMN_LANG);
ruleSetNode.setDecisionModel(() -> dmnDecisionModel);
ruleSetNode.addInMapping("Person", "person");
ruleSetNode.addOutMapping("isAdult", "isAdult");
EndNode endNode = new EndNode();
endNode.setName("End");
endNode.setId(3);
connect(startNode, ruleSetNode);
connect(ruleSetNode, endNode);
process.addNode(startNode);
process.addNode(ruleSetNode);
process.addNode(endNode);
return process;
}
use of io.automatiko.engine.workflow.process.core.node.EndNode in project automatiko-engine by automatiko-io.
the class EndEventHandler method handleTerminateNode.
public void handleTerminateNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
((EndNode) node).setTerminate(true);
EndNode endNode = (EndNode) node;
org.w3c.dom.Node xmlNode = element.getFirstChild();
while (xmlNode != null) {
String nodeName = xmlNode.getNodeName();
if ("terminateEventDefinition".equals(nodeName)) {
String scope = ((Element) xmlNode).getAttribute("scope");
if ("process".equalsIgnoreCase(scope)) {
endNode.setScope(EndNode.PROCESS_SCOPE);
} else {
endNode.setScope(EndNode.CONTAINER_SCOPE);
}
}
xmlNode = xmlNode.getNextSibling();
}
}
use of io.automatiko.engine.workflow.process.core.node.EndNode in project automatiko-engine by automatiko-io.
the class EndEventHandler method createNode.
protected Node createNode(Attributes attrs) {
EndNode node = new EndNode();
node.setTerminate(false);
return node;
}
use of io.automatiko.engine.workflow.process.core.node.EndNode in project automatiko-engine by automatiko-io.
the class ServerlessWorkflowParser method addErrorHandlingToState.
protected void addErrorHandlingToState(Workflow workflow, State state, ServerlessWorkflowFactory factory, AtomicLong ids, WorkflowProcess process, CompositeContextNode subprocess) {
if (state.getOnErrors() != null) {
for (io.serverlessworkflow.api.error.Error error : state.getOnErrors()) {
List<ErrorDefinition> defs = new ArrayList<>();
if (error.getErrorRef() != null) {
workflow.getErrors().getErrorDefs().stream().filter(err -> err.getName().equals(error.getErrorRef())).forEach(err -> defs.add(err));
} else {
workflow.getErrors().getErrorDefs().stream().filter(err -> error.getErrorRefs().contains(err.getName())).forEach(err -> defs.add(err));
}
BoundaryEventNode errorNode = factory.errorBoundaryEventNode(ids.getAndIncrement(), defs, null, process, subprocess, workflow);
if (error.getEnd() != null) {
EndNode onErrorEnd = factory.endNode(ids.getAndIncrement(), state.getName() + "onErrorEnd", error.getEnd().isTerminate(), process);
if (error.getEnd().getProduceEvents() != null && !error.getEnd().getProduceEvents().isEmpty()) {
produceEvents(error.getEnd().getProduceEvents(), factory, workflow, ids, process, errorNode.getId(), onErrorEnd.getId());
} else {
factory.connect(errorNode.getId(), onErrorEnd.getId(), "connect_" + errorNode.getId() + "_" + onErrorEnd.getId(), process, false);
}
} else {
if (error.getTransition().getNextState() != null) {
for (io.automatiko.engine.api.definition.process.Node node : process.getNodes()) {
if (node.getName().equals(error.getTransition().getNextState())) {
if (error.getTransition().getProduceEvents() != null && !error.getTransition().getProduceEvents().isEmpty()) {
produceEvents(error.getTransition().getProduceEvents(), factory, workflow, ids, process, errorNode.getId(), node.getId());
} else {
factory.connect(errorNode.getId(), node.getId(), "connect_" + errorNode.getId() + "_" + node.getId(), process, false);
}
break;
}
}
}
}
}
}
}
Aggregations