Search in sources :

Example 1 with ProcessMetaData

use of io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData in project automatiko-engine by automatiko-io.

the class GeneratorContext method collectRelatedProcesses.

protected void collectRelatedProcesses(Set<File> outcome, Set<ProcessMetaData> relatedProcesses) {
    // add all related process source files
    relatedProcesses.forEach(pm -> outcome.add(new File(pm.getSource())));
    // next check if any of the processes has subprocess
    for (ProcessMetaData metadata : relatedProcesses) {
        if (metadata.getSubProcesses() != null && !metadata.getSubProcesses().isEmpty()) {
            Set<ProcessMetaData> subpocesses = metadata.getSubProcesses().entrySet().stream().map(entry -> this.processes.get(entry.getKey())).collect(Collectors.toSet());
            collectRelatedProcesses(outcome, subpocesses);
        }
    }
}
Also used : StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) LoggerFactory(org.slf4j.LoggerFactory) NodeNotFoundException(io.automatiko.engine.api.workflow.NodeNotFoundException) HashMap(java.util.HashMap) NodeInstanceNotFoundException(io.automatiko.engine.api.workflow.NodeInstanceNotFoundException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProcessInstanceExecutionException(io.automatiko.engine.api.workflow.ProcessInstanceExecutionException) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) VariableNotFoundException(io.automatiko.engine.api.workflow.VariableNotFoundException) InvalidLifeCyclePhaseException(io.automatiko.engine.api.workflow.workitem.InvalidLifeCyclePhaseException) Map(java.util.Map) AccessDeniedException(io.automatiko.engine.api.auth.AccessDeniedException) NotAuthorizedException(io.automatiko.engine.api.workflow.workitem.NotAuthorizedException) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Properties(java.util.Properties) Logger(org.slf4j.Logger) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ApplicationBuildContext(io.automatiko.engine.codegen.context.ApplicationBuildContext) File(java.io.File) Modifier(com.github.javaparser.ast.Modifier) InvalidTransitionException(io.automatiko.engine.api.workflow.workitem.InvalidTransitionException) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) List(java.util.List) VariableViolationException(io.automatiko.engine.api.workflow.VariableViolationException) Paths(java.nio.file.Paths) ProcessInstanceDuplicatedException(io.automatiko.engine.api.workflow.ProcessInstanceDuplicatedException) Entry(java.util.Map.Entry) Optional(java.util.Optional) FileReader(java.io.FileReader) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Collections(java.util.Collections) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ProcessInstanceNotFoundException(io.automatiko.engine.api.workflow.ProcessInstanceNotFoundException) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) File(java.io.File)

Example 2 with ProcessMetaData

use of io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData 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;
}
Also used : Arrays(java.util.Arrays) Enumeration(java.util.Enumeration) XmlProcessReader(io.automatiko.engine.workflow.compiler.xml.XmlProcessReader) Type(io.automatiko.engine.codegen.GeneratedFile.Type) LoggerFactory(org.slf4j.LoggerFactory) ApplicationSection(io.automatiko.engine.codegen.ApplicationSection) ApplicationGenerator.log(io.automatiko.engine.codegen.ApplicationGenerator.log) DefaultResourceGeneratorFactory(io.automatiko.engine.codegen.DefaultResourceGeneratorFactory) Resource(io.automatiko.engine.api.io.Resource) Map(java.util.Map) ZipFile(java.util.zip.ZipFile) TriggerMetaData(io.automatiko.engine.workflow.compiler.canonical.TriggerMetaData) DependencyInjectionAnnotator(io.automatiko.engine.codegen.di.DependencyInjectionAnnotator) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) Process(io.automatiko.engine.api.definition.process.Process) BPMNExtensionsSemanticModule(io.automatiko.engine.workflow.bpmn2.xml.BPMNExtensionsSemanticModule) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ByteArrayResource(io.automatiko.engine.services.io.ByteArrayResource) StandardCharsets(java.nio.charset.StandardCharsets) InternalResource(io.automatiko.engine.services.io.InternalResource) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Stream(java.util.stream.Stream) BPMNDISemanticModule(io.automatiko.engine.workflow.bpmn2.xml.BPMNDISemanticModule) SAXException(org.xml.sax.SAXException) Entry(java.util.Map.Entry) ResourceType.determineResourceType(io.automatiko.engine.api.io.ResourceType.determineResourceType) Optional(java.util.Optional) ModelMetaData(io.automatiko.engine.workflow.compiler.canonical.ModelMetaData) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResourceGeneratorFactory(io.automatiko.engine.codegen.ResourceGeneratorFactory) ProcessToExecModelGenerator(io.automatiko.engine.workflow.compiler.canonical.ProcessToExecModelGenerator) SemanticModules(io.automatiko.engine.workflow.compiler.xml.SemanticModules) ServerlessWorkflowParser(io.automatiko.engine.workflow.serverless.parser.ServerlessWorkflowParser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IoUtils.readBytesFromInputStream(io.automatiko.engine.services.utils.IoUtils.readBytesFromInputStream) StringUtils(io.automatiko.engine.services.utils.StringUtils) ResourceType(io.automatiko.engine.api.io.ResourceType) FileSystemResource(io.automatiko.engine.services.io.FileSystemResource) LinkedHashSet(java.util.LinkedHashSet) UserTaskModelMetaData(io.automatiko.engine.workflow.compiler.canonical.UserTaskModelMetaData) CodegenUtils(io.automatiko.engine.codegen.CodegenUtils) BaseFunctions(io.automatiko.engine.services.execution.BaseFunctions) ConfigGenerator(io.automatiko.engine.codegen.ConfigGenerator) GeneratedFile(io.automatiko.engine.codegen.GeneratedFile) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ApplicationGenerator(io.automatiko.engine.codegen.ApplicationGenerator) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) IOException(java.io.IOException) ProcessConfigGenerator(io.automatiko.engine.codegen.process.config.ProcessConfigGenerator) BPMNSemanticModule(io.automatiko.engine.workflow.bpmn2.xml.BPMNSemanticModule) File(java.io.File) OpenAPIMetaData(io.automatiko.engine.workflow.compiler.canonical.OpenAPIMetaData) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) Paths(java.nio.file.Paths) AbstractGenerator(io.automatiko.engine.codegen.AbstractGenerator) Functions(io.automatiko.engine.api.Functions) Collections(java.util.Collections) ArrayList(java.util.ArrayList) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess)

Example 3 with ProcessMetaData

use of io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData in project automatiko-engine by automatiko-io.

the class ProcessGenerator method internalConfigure.

private MethodDeclaration internalConfigure(ProcessMetaData processMetaData) {
    BlockStmt body = new BlockStmt();
    MethodDeclaration internalConfigure = new MethodDeclaration().setModifiers(Modifier.Keyword.PUBLIC).setType(targetTypeName).setName("configure").setBody(body);
    // always call super.configure
    body.addStatement(new MethodCallExpr(new SuperExpr(), "configure"));
    if (!processMetaData.getGeneratedHandlers().isEmpty()) {
        processMetaData.getGeneratedHandlers().forEach((name, descriptor) -> {
            CompilationUnit handler = descriptor.generateHandlerClassForService();
            ClassOrInterfaceDeclaration clazz = handler.findFirst(ClassOrInterfaceDeclaration.class).get();
            if (useInjection()) {
                boolean tracingAvailable = context.getBuildContext().hasClassAvailable("org.eclipse.microprofile.opentracing.Traced");
                if (tracingAvailable) {
                    FieldDeclaration tracerField = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "io.automatiko.engine.service.tracing.TracingAdds"), "tracer"));
                    annotator.withInjection(tracerField);
                    clazz.addMember(tracerField);
                    clazz.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("executeWorkItem")).forEach(md -> {
                        // add Traced nnotation on method level
                        md.addAnnotation("org.eclipse.microprofile.opentracing.Traced");
                        // next update method body to include extra tags
                        BlockStmt mbody = md.getBody().get();
                        MethodCallExpr tracer = new MethodCallExpr(new NameExpr("tracer"), "addTags").addArgument(new MethodCallExpr(new NameExpr("workItem"), "getProcessInstance"));
                        BlockStmt updatedBody = new BlockStmt();
                        updatedBody.addStatement(tracer);
                        mbody.getStatements().forEach(s -> updatedBody.addStatement(s));
                        md.setBody(updatedBody);
                    });
                }
                boolean faultToleranceAvailable = context.getBuildContext().hasClassAvailable("io.automatiko.addons.fault.tolerance.CircuitClosedEvent");
                if (faultToleranceAvailable) {
                    Boolean disabled = Boolean.parseBoolean(descriptor.metadata("faultToleranceDisabled", "false").toString());
                    if (!disabled) {
                        Long timeout = Long.valueOf(descriptor.metadata("timeout", "-1").toString());
                        Long delay = Long.valueOf(descriptor.metadata("delay", "5000").toString());
                        Integer threshold = Integer.valueOf(descriptor.metadata("requestThreshold", "20").toString());
                        Double ratio = Double.valueOf(descriptor.metadata("failureRatio", ".50").toString());
                        NormalAnnotationExpr circuitBrakerAnnotation = new NormalAnnotationExpr(new Name("org.eclipse.microprofile.faulttolerance.CircuitBreaker"), NodeList.nodeList(new MemberValuePair("delay", new LongLiteralExpr(delay)), new MemberValuePair("requestVolumeThreshold", new IntegerLiteralExpr(threshold)), new MemberValuePair("failureRatio", new DoubleLiteralExpr(ratio)), new MemberValuePair("skipOn", new NameExpr("io.automatiko.engine.api.workflow.HandledServiceExecutionError.class"))));
                        clazz.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("executeWorkItem")).forEach(md -> {
                            // add CircuitBreaker annotation on method level
                            md.addAnnotation(circuitBrakerAnnotation);
                            md.addSingleMemberAnnotation("io.smallrye.faulttolerance.api.CircuitBreakerName", new StringLiteralExpr(name));
                            if (timeout > 0) {
                                md.addSingleMemberAnnotation("org.eclipse.microprofile.faulttolerance.Timeout", new LongLiteralExpr(timeout));
                            }
                        });
                        context.setApplicationProperty("quarkus.arc.selected-alternatives", context.getApplicationProperty("quarkus.arc.selected-alternatives").filter(s -> !s.contains("io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache")).map(s -> s + ",io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache").orElse("io.automatiko.addons.fault.tolerance.internal.AutomatikoStrategyCache"));
                    }
                }
                annotator.withApplicationComponent(clazz);
            } else {
                String packageName = handler.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
                String clazzName = clazz.getName().toString();
                MethodCallExpr workItemManager = new MethodCallExpr(new NameExpr("services"), "getWorkItemManager");
                MethodCallExpr registerHandler = new MethodCallExpr(workItemManager, "registerWorkItemHandler").addArgument(new StringLiteralExpr(name)).addArgument(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, packageName + "." + clazzName), NodeList.nodeList()));
                body.addStatement(registerHandler);
            }
            // annotate for injection or add constructor for initialization
            handler.findAll(FieldDeclaration.class, fd -> !fd.getVariable(0).getNameAsString().equals("tracer")).forEach(fd -> {
                if (useInjection()) {
                    annotator.withInjection(fd);
                }
                if (descriptor.implementation().equalsIgnoreCase("##webservice") && !fd.getVariable(0).getNameAsString().equals("completionHandler") && annotator != null) {
                    annotator.withRestClientInjection(fd);
                } else if (!descriptor.implementation().equalsIgnoreCase("##webservice") && !fd.getVariable(0).getNameAsString().equals("completionHandler")) {
                    BlockStmt constructorBody = new BlockStmt();
                    AssignExpr assignExpr = new AssignExpr(new FieldAccessExpr(new ThisExpr(), fd.getVariable(0).getNameAsString()), new ObjectCreationExpr().setType(fd.getVariable(0).getType().toString()), AssignExpr.Operator.ASSIGN);
                    constructorBody.addStatement(assignExpr);
                    clazz.addConstructor(Keyword.PUBLIC).setBody(constructorBody);
                }
            });
            additionalClasses.add(handler);
        });
        if (useInjection()) {
            BlockStmt actionBody = new BlockStmt();
            LambdaExpr forachBody = new LambdaExpr(new Parameter(new UnknownType(), "h"), actionBody);
            MethodCallExpr forachHandler = new MethodCallExpr(new NameExpr("handlers"), "forEach");
            forachHandler.addArgument(forachBody);
            MethodCallExpr workItemManager = new MethodCallExpr(new NameExpr("services"), "getWorkItemManager");
            MethodCallExpr registerHandler = new MethodCallExpr(workItemManager, "registerWorkItemHandler").addArgument(new MethodCallExpr(new NameExpr("h"), "getName")).addArgument(new NameExpr("h"));
            actionBody.addStatement(registerHandler);
            body.addStatement(forachHandler);
        }
    }
    if (!processMetaData.getGeneratedListeners().isEmpty()) {
        processMetaData.getGeneratedListeners().forEach(listener -> {
            ClassOrInterfaceDeclaration clazz = listener.findFirst(ClassOrInterfaceDeclaration.class).get();
            String packageName = listener.getPackageDeclaration().map(pd -> pd.getName().toString()).orElse("");
            String clazzName = clazz.getName().toString();
            MethodCallExpr eventSupport = new MethodCallExpr(new NameExpr("services"), "getEventSupport");
            MethodCallExpr registerListener = new MethodCallExpr(eventSupport, "addEventListener").addArgument(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, packageName + "." + clazzName), NodeList.nodeList()));
            body.addStatement(registerListener);
            additionalClasses.add(listener);
        });
    }
    body.addStatement(new ReturnStmt(new ThisExpr()));
    return internalConfigure;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) GeneratorContext(io.automatiko.engine.codegen.GeneratorContext) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) CompilationUnit(com.github.javaparser.ast.CompilationUnit) TriggerMetaData(io.automatiko.engine.workflow.compiler.canonical.TriggerMetaData) DependencyInjectionAnnotator(io.automatiko.engine.codegen.di.DependencyInjectionAnnotator) Process(io.automatiko.engine.api.definition.process.Process) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) UnknownType(com.github.javaparser.ast.type.UnknownType) Model(io.automatiko.engine.api.Model) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) SvgServerlessProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgServerlessProcessImageGenerator) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Modifier(com.github.javaparser.ast.Modifier) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Entry(java.util.Map.Entry) Operator(com.github.javaparser.ast.expr.AssignExpr.Operator) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) Name(com.github.javaparser.ast.expr.Name) CastExpr(com.github.javaparser.ast.expr.CastExpr) EndOfInstanceStrategy(io.automatiko.engine.api.workflow.EndOfInstanceStrategy) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) SvgProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgProcessImageGenerator) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) SuperExpr(com.github.javaparser.ast.expr.SuperExpr) StringUtils(io.automatiko.engine.services.utils.StringUtils) SvgBpmnProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator) NoSuchElementException(java.util.NoSuchElementException) Keyword(com.github.javaparser.ast.Modifier.Keyword) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) AbstractProcess(io.automatiko.engine.workflow.AbstractProcess) UserTaskModelMetaData(io.automatiko.engine.workflow.compiler.canonical.UserTaskModelMetaData) CodegenUtils(io.automatiko.engine.codegen.CodegenUtils) BaseFunctions(io.automatiko.engine.services.execution.BaseFunctions) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance) NameExpr(com.github.javaparser.ast.expr.NameExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) Functions(io.automatiko.engine.api.Functions) ServerlessFunctions(io.automatiko.engine.workflow.sw.ServerlessFunctions) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) SimpleName(com.github.javaparser.ast.expr.SimpleName) Name(com.github.javaparser.ast.expr.Name) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CompilationUnit(com.github.javaparser.ast.CompilationUnit) SuperExpr(com.github.javaparser.ast.expr.SuperExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with ProcessMetaData

use of io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData in project automatiko-engine by automatiko-io.

the class ProcessGenerator method classDeclaration.

public ClassOrInterfaceDeclaration classDeclaration(CompilationUnit compilationUnit) {
    ClassOrInterfaceDeclaration cls = new ClassOrInterfaceDeclaration().setName(targetTypeName).setModifiers(Modifier.Keyword.PUBLIC);
    if (useInjection()) {
        annotator.withNamedApplicationComponent(cls, process.getId() + versionSuffix);
        FieldDeclaration handlersInjectFieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, new SimpleName(annotator.multiInstanceInjectionType()), NodeList.nodeList(new ClassOrInterfaceType(null, WorkItemHandler.class.getCanonicalName()))), "handlers"));
        cls.addMember(handlersInjectFieldDeclaration);
    }
    String processInstanceFQCN = ProcessInstanceGenerator.qualifiedName(packageName, typeName);
    FieldDeclaration fieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, appCanonicalName), "app"));
    ConstructorDeclaration emptyConstructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC);
    ConstructorDeclaration baseConstructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)));
    ConstructorDeclaration constructorDeclaration;
    if (useInjection()) {
        constructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").addParameter(new ClassOrInterfaceType(null, new SimpleName(annotator.multiInstanceInjectionType()), NodeList.nodeList(new ClassOrInterfaceType(null, WorkItemHandler.class.getCanonicalName()))), "handlers").addParameter(EndOfInstanceStrategy.class.getCanonicalName(), "strategy").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "handlers"), new NameExpr("handlers"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "endOfInstanceStrategy"), new NameExpr("strategy"), AssignExpr.Operator.ASSIGN)));
    } else {
        constructorDeclaration = new ConstructorDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC).addParameter(appCanonicalName, "app").addParameter(EndOfInstanceStrategy.class.getCanonicalName(), "strategy").setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "super").addArgument(new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "config"), "process"))).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "app"), new NameExpr("app"), AssignExpr.Operator.ASSIGN)).addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "endOfInstanceStrategy"), new NameExpr("strategy"), AssignExpr.Operator.ASSIGN)));
    }
    ProcessMetaData processMetaData = processGenerator.generate();
    if (!processMetaData.getSubProcesses().isEmpty()) {
        for (Entry<String, String> subProcess : processMetaData.getSubProcesses().entrySet()) {
            FieldDeclaration subprocessFieldDeclaration = new FieldDeclaration();
            if (processIdToMetadata.get(subProcess.getKey()) != null) {
                String subProcessPackage = processIdToMetadata.get(subProcess.getKey()).getPackageName();
                if (subProcessPackage != null && !subProcessPackage.isEmpty()) {
                    compilationUnit.addImport(subProcessPackage + "." + StringUtils.capitalize(subProcess.getKey() + "Model"));
                }
            }
            String fieldName = "process" + subProcess.getKey();
            ClassOrInterfaceType modelType = new ClassOrInterfaceType(null, new SimpleName(io.automatiko.engine.api.workflow.Process.class.getCanonicalName()), NodeList.nodeList(new ClassOrInterfaceType(null, StringUtils.capitalize(subProcess.getKey() + "Model"))));
            if (useInjection()) {
                subprocessFieldDeclaration.addVariable(new VariableDeclarator(modelType, fieldName));
                constructorDeclaration.addParameter(annotator.withNamed(new Parameter(modelType, fieldName), subProcess.getKey()));
                constructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new NameExpr(fieldName), AssignExpr.Operator.ASSIGN));
            } else {
                // app.processes().processById()
                MethodCallExpr initSubProcessField = new MethodCallExpr(new MethodCallExpr(new NameExpr("app"), "processes"), "processById").addArgument(new StringLiteralExpr(subProcess.getKey()));
                subprocessFieldDeclaration.addVariable(new VariableDeclarator(modelType, fieldName));
                baseConstructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new CastExpr(modelType, initSubProcessField), Operator.ASSIGN));
                constructorDeclaration.getBody().addStatement(new AssignExpr(new FieldAccessExpr(new ThisExpr(), fieldName), new CastExpr(modelType, initSubProcessField), Operator.ASSIGN));
            }
            cls.addMember(subprocessFieldDeclaration);
            subprocessFieldDeclaration.createGetter();
        }
    }
    if (useInjection()) {
        annotator.withInjection(constructorDeclaration);
    } else {
        emptyConstructorDeclaration.setBody(new BlockStmt().addStatement(new MethodCallExpr(null, "this").addArgument(new ObjectCreationExpr().setType(appCanonicalName))));
    }
    MethodDeclaration createModelMethod = new MethodDeclaration().addModifier(Keyword.PUBLIC).setName("createModel").setType(modelTypeName).setBody(new BlockStmt().addStatement(new ReturnStmt(new ObjectCreationExpr(null, new ClassOrInterfaceType(null, modelTypeName), NodeList.nodeList()))));
    cls.addExtendedType(abstractProcessType(modelTypeName)).addMember(fieldDeclaration).addMember(emptyConstructorDeclaration).addMember(baseConstructorDeclaration).addMember(constructorDeclaration).addMember(createInstanceMethod(processInstanceFQCN)).addMember(createInstanceWithBusinessKeyMethod(processInstanceFQCN)).addMember(createModelMethod).addMember(createInstanceGenericMethod(processInstanceFQCN)).addMember(createInstanceGenericWithBusinessKeyMethod(processInstanceFQCN)).addMember(createInstanceGenericWithWorkflowInstanceMethod(processInstanceFQCN)).addMember(createReadOnlyInstanceGenericWithWorkflowInstanceMethod(processInstanceFQCN)).addMember(internalConfigure(processMetaData)).addMember(internalRegisterListeners(processMetaData)).addMember(userTaskInputModels(processMetaData)).addMember(userTaskOutputModels(processMetaData)).addMember(process(processMetaData));
    if (isServiceProject()) {
        SvgProcessImageGenerator imageGenerator = isServerlessWorkflow() ? new SvgServerlessProcessImageGenerator(process) : new SvgBpmnProcessImageGenerator(process);
        String svg = imageGenerator.generate();
        if (svg != null && !svg.isEmpty()) {
            MethodDeclaration processImageMethod = new MethodDeclaration().setName("image").setModifiers(Keyword.PUBLIC).setType(String.class).setBody(new BlockStmt().addStatement(new ReturnStmt(new StringLiteralExpr().setString(svg))));
            cls.addMember(processImageMethod);
        }
    }
    if (useInjection()) {
        MethodDeclaration initMethod = annotator.withInitMethod(new MethodCallExpr(new ThisExpr(), "activate"));
        cls.addMember(initMethod);
    }
    if (!processMetaData.getTriggers().isEmpty()) {
        for (TriggerMetaData trigger : processMetaData.getTriggers()) {
            // add message produces as field
            if (trigger.getType().equals(TriggerMetaData.TriggerType.ProduceMessage)) {
                String producerFieldType = packageName + "." + typeName + "MessageProducer_" + trigger.getOwnerId();
                String producerFielName = "producer_" + trigger.getOwnerId();
                FieldDeclaration producerFieldieldDeclaration = new FieldDeclaration().addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, producerFieldType), producerFielName));
                cls.addMember(producerFieldieldDeclaration);
                if (useInjection()) {
                    annotator.withInjection(producerFieldieldDeclaration);
                } else {
                    AssignExpr assignExpr = new AssignExpr(new FieldAccessExpr(new ThisExpr(), producerFielName), new ObjectCreationExpr().setType(producerFieldType), AssignExpr.Operator.ASSIGN);
                    cls.getConstructors().forEach(c -> c.getBody().addStatement(assignExpr));
                }
            }
        }
    }
    cls.getMembers().sort(new BodyDeclarationComparator());
    return cls;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) SvgBpmnProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator) SimpleName(com.github.javaparser.ast.expr.SimpleName) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) SvgServerlessProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgServerlessProcessImageGenerator) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TriggerMetaData(io.automatiko.engine.workflow.compiler.canonical.TriggerMetaData) WorkItemHandler(io.automatiko.engine.api.runtime.process.WorkItemHandler) SvgProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgProcessImageGenerator) EndOfInstanceStrategy(io.automatiko.engine.api.workflow.EndOfInstanceStrategy) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with ProcessMetaData

use of io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData in project automatiko-engine by automatiko-io.

the class ProcessInstanceGenerator method classDeclaration.

public ClassOrInterfaceDeclaration classDeclaration() {
    ClassOrInterfaceDeclaration classDecl = new ClassOrInterfaceDeclaration().setName(targetTypeName).addModifier(Modifier.Keyword.PUBLIC);
    classDecl.addExtendedType(new ClassOrInterfaceType(null, AbstractProcessInstance.class.getCanonicalName()).setTypeArguments(new ClassOrInterfaceType(null, model.getModelClassSimpleName()))).addMember(constructorDecl()).addMember(constructorWithBusinessKeyDecl()).addMember(constructorWithWorkflowInstanceAndRuntimeDecl()).addMember(constructorWorkflowInstanceDecl()).addMember(bind()).addMember(unbind());
    if (generatorContext.getApplicationProperty("quarkus.automatiko.instance-locking").orElse("true").equalsIgnoreCase("false")) {
        MethodDeclaration configureLock = new MethodDeclaration().setModifiers(Keyword.PROTECTED).setName("configureLock").addParameter(String.class.getCanonicalName(), "businessKey").setType(new VoidType());
        classDecl.addMember(configureLock);
    }
    ProcessMetaData processMetaData = processGenerator.generate();
    if (!processMetaData.getSubProcesses().isEmpty()) {
        classDecl.getMembers().add(subprocessesMethod(processMetaData));
    }
    classDecl.getMembers().sort(new BodyDeclarationComparator());
    return classDecl;
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) ProcessMetaData(io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Aggregations

ProcessMetaData (io.automatiko.engine.workflow.compiler.canonical.ProcessMetaData)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 Entry (java.util.Map.Entry)6 WorkflowProcess (io.automatiko.engine.api.definition.process.WorkflowProcess)5 File (java.io.File)5 IOException (java.io.IOException)5 Path (java.nio.file.Path)5 Paths (java.nio.file.Paths)5 Collection (java.util.Collection)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 LinkedHashSet (java.util.LinkedHashSet)5 Optional (java.util.Optional)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5