Search in sources :

Example 1 with CollectedResource

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

the class DecisionCloudEventMetaFactoryGeneratorTest method buildCodegen.

private static DecisionCodegen buildCodegen(boolean withCloudEvents) {
    KogitoBuildContext context = QuarkusKogitoBuildContext.builder().withAddonsConfig(AddonsConfig.builder().withCloudEvents(withCloudEvents).build()).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(Paths.get(MODEL_PATH).toAbsolutePath());
    return DecisionCodegen.ofCollectedResources(context, collectedResources);
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) QuarkusKogitoBuildContext(org.kie.kogito.codegen.api.context.impl.QuarkusKogitoBuildContext)

Example 2 with CollectedResource

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

the class DecisionCodegen method loadModelsAndValidate.

private void loadModelsAndValidate() {
    Map<Resource, CollectedResource> r2cr = cResources.stream().collect(Collectors.toMap(CollectedResource::resource, Function.identity()));
    // First, we perform static validation on directly the XML
    DecisionValidation.dmnValidateResources(context(), r2cr.keySet());
    // DMN model processing; any semantic error during compilation will also be thrown accordingly
    DMNRuntime dmnRuntime = DMNRuntimeBuilder.fromDefaults().setRootClassLoader(// KOGITO-4788
    context().getClassLoader()).buildConfiguration().fromResources(r2cr.keySet()).getOrElseThrow(e -> new RuntimeException("Error compiling DMN model(s)", e));
    // Any post-compilation of the DMN model validations: DT (static) analysis
    DecisionValidation.dmnValidateDecisionTablesInModels(context(), dmnRuntime.getModels());
    List<DMNResource> dmnResources = dmnRuntime.getModels().stream().map(model -> new DMNResource(model, r2cr.get(model.getResource()))).collect(toList());
    resources.addAll(dmnResources);
}
Also used : DMNTypeSafePackageName(org.kie.dmn.typesafe.DMNTypeSafePackageName) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) Function(java.util.function.Function) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) CodegenStringUtil(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil) DMNModel(org.kie.dmn.api.core.DMNModel) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) AbstractGenerator(org.kie.kogito.codegen.core.AbstractGenerator) Map(java.util.Map) Decision(org.kie.dmn.model.api.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Path(java.nio.file.Path) DMNTypeSafeTypeGenerator(org.kie.dmn.typesafe.DMNTypeSafeTypeGenerator) DMNOASGeneratorFactory(org.kie.dmn.openapi.DMNOASGeneratorFactory) DecisionConfigGenerator(org.kie.kogito.codegen.decision.config.DecisionConfigGenerator) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) Logger(org.slf4j.Logger) DMNOASResult(org.kie.dmn.openapi.model.DMNOASResult) GeneratedFileType(org.kie.kogito.codegen.api.GeneratedFileType) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApplicationSection(org.kie.kogito.codegen.api.ApplicationSection) OASFactoryResolver(org.eclipse.microprofile.openapi.spi.OASFactoryResolver) Collectors(java.util.stream.Collectors) Resource(org.kie.api.io.Resource) List(java.util.List) DecisionCloudEventMetaFactoryGenerator(org.kie.kogito.codegen.decision.events.DecisionCloudEventMetaFactoryGenerator) Collectors.toList(java.util.stream.Collectors.toList) DMNRuntimeBuilder(org.kie.dmn.core.internal.utils.DMNRuntimeBuilder) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) KogitoGAV(org.kie.kogito.KogitoGAV) DashboardGeneratedFileUtils(org.kie.kogito.codegen.core.DashboardGeneratedFileUtils) CollectedResourceProducer(org.kie.kogito.codegen.core.io.CollectedResourceProducer) Entry(java.util.Map.Entry) Optional(java.util.Optional) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) DRGElement(org.kie.dmn.model.api.DRGElement) DMNAllTypesIndex(org.kie.dmn.typesafe.DMNAllTypesIndex) GrafanaConfigurationWriter(org.kie.kogito.grafana.GrafanaConfigurationWriter) DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) Resource(org.kie.api.io.Resource) DMNRuntime(org.kie.dmn.api.core.DMNRuntime)

Example 3 with CollectedResource

use of org.kie.kogito.codegen.api.io.CollectedResource 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 4 with CollectedResource

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

the class CustomDashboardGeneratedUtilsTest method getMappedJsons.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void getMappedJsons(KogitoBuildContext.Builder contextBuilder) {
    final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(context.getAppPaths().getPaths());
    Map<String, List<Resource>> retrieved = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
    // 2 = valid *.json files inside src/test/META-INF/dashboards
    assertEquals(2, retrieved.size());
    assertEquals(1, retrieved.get(OPERATIONAL_DASHBOARD_PREFIX).size());
    assertEquals(1, retrieved.get(DOMAIN_DASHBOARD_PREFIX).size());
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with CollectedResource

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

the class CustomDashboardGeneratedUtilsTest method addToGeneratedFiles.

@ParameterizedTest
@MethodSource("org.kie.kogito.codegen.api.utils.KogitoContextTestUtils#contextBuilders")
void addToGeneratedFiles(KogitoBuildContext.Builder contextBuilder) {
    final KogitoBuildContext context = contextBuilder.withAppPaths(AppPaths.fromTestDir(new File(".").toPath())).build();
    Collection<CollectedResource> collectedResources = CollectedResourceProducer.fromPaths(context.getAppPaths().getPaths());
    Map<String, List<Resource>> dashboardJsonsMap = CustomDashboardGeneratedUtils.getMappedJsons(collectedResources);
    Collection<GeneratedFile> toPopulate = new ArrayList<>();
    CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX), toPopulate, operationalFunction, OPERATIONAL_DASHBOARD_PREFIX);
    assertEquals(dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).size(), toPopulate.size());
    String sourcePath = dashboardJsonsMap.get(OPERATIONAL_DASHBOARD_PREFIX).get(0).getSourcePath();
    String originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
    validateGeneratedFile(toPopulate.iterator().next(), OPERATIONAL_DASHBOARD_PREFIX, originalFileName);
    toPopulate = new ArrayList<>();
    CustomDashboardGeneratedUtils.addToGeneratedFiles(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX), toPopulate, domainFunction, DOMAIN_DASHBOARD_PREFIX);
    assertEquals(dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).size(), toPopulate.size());
    sourcePath = dashboardJsonsMap.get(DOMAIN_DASHBOARD_PREFIX).get(0).getSourcePath();
    originalFileName = sourcePath.substring(sourcePath.lastIndexOf(File.separator) + 1);
    validateGeneratedFile(toPopulate.iterator().next(), DOMAIN_DASHBOARD_PREFIX, originalFileName);
}
Also used : CollectedResource(org.kie.kogito.codegen.api.io.CollectedResource) ArrayList(java.util.ArrayList) KogitoBuildContext(org.kie.kogito.codegen.api.context.KogitoBuildContext) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

CollectedResource (org.kie.kogito.codegen.api.io.CollectedResource)15 KogitoBuildContext (org.kie.kogito.codegen.api.context.KogitoBuildContext)12 GeneratedFile (org.kie.kogito.codegen.api.GeneratedFile)10 List (java.util.List)9 Collection (java.util.Collection)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ArrayList (java.util.ArrayList)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 GeneratedFileType (org.kie.kogito.codegen.api.GeneratedFileType)5 File (java.io.File)4 Path (java.nio.file.Path)4 Paths (java.nio.file.Paths)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 CollectedResourceProducer (org.kie.kogito.codegen.core.io.CollectedResourceProducer)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 Collectors.toList (java.util.stream.Collectors.toList)3 AbstractGenerator (org.kie.kogito.codegen.core.AbstractGenerator)3