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