Search in sources :

Example 11 with InvalidTemplateException

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

the class ProjectRuntimeGenerator method writeGetKieBaseForSessionMethod.

private void writeGetKieBaseForSessionMethod(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration getKieBaseForSessionMethod = clazz.findAll(MethodDeclaration.class).stream().filter(m -> m.getNameAsString().equals("getKieBaseForSession")).findFirst().orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find getKieBaseForSession method"));
    SwitchStmt switchStmt = getKieBaseForSessionMethod.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find switch inside getKieBaseForSession method"));
    for (Map.Entry<String, String> entry : modelMethod.getkSessionForkBase().entrySet()) {
        StringLiteralExpr sessionName = new StringLiteralExpr(entry.getKey());
        Statement stmt = parseStatement("return getKieBase(\"" + entry.getValue() + "\");");
        SwitchEntry switchEntry = new SwitchEntry(new NodeList<>(sessionName), SwitchEntry.Type.STATEMENT_GROUP, new NodeList<>(stmt));
        switchStmt.getEntries().add(switchEntry);
    }
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) Map(java.util.Map) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException)

Example 12 with InvalidTemplateException

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

the class ProjectRuntimeGenerator method writeInitKieBasesMethod.

private void writeInitKieBasesMethod(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration initKieBasesMethod = clazz.findAll(MethodDeclaration.class).stream().filter(m -> m.getNameAsString().equals("initKieBases")).findFirst().orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find initKieBases method"));
    IfStmt ifStmt = initKieBasesMethod.findFirst(IfStmt.class).orElseThrow(() -> new InvalidTemplateException(generator, "Cannot find if statement in initKieBases method"));
    BlockStmt ifBlock = ifStmt.getThenStmt().asBlockStmt();
    for (String kbaseName : modelMethod.getKieBaseNames()) {
        ifBlock.addStatement("kbaseMap.put( \"" + kbaseName + "\", " + "new KieBaseImpl( KieBaseBuilder.createKieBaseFromModel( model.getModelsForKieBase( \"" + kbaseName + "\" ), " + "model.getKieModuleModel().getKieBaseModels().get( \"" + kbaseName + "\" ) ) ) );\n");
    }
}
Also used : IfStmt(com.github.javaparser.ast.stmt.IfStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException)

Example 13 with InvalidTemplateException

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

the class DecisionCloudEventMetaFactoryGenerator method generate.

public String generate() {
    CompilationUnit compilationUnit = generator.compilationUnitOrThrow("Cannot generate CloudEventMetaFactory");
    ClassOrInterfaceDeclaration classDefinition = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Compilation unit doesn't contain a class or interface declaration!"));
    MethodDeclaration templatedBuildMethod = classDefinition.findFirst(MethodDeclaration.class, x -> x.getName().toString().startsWith("buildCloudEventMeta_$methodName$")).orElseThrow(() -> new InvalidTemplateException(generator, "Impossible to find expected buildCloudEventMeta_ method"));
    Set<DecisionCloudEventMeta> methodDataList = this.getCloudEventMetaBuilder().build(models);
    methodDataList.forEach(methodData -> {
        MethodDeclaration builderMethod = templatedBuildMethod.clone();
        String methodNameValue = String.format("%s_%s", methodData.getKind().name(), methodData.methodNameChunk);
        String builderMethodName = getBuilderMethodName(classDefinition, templatedBuildMethod.getNameAsString(), methodNameValue);
        builderMethod.setName(builderMethodName);
        Map<String, Expression> expressions = new HashMap<>();
        expressions.put("$type$", new StringLiteralExpr(methodData.getType()));
        expressions.put("$source$", new StringLiteralExpr(methodData.getSource()));
        expressions.put("$kind$", new FieldAccessExpr(new NameExpr(new SimpleName(EventKind.class.getName())), methodData.getKind().name()));
        builderMethod.findFirst(MethodCallExpr.class).ifPresent(callExpr -> CodegenUtils.interpolateArguments(callExpr, expressions));
        classDefinition.addMember(builderMethod);
    });
    templatedBuildMethod.remove();
    if (context.hasDI()) {
        context.getDependencyInjectionAnnotator().withFactoryClass(classDefinition);
        classDefinition.findAll(FieldDeclaration.class, CodegenUtils::isConfigBeanField).forEach(fd -> context.getDependencyInjectionAnnotator().withInjection(fd));
        classDefinition.findAll(MethodDeclaration.class, x -> x.getName().toString().startsWith("buildCloudEventMeta_")).forEach(md -> context.getDependencyInjectionAnnotator().withFactoryMethod(md));
    }
    return compilationUnit.toString();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AbstractCloudEventMetaFactoryGenerator(org.kie.kogito.codegen.core.events.AbstractCloudEventMetaFactoryGenerator) SimpleName(com.github.javaparser.ast.expr.SimpleName) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) DMNModel(org.kie.dmn.api.core.DMNModel) EventKind(org.kie.kogito.event.EventKind) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Map(java.util.Map) CodegenUtils(org.kie.kogito.codegen.core.CodegenUtils) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) Expression(com.github.javaparser.ast.expr.Expression) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 14 with InvalidTemplateException

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

the class ProcessCloudEventMetaFactoryGenerator method generate.

public String generate() {
    CompilationUnit compilationUnit = generator.compilationUnitOrThrow("Cannot generate CloudEventMetaFactory");
    ClassOrInterfaceDeclaration classDefinition = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Compilation unit doesn't contain a class or interface declaration!"));
    MethodDeclaration templatedBuildMethod = classDefinition.findFirst(MethodDeclaration.class, x -> x.getName().toString().startsWith("buildCloudEventMeta_")).orElseThrow(() -> new InvalidTemplateException(generator, "Impossible to find expected buildCloudEventMeta_ method"));
    Set<ProcessCloudEventMeta> processCloudEventMetaList = this.getCloudEventMetaBuilder().build(this.generators);
    processCloudEventMetaList.forEach(processCloudEventMeta -> {
        MethodDeclaration builderMethod = templatedBuildMethod.clone();
        String methodNameValue = String.format("%s_%s", processCloudEventMeta.getKind().name(), toValidJavaIdentifier(processCloudEventMeta.triggerName));
        String builderMethodName = getBuilderMethodName(classDefinition, templatedBuildMethod.getNameAsString(), methodNameValue);
        builderMethod.setName(builderMethodName);
        Map<String, Expression> expressions = new HashMap<>();
        expressions.put("$type$", new StringLiteralExpr(processCloudEventMeta.getType()));
        expressions.put("$source$", new StringLiteralExpr(processCloudEventMeta.getSource()));
        expressions.put("$kind$", new FieldAccessExpr(new NameExpr(new SimpleName(EventKind.class.getName())), processCloudEventMeta.getKind().name()));
        ObjectCreationExpr objectCreationExpr = builderMethod.findAll(ObjectCreationExpr.class).get(0);
        CodegenUtils.interpolateArguments(objectCreationExpr, expressions);
        if (context.hasDI()) {
            context.getDependencyInjectionAnnotator().withFactoryMethod(builderMethod);
        }
        classDefinition.addMember(builderMethod);
    });
    templatedBuildMethod.remove();
    if (context.hasDI()) {
        context.getDependencyInjectionAnnotator().withFactoryClass(classDefinition);
    }
    return compilationUnit.toString();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AbstractCloudEventMetaFactoryGenerator(org.kie.kogito.codegen.core.events.AbstractCloudEventMetaFactoryGenerator) SimpleName(com.github.javaparser.ast.expr.SimpleName) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) Set(java.util.Set) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) EventKind(org.kie.kogito.event.EventKind) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) ProcessExecutableModelGenerator(org.kie.kogito.codegen.process.ProcessExecutableModelGenerator) CodegenUtils(org.kie.kogito.codegen.core.CodegenUtils) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SimpleName(com.github.javaparser.ast.expr.SimpleName) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) Expression(com.github.javaparser.ast.expr.Expression) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr)

Example 15 with InvalidTemplateException

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

the class PredictionModelsGenerator method populateStaticKieRuntimeFactoryFunctionInit.

private void populateStaticKieRuntimeFactoryFunctionInit(CompilationUnit compilationUnit) {
    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"));
    for (PMMLResource resource : resources) {
        StringLiteralExpr getResAsStream = getReadResourceMethod(resource);
        initMethod.addArgument(getResAsStream);
    }
}
Also used : InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) StringEscapeUtils(com.github.javaparser.utils.StringEscapeUtils) 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) CompilationUnit(com.github.javaparser.ast.CompilationUnit) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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