use of io.automatiko.engine.workflow.base.core.TagDefinition in project automatiko-engine by automatiko-io.
the class ProcessVisitor method visitProcess.
public void visitProcess(WorkflowProcess process, MethodDeclaration processMethod, ProcessMetaData metadata, String workflowType) {
BlockStmt body = new BlockStmt();
ClassOrInterfaceType processFactoryType = new ClassOrInterfaceType(null, ExecutableProcessFactory.class.getSimpleName());
boolean serverless = ProcessToExecModelGenerator.isServerlessWorkflow(process);
// create local variable factory and assign new fluent process to it
VariableDeclarationExpr factoryField = new VariableDeclarationExpr(processFactoryType, FACTORY_FIELD_NAME);
MethodCallExpr assignFactoryMethod = new MethodCallExpr(new NameExpr(processFactoryType.getName().asString()), "createProcess");
assignFactoryMethod.addArgument(new StringLiteralExpr(process.getId())).addArgument(new StringLiteralExpr(workflowType)).addArgument(new BooleanLiteralExpr(serverless));
body.addStatement(new AssignExpr(factoryField, assignFactoryMethod, AssignExpr.Operator.ASSIGN));
// item definitions
Set<String> visitedVariables = new HashSet<>();
VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.base.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
visitVariableScope(variableScope, body, visitedVariables);
visitSubVariableScopes(process.getNodes(), body, visitedVariables);
Collection<TagDefinition> tagDefinitions = ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getTagDefinitions();
if (tagDefinitions != null) {
for (TagDefinition tag : tagDefinitions) {
if (tag instanceof FunctionTagDefinition) {
String expression = tag.getExpression();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(expression);
if (matcher.find()) {
expression = matcher.group(1);
}
BlockStmt actionBody = new BlockStmt();
if (serverless) {
MethodCallExpr evaluate = new MethodCallExpr(null, "expressionAsString").addArgument(new NameExpr("context")).addArgument(new NameExpr("exp"));
actionBody.addStatement(new ReturnStmt(evaluate));
} else {
List<Variable> variables = variableScope.getVariables();
variables.stream().filter(v -> tag.getExpression().contains(v.getName())).map(ActionNodeVisitor::makeAssignment).forEach(actionBody::addStatement);
actionBody.addStatement(new ReturnStmt(new NameExpr(expression)));
}
LambdaExpr lambda = new LambdaExpr(NodeList.nodeList(new Parameter(new UnknownType(), "exp"), new Parameter(new UnknownType(), "context")), actionBody);
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "tag", new StringLiteralExpr(tag.getId()), new StringLiteralExpr().setString(tag.getExpression()), lambda));
} else {
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "tag", new StringLiteralExpr(tag.getId()), new StringLiteralExpr(tag.getExpression()), new NullLiteralExpr()));
}
}
if (((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getImports() != null && !((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getImports().isEmpty()) {
NodeList<Expression> items = NodeList.nodeList(((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getImports().stream().map(s -> new StringLiteralExpr(s)).collect(Collectors.toList()));
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "imports", items.toArray(Expression[]::new)));
}
}
metadata.setDynamic(((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).isDynamic());
// the process itself
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_NAME, new StringLiteralExpr(process.getName()))).addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_PACKAGE_NAME, new StringLiteralExpr(process.getPackageName()))).addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_DYNAMIC, new BooleanLiteralExpr(metadata.isDynamic()))).addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VERSION, new StringLiteralExpr(getOrDefault(process.getVersion(), DEFAULT_VERSION)))).addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VISIBILITY, new StringLiteralExpr(getOrDefault(process.getVisibility(), WorkflowProcess.PUBLIC_VISIBILITY))));
visitMetaData(process.getMetaData(), body, FACTORY_FIELD_NAME);
visitHeader(process, body);
List<Node> processNodes = new ArrayList<>();
for (io.automatiko.engine.api.definition.process.Node procNode : process.getNodes()) {
processNodes.add((io.automatiko.engine.workflow.process.core.Node) procNode);
}
visitNodes(process, processNodes, body, variableScope, metadata);
visitConnections(process.getNodes(), body);
String timeout = (String) process.getMetaData().get("timeout");
if (timeout != null) {
String extraNodeIds = (String) process.getMetaData().get("timeoutNodes");
if (extraNodeIds != null) {
List<Expression> arguments = new ArrayList<>();
arguments.add(new IntegerLiteralExpr(process.getNodes().length));
arguments.add(new StringLiteralExpr(timeout));
arguments.addAll(Stream.of(extraNodeIds.split(",")).map(s -> new LongLiteralExpr(s)).collect(Collectors.toList()));
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_EXEC_TIMEOUT, arguments.toArray(Expression[]::new)));
} else {
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_EXEC_TIMEOUT, new IntegerLiteralExpr(process.getNodes().length), new StringLiteralExpr(timeout)));
}
}
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, METHOD_VALIDATE));
MethodCallExpr getProcessMethod = new MethodCallExpr(new NameExpr(FACTORY_FIELD_NAME), "getProcess");
body.addStatement(new ReturnStmt(getProcessMethod));
processMethod.setBody(body);
}
use of io.automatiko.engine.workflow.base.core.TagDefinition 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.TagDefinition 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.TagDefinition 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