Search in sources :

Example 1 with SvgBpmnProcessImageGenerator

use of io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator in project automatiko-engine by automatiko-io.

the class SvgProcessImageGeneratorTest method testGeneration.

public void testGeneration(String processResource) throws IOException {
    BpmnProcessCompiler compiler = new BpmnProcessCompiler();
    BpmnProcess process = compiler.from(null, new ClassPathResource(processResource)).get(0);
    assertThat(process).isNotNull();
    SvgBpmnProcessImageGenerator generator = new SvgBpmnProcessImageGenerator((WorkflowProcess) process.process());
    String svg = generator.generate();
    assertThat(svg).isNotEmpty();
    Files.write(Paths.get("target", "test.svg"), svg.getBytes(StandardCharsets.UTF_8));
}
Also used : BpmnProcess(io.automatiko.engine.workflow.bpmn2.BpmnProcess) BpmnProcessCompiler(io.automatiko.engine.workflow.bpmn2.BpmnProcessCompiler) SvgBpmnProcessImageGenerator(io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator) ClassPathResource(io.automatiko.engine.services.io.ClassPathResource)

Example 2 with SvgBpmnProcessImageGenerator

use of io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator 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)

Aggregations

SvgBpmnProcessImageGenerator (io.automatiko.engine.codegen.process.image.SvgBpmnProcessImageGenerator)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 CastExpr (com.github.javaparser.ast.expr.CastExpr)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 SimpleName (com.github.javaparser.ast.expr.SimpleName)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 WorkItemHandler (io.automatiko.engine.api.runtime.process.WorkItemHandler)1