use of io.automatiko.engine.api.definition.process.WorkflowProcess in project automatiko-engine by automatiko-io.
the class InputModelClassGenerator method generate.
public ModelMetaData generate() {
// create model class for all variables
String packageName = workFlowProcess.getPackageName();
modelMetaData = new ModelMetaData(workflowType, workFlowProcess.getId(), CodegenUtils.version(workFlowProcess.getVersion()), packageName, className, workFlowProcess.getVisibility(), VariableDeclarations.ofInput((VariableScope) ((io.automatiko.engine.workflow.base.core.Process) workFlowProcess).getDefaultContext(VariableScope.VARIABLE_SCOPE)), true, ProcessToExecModelGenerator.isServerlessWorkflow(workFlowProcess) ? "/class-templates/JsonModelTemplate.java" : "/class-templates/ModelNoIDTemplate.java", "Input data model for " + workFlowProcess.getName(), "Describes input data model expected by " + workFlowProcess.getName());
modelMetaData.setSupportsValidation(context.getBuildContext().isValidationSupported());
modelMetaData.setSupportsOpenApi(context.getBuildContext().isOpenApiSupported());
if (context.getApplicationProperty("quarkus.automatiko.target-deployment").orElse("unknown").equals("gcp-pubsub")) {
modelMetaData.addAugmentor(new GcpPubSubModelAugmentor());
}
if (context.getBuildContext().isGraphQLSupported()) {
modelMetaData.addAugmentor(new GraphQLModelAugmentor(true, null, context));
}
modelFileName = modelMetaData.getModelClassName().replace('.', '/') + ".java";
return modelMetaData;
}
use of io.automatiko.engine.api.definition.process.WorkflowProcess in project automatiko-engine by automatiko-io.
the class LambdaSubProcessNodeVisitor method visitNode.
@Override
public void visitNode(WorkflowProcess process, String factoryField, SubProcessNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
InputStream resourceAsStream = this.getClass().getResourceAsStream("/class-templates/SubProcessFactoryTemplate.java");
Optional<Expression> retValue = parse(resourceAsStream).findFirst(Expression.class);
String name = node.getName();
String subProcessId = node.getProcessId();
String subProcessVersion = ModelMetaData.version(node.getProcessVersion());
NodeValidator.of(getNodeKey(), name).notEmpty("subProcessId", subProcessId).validate();
body.addStatement(getAssignedFactoryMethod(factoryField, SubProcessNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Call Activity")).addStatement(getFactoryMethod(getNodeId(node), METHOD_PROCESS_ID, new StringLiteralExpr(subProcessId))).addStatement(getFactoryMethod(getNodeId(node), METHOD_PROCESS_NAME, new StringLiteralExpr(getOrDefault(node.getProcessName(), "")))).addStatement(getFactoryMethod(getNodeId(node), METHOD_PROCESS_VERSION, new StringLiteralExpr(getOrDefault(node.getProcessVersion(), "")))).addStatement(getFactoryMethod(getNodeId(node), METHOD_WAIT_FOR_COMPLETION, new BooleanLiteralExpr(node.isWaitForCompletion()))).addStatement(getFactoryMethod(getNodeId(node), METHOD_INDEPENDENT, new BooleanLiteralExpr(node.isIndependent())));
Map<String, String> inputTypes = (Map<String, String>) node.getMetaData("BPMN.InputTypes");
String subProcessModelClassName = ProcessToExecModelGenerator.extractModelClassName(subProcessId, subProcessVersion);
ModelMetaData subProcessModel = new ModelMetaData(process.getType(), subProcessId, subProcessVersion, metadata.getPackageName(), subProcessModelClassName, WorkflowProcess.PRIVATE_VISIBILITY, VariableDeclarations.ofRawInfo(inputTypes), false, "Data model for " + name, "A complete data model for " + name);
retValue.ifPresent(retValueExpression -> {
retValueExpression.findAll(ClassOrInterfaceType.class).stream().filter(t -> t.getNameAsString().equals("$Type$")).forEach(t -> t.setName(subProcessModelClassName));
retValueExpression.findFirst(MethodDeclaration.class, m -> m.getNameAsString().equals("bind")).ifPresent(m -> m.setBody(bind(variableScope, node, subProcessModel)));
retValueExpression.findFirst(MethodDeclaration.class, m -> m.getNameAsString().equals("createInstance")).ifPresent(m -> m.setBody(createInstance(node, metadata)));
retValueExpression.findFirst(MethodDeclaration.class, m -> m.getNameAsString().equals("unbind")).ifPresent(m -> m.setBody(unbind(variableScope, node)));
retValueExpression.findFirst(MethodDeclaration.class, m -> m.getNameAsString().equals("abortInstance")).ifPresent(m -> m.setBody(abortInstance(node, metadata)));
});
if (retValue.isPresent()) {
body.addStatement(getFactoryMethod(getNodeId(node), getNodeKey(), retValue.get()));
} else {
body.addStatement(getFactoryMethod(getNodeId(node), getNodeKey()));
}
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getDoneMethod(getNodeId(node)));
}
use of io.automatiko.engine.api.definition.process.WorkflowProcess 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.api.definition.process.WorkflowProcess in project automatiko-engine by automatiko-io.
the class ProcessToExecModelGeneratorTest method testScriptAndWorkItemModelGeneration.
@Test
public void testScriptAndWorkItemModelGeneration() {
ExecutableProcessFactory factory = ExecutableProcessFactory.createProcess("demo.orders");
factory.variable("order", new ObjectDataType(Integer.class)).variable("order", new ObjectDataType(List.class, "java.util.List<String>")).variable("approver", new ObjectDataType(String.class)).name("orders").packageName("com.myspace.demo").dynamic(false).version("1.0").workItemNode(1).name("Log").workName("Log").done().actionNode(2).name("Dump order").action("java", "System.out.println(\"Order has been created \" + order);").done().endNode(3).name("end").terminate(false).done().startNode(4).name("start").done().connection(2, 1).connection(4, 2).connection(1, 3);
Process process = factory.validate().getProcess();
ModelMetaData modelMetadata = ProcessToExecModelGenerator.INSTANCE.generateModel((WorkflowProcess) process);
assertNotNull(modelMetadata, "Dumper should return non null class for process");
logger.debug(modelMetadata.generate());
assertEquals("com.myspace.demo.Orders_1_0Model", modelMetadata.getModelClassName());
}
use of io.automatiko.engine.api.definition.process.WorkflowProcess in project automatiko-engine by automatiko-io.
the class ProcessCodegen method populateSubprocesses.
protected List<AbstractResourceGenerator> populateSubprocesses(WorkflowProcess parentProcess, ProcessMetaData metaData, Map<String, ProcessMetaData> processIdToMetadata, Map<String, ModelClassGenerator> processIdToModelGenerator, List<ProcessExecutableModelGenerator> processExecutableModelGenerators, Map<String, List<UserTaskModelMetaData>> processIdToUserTaskModel) {
List<AbstractResourceGenerator> subprocesses = new ArrayList<AbstractResourceGenerator>();
for (Entry<String, String> entry : metaData.getSubProcesses().entrySet()) {
ProcessExecutableModelGenerator execModelGen = processExecutableModelGenerators.stream().filter(p -> p.getProcessId().equals(entry.getValue())).findFirst().orElse(null);
if (execModelGen != null) {
WorkflowProcess workFlowProcess = execModelGen.process();
ModelClassGenerator modelClassGenerator = processIdToModelGenerator.get(entry.getValue());
Optional.of(new SubprocessResourceGenerator(context(), workFlowProcess, modelClassGenerator.className(), execModelGen.className(), applicationCanonicalName)).map(r -> r.withDependencyInjection(annotator).withParentProcess(parentProcess).withUserTasks(processIdToUserTaskModel.get(execModelGen.getProcessId())).withSignals(processIdToMetadata.get(execModelGen.getProcessId()).getSignals()).withTriggers(processIdToMetadata.get(execModelGen.getProcessId()).isStartable(), processIdToMetadata.get(execModelGen.getProcessId()).isDynamic()).withSubProcesses(populateSubprocesses(workFlowProcess, processIdToMetadata.get(execModelGen.getProcessId()), processIdToMetadata, processIdToModelGenerator, processExecutableModelGenerators, processIdToUserTaskModel))).ifPresent(subprocesses::add);
}
}
return subprocesses;
}
Aggregations