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-issues#1474] Fix NPE when loading multiple Signavio MID #6080

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.kie.dmn.feel.runtime.functions.SumFunction;
import org.kie.dmn.feel.util.NumberEvalHelper;
import org.kie.dmn.model.api.DMNElement.ExtensionElements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
Expand All @@ -63,6 +65,8 @@
@XStreamAlias("MultiInstanceDecisionLogic")
public class MultiInstanceDecisionLogic {

private static Logger logger = LoggerFactory.getLogger(MultiInstanceDecisionLogic.class);

@XStreamAlias("iterationExpression")
private String iterationExpression;

Expand Down Expand Up @@ -132,6 +136,12 @@ public void compileEvaluator(DMNNode node, DMNCompilerImpl compiler, DMNCompiler
di.setEvaluator(miEvaluator);

compiler.addCallback((cCompiler, cCtx, cModel) -> {
if (cModel != model) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping MID processing for imported model: {}", cModel.getName());
}
return;
}
MIDDependenciesProcessor processor = new MIDDependenciesProcessor(midl, cModel);
addRequiredDecisions(miEvaluator, processor);
removeChildElementsFromIndex(cModel, processor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
Expand All @@ -37,6 +38,7 @@
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.dmn.api.core.ast.DMNNode;
import org.kie.dmn.model.api.DRGElement;
import org.kie.dmn.model.api.Definitions;
import org.slf4j.Logger;
Expand All @@ -50,17 +52,17 @@ public class SignavioTest {
@Test
void test() {
DMNRuntime runtime = createRuntime("Test_Signavio_multiple.dmn");

List<DMNModel> models = runtime.getModels();

DMNContext context = runtime.newContext();
context.set("persons", Arrays.asList("p1", "p2"));

DMNModel model0 = models.get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

assertThat((List<String>) evaluateAll.getContext().get("Greeting for each Person in Persons")).contains("Hello p1", "Hello p2");
}

Expand Down Expand Up @@ -142,14 +144,14 @@ private void checkSurveryMID(DMNRuntime runtime, Object numbers, Object iteratin
assertThat(evaluateAll.getDecisionResultByName("iterating").getResult()).isEqualTo(iterating);
}

private DMNRuntime createRuntime(String modelFileName) {
final KieServices ks = KieServices.Factory.get();
private DMNRuntime createRuntime(String... modelFileNames) {
final KieServices ks = KieServices.get();
final KieFileSystem kfs = ks.newKieFileSystem();

KieModuleModel kmm = ks.newKieModuleModel();
kmm.setConfigurationProperty("org.kie.dmn.profiles.signavio", "org.kie.dmn.signavio.KieDMNSignavioProfile");
kfs.writeKModuleXML(kmm.toXML());
kfs.write(ks.getResources().newClassPathResource(modelFileName, this.getClass()));
Arrays.stream(modelFileNames).forEachOrdered(f -> kfs.write(ks.getResources().newClassPathResource(f, getClass())));

KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
Expand Down Expand Up @@ -214,63 +216,63 @@ void zipFunctions() {
@SuppressWarnings("unchecked")
void midTakesCareOfRequirements() {
DMNRuntime runtime = createRuntime("Test_SignavioMID.dmn");

List<DMNModel> models = runtime.getModels();

DMNContext context = runtime.newContext();
context.set("numbers1", Arrays.asList(1,2));
context.set("numbers2", Arrays.asList(2,3));

DMNModel model0 = models.get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

List<Object> result = (List<Object>) evaluateAll.getDecisionResultByName("calculate").getResult();
assertThat(result).hasSize(6);

assertThat(result).doesNotContainNull();
}


@Test
void signavioConcatFunction() {
DMNRuntime runtime = createRuntime("Signavio_Concat.dmn");

List<DMNModel> models = runtime.getModels();

DMNContext context = runtime.newContext();
context.set("listOfNames", Arrays.asList("John", "Jane", "Doe"));

DMNModel model0 = models.get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

assertThat(evaluateAll.getDecisionResultByName("concatNames").getResult()).isEqualTo("JohnJaneDoe");
}


private void checkBothFunctionsAreWorking(DMNRuntime runtime) {
List<DMNModel> models = runtime.getModels();

DMNContext context = runtime.newContext();
context.set("names", Arrays.asList("John Doe", "Jane Doe"));
context.set("ages", Arrays.asList(37, 35));

DMNModel model0 = models.get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

assertThat((List<?>) evaluateAll.getDecisionResultByName("zipvararg").getResult()).hasSize(2);
assertThat((List<?>) evaluateAll.getDecisionResultByName("zipsinglelist").getResult()).hasSize(2);
}

@Test
void signavioIterateMultiinstanceWithComplexInputs() {
DMNRuntime runtime = createRuntime("Iterate Complex List.dmn");

DMNContext context = runtime.newContext();
Map<String, Object> johnDoe = new HashMap<>();
johnDoe.put("iD", "id-john");
Expand All @@ -279,7 +281,7 @@ void signavioIterateMultiinstanceWithComplexInputs() {
alice.put("iD", "id-alice");
alice.put("name", "Alice");
context.set("customer", Collections.singletonMap("persons", Arrays.asList(johnDoe, alice)));

DMNModel model0 = runtime.getModels().get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
Expand All @@ -291,31 +293,41 @@ void signavioIterateMultiinstanceWithComplexInputs() {
@Test
void signavioIterateMultiinstanceMultipleDecisions() {
DMNRuntime runtime = createRuntime("MID with multiple inside decisions.dmn");

DMNContext context = runtime.newContext();
context.set("names", Arrays.asList("John", "Alice"));

DMNModel model0 = runtime.getModels().get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

assertThat(evaluateAll.getDecisionResultByName("overallage").getResult()).isEqualTo(new BigDecimal("18"));
}

@Test
void signavioIterateMultiinstanceMultipleDecisionsOutside() {
DMNRuntime runtime = createRuntime("MID with outside requirement.dmn");

DMNContext context = runtime.newContext();
context.set("numbers", Arrays.asList(1,2,3));
context.set("operand", "PLUS");

DMNModel model0 = runtime.getModels().get(0);
LOG.info("EVALUATE ALL:");
DMNResult evaluateAll = runtime.evaluateAll(model0, context);
LOG.info("{}", evaluateAll);

assertThat(evaluateAll.getDecisionResultByName("sumUp").getResult()).isEqualTo(new BigDecimal("6"));
}

@Test
void signavioMultiInstanceDecisionTableWithinMultipleFiles() {
DMNRuntime dmnRuntime = createRuntime("MID with outside requirement.dmn", "survey MID SUM.dmn", "Signavio_Concat.dmn");

assertThat(dmnRuntime.getModels())
.flatExtracting(DMNModel::getDecisions)
.extracting(DMNNode::getName)
.containsOnly("sumUp", "iterating", "determineModifier", "concatNames");
}
}
Loading