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