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);
}
}
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");
}
}
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();
}
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();
}
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);
}
}
Aggregations