Search in sources :

Example 1 with InvalidTemplateException

use of org.kie.kogito.codegen.api.template.InvalidTemplateException in project kogito-runtimes by kiegroup.

the class DecisionContainerGenerator method compilationUnit.

@Override
public CompilationUnit compilationUnit() {
    CompilationUnit compilationUnit = templatedGenerator.compilationUnitOrThrow("Invalid Template: No CompilationUnit");
    ClassOrInterfaceType applicationClass = StaticJavaParser.parseClassOrInterfaceType(applicationCanonicalName);
    final InitializerDeclaration staticDeclaration = compilationUnit.findFirst(InitializerDeclaration.class).orElseThrow(() -> new InvalidTemplateException(templatedGenerator, "Missing static block"));
    final MethodCallExpr initMethod = staticDeclaration.findFirst(MethodCallExpr.class, mtd -> "init".equals(mtd.getNameAsString())).orElseThrow(() -> new InvalidTemplateException(templatedGenerator, "Missing init() method"));
    setupPmmlIfAvailable(initMethod);
    setupExecIdSupplierVariable(initMethod);
    setupDecisionModelTransformerVariable(initMethod);
    for (CollectedResource resource : resources) {
        Optional<String> encoding = determineEncoding(resource);
        MethodCallExpr getResAsStream = getReadResourceMethod(applicationClass, resource);
        MethodCallExpr isr = new MethodCallExpr("readResource").addArgument(getResAsStream);
        encoding.map(StringLiteralExpr::new).ifPresent(isr::addArgument);
        initMethod.addArgument(isr);
    }
    return compilationUnit;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) Logger(org.slf4j.Logger) AbstractApplicationSection(org.kie.kogito.codegen.core.AbstractApplicationSection) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) LoggerFactory(org.slf4j.LoggerFactory) ReadResourceUtil.getReadResourceMethod(org.kie.kogito.codegen.decision.ReadResourceUtil.getReadResourceMethod) DmnExecutionIdSupplier(org.kie.kogito.dmn.DmnExecutionIdSupplier) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayList(java.util.ArrayList) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) BufferedReader(java.io.BufferedReader) CompilationUnit(com.github.javaparser.ast.CompilationUnit) CodegenUtils.newObject(org.kie.kogito.codegen.core.CodegenUtils.newObject) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with InvalidTemplateException

use of org.kie.kogito.codegen.api.template.InvalidTemplateException in project kogito-runtimes by kiegroup.

the class DecisionModelResourcesProviderGenerator method setupResourcesVariable.

private void setupResourcesVariable(final ClassOrInterfaceDeclaration typeDeclaration) {
    final List<MethodDeclaration> getResourcesMethods = typeDeclaration.getMethodsBySignature("getResources");
    final ClassOrInterfaceType applicationClass = StaticJavaParser.parseClassOrInterfaceType(applicationCanonicalName);
    if (getResourcesMethods.size() != 1) {
        throw new InvalidTemplateException(generator, "A \"getResources()\" method was not found");
    }
    final MethodDeclaration getResourcesMethod = getResourcesMethods.get(0);
    final BlockStmt body = getResourcesMethod.getBody().orElseThrow(() -> new RuntimeException("Can't find the body of the \"get()\" method."));
    final VariableDeclarator resourcePathsVariable = getResourcesMethod.findFirst(VariableDeclarator.class).orElseThrow(() -> new RuntimeException("Can't find a variable declaration in the \"get()\" method."));
    if (!context.getGAV().isPresent()) {
        LOGGER.error("Impossible to obtain project group-artifact-id, using empty value");
    }
    KogitoGAV gav = context.getGAV().orElse(KogitoGAV.EMPTY_GAV);
    for (DMNResource resource : resources) {
        final MethodCallExpr add = new MethodCallExpr(resourcePathsVariable.getNameAsExpression(), "add");
        final MethodCallExpr getResAsStream = getReadResourceMethod(applicationClass, resource.getCollectedResource());
        final MethodCallExpr isr = new MethodCallExpr("readResource").addArgument(getResAsStream);
        add.addArgument(newObject(DefaultDecisionModelResource.class, newGAV(gav), new StringLiteralExpr(resource.getDmnModel().getNamespace()), new StringLiteralExpr(resource.getDmnModel().getName()), makeDecisionModelMetadata(resource), isr));
        body.addStatement(body.getStatements().size() - 1, add);
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) DefaultDecisionModelResource(org.kie.kogito.dmn.DefaultDecisionModelResource) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KogitoGAV(org.kie.kogito.KogitoGAV) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with InvalidTemplateException

use of org.kie.kogito.codegen.api.template.InvalidTemplateException in project kogito-runtimes by kiegroup.

the class MessageConsumerGenerator method generate.

public String generate() {
    ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find class declaration"));
    generateModelMethods(template);
    template.setName(resourceClazzName);
    template.findAll(ConstructorDeclaration.class).forEach(cd -> cd.setName(resourceClazzName));
    template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, dataClazzName));
    template.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$ProcessName$", processName)));
    template.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$Trigger$", trigger.getName())));
    template.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataType$", trigger.getDataType())));
    template.findAll(MethodCallExpr.class).forEach(this::interpolateStrings);
    // legacy: force initialize fields
    if (!context.hasDI()) {
        template.findAll(FieldDeclaration.class, fd -> isProcessField(fd)).forEach(fd -> initializeProcessField(fd));
        template.findAll(FieldDeclaration.class, fd -> isApplicationField(fd)).forEach(fd -> initializeApplicationField(fd));
        template.findAll(FieldDeclaration.class, fd -> isObjectMapperField(fd)).forEach(fd -> initializeObjectMapperField(fd));
    }
    template.getMembers().sort(new BodyDeclarationComparator());
    return clazz.toString();
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TriggerMetaData(org.jbpm.compiler.canonical.TriggerMetaData) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) StartNode(org.jbpm.workflow.core.node.StartNode) ProcessDataEventConverter(org.kie.kogito.services.event.ProcessDataEventConverter) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) Function(java.util.function.Function) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) WorkflowProcess(org.kie.api.definition.process.WorkflowProcess) ProcessDataEvent(org.kie.kogito.services.event.ProcessDataEvent) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CodegenUtils.isApplicationField(org.kie.kogito.codegen.core.CodegenUtils.isApplicationField) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) CodegenUtils.isObjectMapperField(org.kie.kogito.codegen.core.CodegenUtils.isObjectMapperField) CodegenUtils.isProcessField(org.kie.kogito.codegen.core.CodegenUtils.isProcessField) CodegenUtils.interpolateTypes(org.kie.kogito.codegen.core.CodegenUtils.interpolateTypes) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Keyword(com.github.javaparser.ast.Modifier.Keyword) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Node(org.kie.api.definition.process.Node) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Operator(com.github.javaparser.ast.expr.AssignExpr.Operator) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) StringUtils(org.drools.core.util.StringUtils) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with InvalidTemplateException

use of org.kie.kogito.codegen.api.template.InvalidTemplateException in project kogito-runtimes by kiegroup.

the class MessageProducerGenerator method generate.

public String generate() {
    ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find class declaration"));
    template.setName(resourceClazzName);
    template.findAll(ConstructorDeclaration.class).forEach(cd -> cd.setName(resourceClazzName));
    template.findAll(ClassOrInterfaceType.class).forEach(cls -> CodegenUtils.interpolateTypes(cls, trigger.getDataType()));
    template.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$Trigger$", trigger.getName())));
    template.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$ClassName$", resourceClazzName)));
    template.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataType$", trigger.getDataType())));
    template.findAll(StringLiteralExpr.class).forEach(s -> s.setString(s.getValue().replace("$channel$", trigger.getName())));
    template.getMembers().sort(new BodyDeclarationComparator());
    return clazz.toString();
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BodyDeclarationComparator(org.kie.kogito.codegen.core.BodyDeclarationComparator) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException)

Example 5 with InvalidTemplateException

use of org.kie.kogito.codegen.api.template.InvalidTemplateException in project kogito-runtimes by kiegroup.

the class ProcessContainerGenerator method setupProcessById.

private void setupProcessById(CompilationUnit compilationUnit) {
    byProcessIdBody.addStatement(new ReturnStmt(new NullLiteralExpr()));
    compilationUnit.findFirst(MethodDeclaration.class, m -> m.getNameAsString().equals("processById")).orElseThrow(() -> new InvalidTemplateException(templatedGenerator, "Cannot find 'processById' method body")).setBody(this.byProcessIdBody);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException)

Aggregations

InvalidTemplateException (org.kie.kogito.codegen.api.template.InvalidTemplateException)22 CompilationUnit (com.github.javaparser.ast.CompilationUnit)15 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)15 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)10 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)9 NameExpr (com.github.javaparser.ast.expr.NameExpr)8 TemplatedGenerator (org.kie.kogito.codegen.api.template.TemplatedGenerator)8 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)7 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)7 Expression (com.github.javaparser.ast.expr.Expression)6 Collection (java.util.Collection)6 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 SimpleName (com.github.javaparser.ast.expr.SimpleName)5 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)5