Search in sources :

Example 6 with GeneratedFile

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

the class PersistenceGenerator method kafkaBasedPersistence.

protected Collection<GeneratedFile> kafkaBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
    }
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    TemplatedGenerator generator = TemplatedGenerator.builder().withTemplateBasePath(CLASS_TEMPLATES_PERSISTENCE).withFallbackContext(JavaKogitoBuildContext.CONTEXT_NAME).withPackageName(KOGITO_PROCESS_INSTANCE_PACKAGE).build(context(), "KafkaStreamsTopologyProducer");
    CompilationUnit parsedClazzFile = generator.compilationUnitOrThrow();
    ClassOrInterfaceDeclaration producer = parsedClazzFile.findFirst(ClassOrInterfaceDeclaration.class).orElseThrow(() -> new InvalidTemplateException(generator, "Failed to find template for KafkaStreamsTopologyProducer"));
    MethodCallExpr asListOfProcesses = new MethodCallExpr(new NameExpr("java.util.Arrays"), "asList");
    protoGenerator.getProcessIds().forEach(p -> asListOfProcesses.addArgument(new StringLiteralExpr(p)));
    producer.getFieldByName("processes").orElseThrow(() -> new InvalidTemplateException(generator, "Failed to find field 'processes' in KafkaStreamsTopologyProducer template")).getVariable(0).setInitializer(asListOfProcesses);
    String clazzName = KOGITO_PROCESS_INSTANCE_PACKAGE + "." + producer.getName().asString();
    generatedFiles.add(new GeneratedFile(GeneratedFileType.SOURCE, clazzName.replace('.', '/') + JAVA, parsedClazzFile.toString()));
    CompilationUnit compilationUnit = new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE);
    compilationUnit.getTypes().add(persistenceProviderClazz);
    generatePersistenceProviderClazz(persistenceProviderClazz, compilationUnit).ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : TemplatedGenerator(org.kie.kogito.codegen.api.template.TemplatedGenerator) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) InvalidTemplateException(org.kie.kogito.codegen.api.template.InvalidTemplateException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with GeneratedFile

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

the class KieSessionModelBuilder method generate.

List<GeneratedFile> generate() {
    ModelSourceClass modelSourceClass = new ModelSourceClass(DroolsModelBuilder.DUMMY_RELEASE_ID, kieBaseModels, modelByKBase);
    ProjectRuntimeGenerator projectRuntimeGenerator = new ProjectRuntimeGenerator(modelSourceClass.getModelMethod(), context);
    return List.of(new GeneratedFile(RuleCodegen.RULE_TYPE, modelSourceClass.getName(), modelSourceClass.generate()), new GeneratedFile(RuleCodegen.RULE_TYPE, projectRuntimeGenerator.getName(), projectRuntimeGenerator.generate()));
}
Also used : ModelSourceClass(org.drools.modelcompiler.builder.ModelSourceClass) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile)

Example 8 with GeneratedFile

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

the class KogitoAssetsProcessor method generateSources.

@BuildStep
public KogitoGeneratedSourcesBuildItem generateSources(Capabilities capabilities, KogitoBuildContextBuildItem contextBuildItem) {
    final KogitoBuildContext context = contextBuildItem.getKogitoBuildContext();
    validateAvailableCapabilities(context, capabilities);
    Collection<GeneratedFile> generatedFiles = generateFiles(context);
    // During actual hot reloads it will be regenerated by the compilation providers in order to retrigger this build step
    if (!liveReload.isLiveReload()) {
        generatedFiles.add(new GeneratedFile(GeneratedFileType.SOURCE, HOT_RELOAD_SUPPORT_PATH + ".java", getHotReloadSupportSource()));
    }
    return new KogitoGeneratedSourcesBuildItem(generatedFiles);
}
Also used : KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 9 with GeneratedFile

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

the class ProcessesAssetsProcessor method postGenerationProcessing.

/**
 * Main entry point of the Quarkus extension
 */
@BuildStep
public void postGenerationProcessing(List<KogitoGeneratedClassesBuildItem> generatedKogitoClasses, BuildProducer<GeneratedBeanBuildItem> generatedBeans, BuildProducer<AdditionalStaticResourceBuildItem> staticResProducer, BuildProducer<NativeImageResourceBuildItem> resource, BuildProducer<NativeImageResourcePatternsBuildItem> resourcePatterns, BuildProducer<GeneratedResourceBuildItem> genResBI, BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfiguration, CombinedIndexBuildItem combinedIndexBuildItem, KogitoBuildContextBuildItem kogitoBuildContextBuildItem, Capabilities capabilities) throws IOException {
    // merge project index with classes generated by Kogito
    IndexView aggregatedIndex = generateAggregatedIndex(combinedIndexBuildItem.getComputingIndex(), generatedKogitoClasses);
    // configure the application generator
    KogitoBuildContext context = kogitoBuildContextBuildItem.getKogitoBuildContext();
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    if (capabilities.isCapabilityWithPrefixPresent(PERSISTENCE_CAPABILITY)) {
        generatedFiles.addAll(generatePersistenceInfo(context, aggregatedIndex, generatedBeans, resourcePatterns, runTimeConfiguration, liveReload.isLiveReload()));
    }
    Map<String, byte[]> classes = new HashMap<>();
    for (KogitoGeneratedClassesBuildItem generatedKogitoClass : generatedKogitoClasses) {
        classes.putAll(generatedKogitoClass.getGeneratedClasses());
    }
    // Json schema files
    generatedFiles.addAll(generateJsonSchema(context, aggregatedIndex, classes));
    // Write files to disk
    dumpFilesToDisk(context.getAppPaths(), generatedFiles);
    // register resources to the Quarkus environment
    registerResources(generatedFiles, staticResProducer, resource, genResBI);
    registerProcessSVG(context, resource);
}
Also used : HashMap(java.util.HashMap) IndexView(org.jboss.jandex.IndexView) KogitoGeneratedClassesBuildItem(org.kie.kogito.quarkus.common.deployment.KogitoGeneratedClassesBuildItem) ArrayList(java.util.ArrayList) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 10 with GeneratedFile

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

the class PersistenceGenerator method jdbcBasedPersistence.

private Collection<GeneratedFile> jdbcBasedPersistence() {
    ClassOrInterfaceDeclaration persistenceProviderClazz = new ClassOrInterfaceDeclaration().setName(KOGITO_PROCESS_INSTANCE_FACTORY_IMPL).setModifiers(Modifier.Keyword.PUBLIC).addExtendedType(KOGITO_PROCESS_INSTANCE_FACTORY_PACKAGE);
    Collection<GeneratedFile> generatedFiles = new ArrayList<>();
    if (context().hasDI()) {
        context().getDependencyInjectionAnnotator().withApplicationComponent(persistenceProviderClazz);
        final String datasourceClass = "javax.sql.DataSource";
        ConstructorDeclaration constructor = persistenceProviderClazz.addConstructor(Keyword.PUBLIC).addParameter(datasourceClass, "dataSource").addParameter(StaticJavaParser.parseClassOrInterfaceType(Boolean.class.getName()), AUTO_DDL).setBody(new BlockStmt().addStatement(new ExplicitConstructorInvocationStmt().setThis(false).addArgument(new NameExpr("dataSource")).addArgument(AUTO_DDL)));
        Optional<Parameter> autoDDL = constructor.getParameterByName(AUTO_DDL);
        autoDDL.ifPresent(parameter -> context().getDependencyInjectionAnnotator().withConfigInjection(parameter, KOGITO_PERSISTENCE_AUTO_DDL, Boolean.TRUE.toString()));
        context().getDependencyInjectionAnnotator().withInjection(constructor);
        // empty constructor for DI
        persistenceProviderClazz.addConstructor(Keyword.PROTECTED);
    }
    addOptimisticLockFlag(persistenceProviderClazz);
    Optional<GeneratedFile> generatedPgClientFile = generatePersistenceProviderClazz(persistenceProviderClazz, new CompilationUnit(KOGITO_PROCESS_INSTANCE_PACKAGE).addType(persistenceProviderClazz));
    generatedPgClientFile.ifPresent(generatedFiles::add);
    return generatedFiles;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Parameter(com.github.javaparser.ast.body.Parameter)

Aggregations

GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)13 ArrayList (java.util.ArrayList)8 CompilationUnit (com.github.javaparser.ast.CompilationUnit)7 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)6 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)5 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)5 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)4 SimpleName (com.github.javaparser.ast.expr.SimpleName)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Optional (java.util.Optional)4 Parameter (com.github.javaparser.ast.body.Parameter)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3