Search in sources :

Example 1 with DecisionCloudEventMetaFactoryGenerator

use of org.kie.kogito.codegen.decision.events.DecisionCloudEventMetaFactoryGenerator in project kogito-runtimes by kiegroup.

the class DecisionCodegen method generateAndStoreRestResources.

private void generateAndStoreRestResources() {
    // REST resources
    List<DecisionRestResourceGenerator> rgs = new ArrayList<>();
    List<DMNModel> models = resources.stream().map(DMNResource::getDmnModel).collect(Collectors.toList());
    DMNOASResult oasResult = null;
    try {
        oasResult = DMNOASGeneratorFactory.generator(models).build();
        String jsonContent = new ObjectMapper().writeValueAsString(oasResult.getJsonSchemaNode());
        storeFile(GeneratedFileType.STATIC_HTTP_RESOURCE, "dmnDefinitions.json", jsonContent);
    } catch (Exception e) {
        LOGGER.error("Error while trying to generate OpenAPI specification for the DMN models", e);
    }
    for (DMNModel model : models) {
        if (model.getName() == null || model.getName().isEmpty()) {
            throw new RuntimeException("Model name should not be empty");
        }
        boolean stronglyTypedEnabled = Optional.ofNullable(context()).flatMap(c -> c.getApplicationProperty(STRONGLY_TYPED_CONFIGURATION_KEY)).map(Boolean::parseBoolean).orElse(false);
        if (stronglyTypedEnabled) {
            generateStronglyTypedInput(model);
        }
        DecisionRestResourceGenerator resourceGenerator = new DecisionRestResourceGenerator(context(), model, applicationCanonicalName()).withStronglyTyped(stronglyTypedEnabled).withOASResult(oasResult, isMPAnnotationsPresent(), isIOSwaggerOASv3AnnotationsPresent());
        rgs.add(resourceGenerator);
    }
    if (context().hasRESTForGenerator(this)) {
        for (DecisionRestResourceGenerator resourceGenerator : rgs) {
            if (context().getAddonsConfig().usePrometheusMonitoring()) {
                generateAndStoreGrafanaDashboards(resourceGenerator);
            }
            storeFile(REST_TYPE, resourceGenerator.generatedFilePath(), resourceGenerator.generate());
        }
    }
    DMNMarshaller marshaller = DMNMarshallerFactory.newDefaultMarshaller();
    for (DMNResource resource : resources) {
        DMNModel model = resource.getDmnModel();
        Definitions definitions = model.getDefinitions();
        for (DRGElement drg : definitions.getDrgElement()) {
            if (drg instanceof Decision) {
                Decision decision = (Decision) drg;
                decision.setExpression(null);
            } else if (drg instanceof BusinessKnowledgeModel) {
                BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) drg;
                bkm.setEncapsulatedLogic(null);
            }
        }
        String relativePath = CodegenStringUtil.escapeIdentifier(model.getNamespace()).replace(".", "/") + "/" + CodegenStringUtil.escapeIdentifier(model.getName()) + ".dmn_nologic";
        storeFile(GeneratedFileType.INTERNAL_RESOURCE, relativePath, marshaller.marshal(definitions));
    }
    if (context().getAddonsConfig().useCloudEvents()) {
        final DecisionCloudEventMetaFactoryGenerator ceMetaFactoryGenerator = new DecisionCloudEventMetaFactoryGenerator(context(), models);
        storeFile(REST_TYPE, ceMetaFactoryGenerator.generatedFilePath(), ceMetaFactoryGenerator.generate());
    }
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) DMNOASResult(org.kie.dmn.openapi.model.DMNOASResult) DecisionCloudEventMetaFactoryGenerator(org.kie.kogito.codegen.decision.events.DecisionCloudEventMetaFactoryGenerator) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) Decision(org.kie.dmn.model.api.Decision) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNModel(org.kie.dmn.api.core.DMNModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DRGElement(org.kie.dmn.model.api.DRGElement)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 DMNModel (org.kie.dmn.api.core.DMNModel)1 DMNMarshaller (org.kie.dmn.api.marshalling.DMNMarshaller)1 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)1 DRGElement (org.kie.dmn.model.api.DRGElement)1 Decision (org.kie.dmn.model.api.Decision)1 Definitions (org.kie.dmn.model.api.Definitions)1 DMNOASResult (org.kie.dmn.openapi.model.DMNOASResult)1 DecisionCloudEventMetaFactoryGenerator (org.kie.kogito.codegen.decision.events.DecisionCloudEventMetaFactoryGenerator)1