Skip to content

Commit f61c0c2

Browse files
committed
[#754] Add configuration for recursive calls of BKMs
1 parent 30e7658 commit f61c0c2

File tree

9 files changed

+38
-36
lines changed

9 files changed

+38
-36
lines changed

dmn-core/src/main/java/com/gs/dmn/runtime/interpreter/AbstractDMNInterpreter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ protected DMNContext makeDecisionGlobalContext(DRGElementReference<TDecision> re
349349
}
350350

351351
private DMNContext makeDecisionGlobalContext(TDRGElement element, Map<QualifiedName, Object> informationRequirements) {
352-
// No caching when is not strongly typed, input data types might be inferred from input data
353-
boolean strongTyping = dmnTransformer.isStrongTyping();
354-
DMNContext globalContext = strongTyping ? this.dmnTransformer.makeGlobalContext(element) : this.dmnTransformer.makeGlobalContext(element, true);
352+
DMNContext globalContext = this.dmnTransformer.makeGlobalContext(element);
355353
for (Map.Entry<QualifiedName, Object> entry: informationRequirements.entrySet()) {
356354
String key = this.dmnTransformer.bindingName(entry.getKey());
357355
globalContext.bind(key, entry.getValue());

dmn-core/src/main/java/com/gs/dmn/transformation/InputParameters.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ protected static boolean getOptionalBooleanParam(Map<String, String> parameters,
6969
private final String tckFileExtension;
7070
private final Charset charset;
7171

72+
private final boolean recursiveCalls;
73+
7274
private final String javaRootPackage;
7375
private final boolean onePackage;
7476
private final boolean singletonInputData;
@@ -110,14 +112,16 @@ public InputParameters(Map<String, String> inputParameters) {
110112
String charsetName = InputParameters.getOptionalParam(inputParameters, "encoding");
111113
this.charset = charsetName == null ? StandardCharsets.UTF_8 : Charset.forName(charsetName);
112114

115+
this.strongTyping = InputParameters.getOptionalBooleanParam(inputParameters, "strongTyping", "true");
116+
this.recursiveCalls = InputParameters.getOptionalBooleanParam(inputParameters, "recursiveCalls", "true");
117+
113118
this.javaRootPackage = InputParameters.getOptionalParam(inputParameters, "javaRootPackage");
114119
this.onePackage = InputParameters.getOptionalBooleanParam(inputParameters, "onePackage", "false");
115120
this.caching = InputParameters.getOptionalBooleanParam(inputParameters, "caching");
116121
String cachingThresholdParam = InputParameters.getOptionalParam(inputParameters, "cachingThreshold", "1");
117122
this.cachingThreshold = Integer.parseInt(cachingThresholdParam);
118123
this.singletonInputData = InputParameters.getOptionalBooleanParam(inputParameters, "singletonInputData", "true");
119124
this.singletonDecision = InputParameters.getOptionalBooleanParam(inputParameters, "singletonDecision", "false");
120-
this.strongTyping = InputParameters.getOptionalBooleanParam(inputParameters, "strongTyping", "true");
121125
this.parallelStream = InputParameters.getOptionalBooleanParam(inputParameters, "parallelStream", "false");
122126
this.generateProtoMessages = InputParameters.getOptionalBooleanParam(inputParameters, "generateProtoMessages", "false");
123127
this.generateProtoServices = InputParameters.getOptionalBooleanParam(inputParameters, "generateProtoServices", "false");
@@ -177,6 +181,14 @@ public Charset getCharset() {
177181
return this.charset;
178182
}
179183

184+
public boolean isStrongTyping() {
185+
return strongTyping;
186+
}
187+
188+
public boolean isRecursiveCalls() {
189+
return recursiveCalls;
190+
}
191+
180192
public String getJavaRootPackage() {
181193
return javaRootPackage;
182194
}
@@ -193,10 +205,6 @@ public boolean isSingletonDecision() {
193205
return singletonDecision;
194206
}
195207

196-
public boolean isStrongTyping() {
197-
return strongTyping;
198-
}
199-
200208
public boolean isCaching() {
201209
return caching;
202210
}

dmn-core/src/main/java/com/gs/dmn/transformation/basic/BasicDMNToJavaTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,11 @@ public String extractMemberFromProtoValue(String protoValue, Type type, boolean
22832283
return this.nativeFactory.extractMemberFromProtoValue(protoValue, type, staticContext);
22842284
}
22852285

2286+
@Override
2287+
public boolean isRecursiveCalls() {
2288+
return this.inputParameters.isRecursiveCalls();
2289+
}
2290+
22862291
//
22872292
// Mock testing related methods
22882293
//

dmn-core/src/main/java/com/gs/dmn/transformation/basic/BasicDMNToNativeTransformer.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -635,18 +635,6 @@ default DMNContext makeGlobalContext(TDRGElement element) {
635635
return globalContext;
636636
}
637637

638-
default DMNContext makeGlobalContext(TDRGElement element, boolean isRecursive) {
639-
DMNContext libraryContext = makeLibraryContext(element, makeBuiltInContext());
640-
DMNContext globalContext = DMNContext.of(
641-
libraryContext,
642-
DMNContextKind.GLOBAL,
643-
element,
644-
getDMNEnvironmentFactory().makeEnvironment(element, isRecursive),
645-
RuntimeEnvironment.of()
646-
);
647-
return globalContext;
648-
}
649-
650638
default DMNContext makeLibraryContext(TDRGElement element, DMNContext parentContext) {
651639
// Filter libs
652640
List<Pair<String, ELLib>> importedLibraries = findImportedLibraries(element);
@@ -846,6 +834,9 @@ default DMNContext makeRelationContext(TDRGElement element, TRelation relation,
846834
);
847835
}
848836

837+
// Supports recursive calls for BKMs
838+
boolean isRecursiveCalls();
839+
849840
//
850841
// Mock testing related methods
851842
//

dmn-core/src/main/java/com/gs/dmn/transformation/basic/DMNEnvironmentFactory.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ public interface DMNEnvironmentFactory {
6262
//
6363
Environment makeEnvironment(TDRGElement element);
6464

65-
Environment makeEnvironment(TDRGElement element, boolean isRecursive);
66-
6765
//
6866
// Decision Table
6967
//

dmn-core/src/main/java/com/gs/dmn/transformation/basic/StandardDMNEnvironmentFactory.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -766,20 +766,20 @@ public Type externalFunctionReturnFEELType(TNamedElement element, Expression<Typ
766766

767767
@Override
768768
public Environment makeEnvironment(TDRGElement element) {
769-
Environment environment = this.environmentMemoizer.get(element);
770-
if (environment == null) {
771-
environment = makeEnvironmentNoCache(element);
772-
this.environmentMemoizer.put(element, environment);
769+
// No caching when is not strongly typed, input data types might be inferred from input data
770+
if (this.dmnTransformer.isStrongTyping()) {
771+
Environment environment = this.environmentMemoizer.get(element);
772+
if (environment == null) {
773+
environment = makeEnvironmentNoCache(element);
774+
this.environmentMemoizer.put(element, environment);
775+
}
776+
return environment;
777+
} else {
778+
return makeEnvironmentNoCache(element);
773779
}
774-
return environment;
775780
}
776781

777782
private Environment makeEnvironmentNoCache(TDRGElement element) {
778-
return makeEnvironment(element, true);
779-
}
780-
781-
@Override
782-
public Environment makeEnvironment(TDRGElement element, boolean isRecursive) {
783783
TDefinitions definitions = this.dmnModelRepository.getModel(element);
784784

785785
try {
@@ -797,7 +797,8 @@ public Environment makeEnvironment(TDRGElement element, boolean isRecursive) {
797797
// Add it to cache to avoid infinite loops
798798
this.environmentMemoizer.put(element, elementEnvironment);
799799
// Add declaration of element to support recursion
800-
if (isRecursive) {
800+
boolean recursiveCalls = this.dmnTransformer.isRecursiveCalls() && this.dmnTransformer.isStrongTyping();
801+
if (recursiveCalls && element instanceof TBusinessKnowledgeModel) {
801802
Declaration declaration = makeDeclaration(element, elementEnvironment, element);
802803
addDeclaration(elementEnvironment, declaration, element, element);
803804
}

dmn-core/src/main/java/com/gs/dmn/validation/TypeRefValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private Type inferType(TDRGElement element, BasicDMNToJavaTransformer dmnTransfo
150150
type = dmnEnvironmentFactory.toFEELType(model, typeRef);
151151
} else {
152152
TExpression expression = dmnModelRepository.expression(element);
153-
DMNContext globalContext = dmnTransformer.makeGlobalContext(element, false);
153+
DMNContext globalContext = dmnTransformer.makeGlobalContext(element);
154154
type = dmnEnvironmentFactory.expressionType(element, expression, globalContext);
155155
}
156156
} catch (Exception e) {

dmn-core/src/test/java/com/gs/dmn/transformation/CL3TckDMNToJavaTransformerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void testCL3() throws Exception {
6262
doFolderTest("1.5","1157-implicit-conversions");
6363

6464
// extensions
65-
doFolderTest("1.1", "9001-recursive-function");
65+
doFolderTest("1.1", "9001-recursive-function", new Pair<>("recursiveCalls", "true"));
6666
}
6767

6868
@Test

dmn-core/src/test/java/com/gs/dmn/transformation/CL3TckDMNToKotlinTransformerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.gs.dmn.transformation;
1414

15+
import com.gs.dmn.runtime.Pair;
1516
import org.junit.jupiter.api.Test;
1617

1718
public class CL3TckDMNToKotlinTransformerTest extends AbstractTckDMNToKotlinTransformerTest {
@@ -39,6 +40,6 @@ public void testCL3() throws Exception {
3940
doFolderTest("1.1", "0030-user-defined-functions");
4041
doFolderTest("1.1", "0031-static-user-defined-functions");
4142

42-
doFolderTest("1.1", "9001-recursive-function");
43+
doFolderTest("1.1", "9001-recursive-function", new Pair<>("recursiveCalls", "true"));
4344
}
4445
}

0 commit comments

Comments
 (0)