Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-kogito-runtimes#2608] Generating one definitions json for each DMN model #3424

Merged
merged 7 commits into from
Mar 8, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.kie.kogito.codegen.decision;

import org.kie.dmn.api.core.DMNModel;

public class CodegenUtils {

private CodegenUtils() {
}

public static String getDefinitionsFileFromModel(DMNModel dmnModel) {
String resourcePath = dmnModel.getResource().getSourcePath();
String modelName = resourcePath.contains("/") ? resourcePath.substring(resourcePath.lastIndexOf('/') + 1) : resourcePath;
return modelName.replace(" ", "_").replace(".dmn", ".json");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -66,6 +65,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import static java.util.stream.Collectors.toList;
import static org.kie.kogito.codegen.decision.CodegenUtils.getDefinitionsFileFromModel;

public class DecisionCodegen extends AbstractGenerator {

Expand Down Expand Up @@ -134,19 +134,21 @@ private void generateAndStoreRestResources() {
List<DecisionRestResourceGenerator> rgs = new ArrayList<>(); // REST resources
List<DMNModel> models = resources.stream().map(DMNResource::getDmnModel).collect(Collectors.toList());

DMNOASResult oasResult = null;
try {
Comparator<DMNModel> nsNameComparator = Comparator.comparing(DMNModel::getNamespace).thenComparing(DMNModel::getName);
List<DMNModel> orderedModels = new ArrayList<>(models);
Collections.sort(orderedModels, nsNameComparator);
oasResult = DMNOASGeneratorFactory.generator(orderedModels).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);
}
// DMNOASResult oasResult = null;
// try {
// Comparator<DMNModel> nsNameComparator = Comparator.comparing(DMNModel::getNamespace).thenComparing(DMNModel::getName);
// List<DMNModel> orderedModels = new ArrayList<>(models);
// Collections.sort(orderedModels, nsNameComparator);
// oasResult = DMNOASGeneratorFactory.generator(orderedModels).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) {
DMNOASResult oasResult = generateAndStoreDefinitionsJson(model);

if (model.getName() == null || model.getName().isEmpty()) {
throw new RuntimeException("Model name should not be empty");
}
Expand Down Expand Up @@ -198,6 +200,19 @@ private void generateAndStoreRestResources() {
}
}

private DMNOASResult generateAndStoreDefinitionsJson(DMNModel dmnModel) {
DMNOASResult toReturn = null;
try {
toReturn = DMNOASGeneratorFactory.generator(Collections.singleton(dmnModel)).build();
String jsonContent = new ObjectMapper().writeValueAsString(toReturn.getJsonSchemaNode());
final String DMN_DEFINITIONS_JSON = getDefinitionsFileFromModel(dmnModel);
storeFile(GeneratedFileType.STATIC_HTTP_RESOURCE, DMN_DEFINITIONS_JSON, jsonContent);
} catch (Exception e) {
LOGGER.error("Error while trying to generate OpenAPI specification for the DMN models", e);
}
return toReturn;
}

private void generateAndStoreDecisionModelResourcesProvider() {
final DecisionModelResourcesProviderGenerator generator = new DecisionModelResourcesProviderGenerator(context(),
applicationCanonicalName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

import static com.github.javaparser.StaticJavaParser.parseStatement;
import static java.util.function.Predicate.not;
import static org.kie.kogito.codegen.decision.CodegenUtils.getDefinitionsFileFromModel;

public class DecisionRestResourceGenerator {

Expand Down Expand Up @@ -203,7 +204,7 @@ private void processOASAnn(MethodDeclaration dmnMethod, DecisionService ds) {
inputRef = withOASResult.getNamingPolicy().getRef(identifyInputSet);
outputRef = withOASResult.getNamingPolicy().getRef(identifyOutputSet);
}
final String DMN_DEFINITIONS_JSON = "/dmnDefinitions.json";
final String DMN_DEFINITIONS_JSON = "/" + getDefinitionsFileFromModel(dmnModel);
// MP / Quarkus
final String Q_CTX_PATH = context.getApplicationProperty("quarkus.http.root-path").filter(not("/"::equals)).orElse("");
processAnnForRef(dmnMethod,
Expand Down
Loading