diff --git a/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNCompilerTest.java b/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNCompilerTest.java index 7e9874ef7da..18d96e5ef71 100644 --- a/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNCompilerTest.java +++ b/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNCompilerTest.java @@ -56,7 +56,7 @@ import static org.kie.dmn.core.util.DynamicTypeUtils.entry; import static org.kie.dmn.core.util.DynamicTypeUtils.mapOf; import static org.kie.dmn.core.util.DynamicTypeUtils.prototype; -import static org.kie.dmn.feel.codegen.feel11.CodegenTestUtil.newEmptyEvaluationContext; +import static org.kie.dmn.feel.util.EvaluationContextTestUtil.newEmptyEvaluationContext; public class DMNCompilerTest extends BaseVariantTest { diff --git a/kie-dmn/kie-dmn-feel/src/main/antlr4/org/kie/dmn/feel/parser/feel11/FEEL_1_1.g4 b/kie-dmn/kie-dmn-feel/src/main/antlr4/org/kie/dmn/feel/parser/feel11/FEEL_1_1.g4 index 4760125e128..9f04edc4532 100644 --- a/kie-dmn/kie-dmn-feel/src/main/antlr4/org/kie/dmn/feel/parser/feel11/FEEL_1_1.g4 +++ b/kie-dmn/kie-dmn-feel/src/main/antlr4/org/kie/dmn/feel/parser/feel11/FEEL_1_1.g4 @@ -135,6 +135,7 @@ type helper.popScope(); } : {_input.LT(1).getText().equals("list")}? sk=Identifier LT type GT #listType + | {_input.LT(1).getText().equals("range")}? sk=Identifier LT type GT #rangeType | {_input.LT(1).getText().equals("context")}? sk=Identifier LT Identifier COLON type ( COMMA Identifier COLON type )* GT #contextType | FUNCTION #qnType | FUNCTION LT (type ( COMMA type )*)? GT RARROW type #functionType diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerHelper.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerHelper.java index 30930577189..dc87cd9e5d7 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerHelper.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerHelper.java @@ -72,6 +72,7 @@ import org.kie.dmn.feel.lang.ast.QualifiedNameNode; import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode; import org.kie.dmn.feel.lang.ast.RangeNode; +import org.kie.dmn.feel.lang.ast.RangeTypeNode; import org.kie.dmn.feel.lang.ast.SignedUnaryNode; import org.kie.dmn.feel.lang.ast.StringNode; import org.kie.dmn.feel.lang.ast.TemporalConstantNode; @@ -136,6 +137,7 @@ import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.QUALIFIEDNAMENODE_CT; import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.QUANTIFIEDEXPRESSIONNODE_CT; import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.RANGENODE_CT; +import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.RANGETYPENODE_CT; import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.SIGNEDUNARYNODE_CT; import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.STRINGNODE_CT; import static org.kie.dmn.feel.codegen.feel11.DMNCodegenConstants.TEMPORALCONSTANTNODE_CT; @@ -431,6 +433,12 @@ public BlockStmt add(RangeNode n) { endExpression), n.getText()); } + public BlockStmt add(RangeTypeNode n) { + Expression genTypeNodeExpression = getNodeExpression(n.getGenericTypeNode()); + return addVariableDeclaratorWithObjectCreation(RANGETYPENODE_CT, NodeList.nodeList(genTypeNodeExpression), + n.getText()); + } + public BlockStmt add(SignedUnaryNode n) { Expression signExpression = getEnumExpression(n.getSign()); Expression expressionExpression = getNodeExpression(n.getExpression()); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerVisitor.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerVisitor.java index 0a509bca667..a4bef5554d3 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerVisitor.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/ASTCompilerVisitor.java @@ -50,6 +50,7 @@ import org.kie.dmn.feel.lang.ast.QualifiedNameNode; import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode; import org.kie.dmn.feel.lang.ast.RangeNode; +import org.kie.dmn.feel.lang.ast.RangeTypeNode; import org.kie.dmn.feel.lang.ast.SignedUnaryNode; import org.kie.dmn.feel.lang.ast.StringNode; import org.kie.dmn.feel.lang.ast.TemporalConstantNode; @@ -253,6 +254,12 @@ public BlockStmt visit(RangeNode n) { return compilerHelper.add(n); } + @Override + public BlockStmt visit(RangeTypeNode n) { + LOGGER.trace("visit {}", n); + return compilerHelper.add(n); + } + @Override public BlockStmt visit(SignedUnaryNode n) { LOGGER.trace("visit {}", n); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/DMNCodegenConstants.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/DMNCodegenConstants.java index 96520d8689c..f5db4e41509 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/DMNCodegenConstants.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/codegen/feel11/DMNCodegenConstants.java @@ -51,6 +51,7 @@ import org.kie.dmn.feel.lang.ast.QualifiedNameNode; import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode; import org.kie.dmn.feel.lang.ast.RangeNode; +import org.kie.dmn.feel.lang.ast.RangeTypeNode; import org.kie.dmn.feel.lang.ast.SignedUnaryNode; import org.kie.dmn.feel.lang.ast.StringNode; import org.kie.dmn.feel.lang.ast.TemporalConstantNode; @@ -142,6 +143,8 @@ public class DMNCodegenConstants { public static final ClassOrInterfaceType LISTNODE_CT = parseClassOrInterfaceType(ListNode.class.getCanonicalName()); public static final ClassOrInterfaceType LISTTYPENODE_CT = parseClassOrInterfaceType(ListTypeNode.class.getCanonicalName()); + public static final ClassOrInterfaceType RANGETYPENODE_CT = + parseClassOrInterfaceType(RangeTypeNode.class.getCanonicalName()); public static final ClassOrInterfaceType NAMEDEFNODE_CT = parseClassOrInterfaceType(NameDefNode.class.getCanonicalName()); public static final ClassOrInterfaceType NAMEDPARAMETERNODE_CT = diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/RangeTypeNode.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/RangeTypeNode.java new file mode 100644 index 00000000000..87771e9ac0c --- /dev/null +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/RangeTypeNode.java @@ -0,0 +1,48 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.dmn.feel.lang.ast; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.kie.dmn.feel.lang.EvaluationContext; +import org.kie.dmn.feel.lang.Type; +import org.kie.dmn.feel.lang.types.GenRangeType; + +public class RangeTypeNode extends TypeNode { + + private final TypeNode genericTypeNode; + + public RangeTypeNode(ParserRuleContext ctx, TypeNode gen) { + super(ctx); + this.genericTypeNode = gen; + } + + public RangeTypeNode(TypeNode genericTypeNode, String text) { + this.genericTypeNode = genericTypeNode; + this.setText(text); + } + + @Override + public Type evaluate(EvaluationContext ctx) { + Type gen = genericTypeNode.evaluate(ctx); + return new GenRangeType(gen); + } + + @Override + public T accept(Visitor v) { + return v.visit(this); + } + + public TypeNode getGenericTypeNode() { + return genericTypeNode; + } + +} diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/Visitor.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/Visitor.java index 6904d03b2d5..82775356454 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/Visitor.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/Visitor.java @@ -50,6 +50,7 @@ public interface Visitor { T visit(QualifiedNameNode n); T visit(QuantifiedExpressionNode n); T visit(RangeNode n); + T visit(RangeTypeNode n); T visit(SignedUnaryNode n); T visit(StringNode n); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/visitor/DefaultedVisitor.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/visitor/DefaultedVisitor.java index 68d5baaa16a..6eb2d6b79d5 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/visitor/DefaultedVisitor.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/ast/visitor/DefaultedVisitor.java @@ -49,6 +49,7 @@ import org.kie.dmn.feel.lang.ast.QualifiedNameNode; import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode; import org.kie.dmn.feel.lang.ast.RangeNode; +import org.kie.dmn.feel.lang.ast.RangeTypeNode; import org.kie.dmn.feel.lang.ast.SignedUnaryNode; import org.kie.dmn.feel.lang.ast.StringNode; import org.kie.dmn.feel.lang.ast.UnaryTestListNode; @@ -194,6 +195,11 @@ public T visit(RangeNode n) { return defaultVisit(n); } + @Override + public T visit(RangeTypeNode n) { + return defaultVisit(n); + } + @Override public T visit(SignedUnaryNode n) { return defaultVisit(n); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/types/GenRangeType.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/types/GenRangeType.java new file mode 100644 index 00000000000..4386040ba0d --- /dev/null +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/types/GenRangeType.java @@ -0,0 +1,64 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.dmn.feel.lang.types; + +import org.kie.dmn.feel.lang.SimpleType; +import org.kie.dmn.feel.lang.Type; +import org.kie.dmn.feel.runtime.impl.RangeImpl; + +public class GenRangeType implements SimpleType { + + /** + * Represents the "generic" type of the current list + */ + private final Type gen; + + + public GenRangeType(Type gen) { + this.gen = gen; + } + + @Override + public boolean isInstanceOf(Object o) { + if (o instanceof RangeImpl rangeImpl) { + return gen.isInstanceOf(rangeImpl.getLowEndPoint()) && gen.isInstanceOf(rangeImpl.getHighEndPoint()); + } else { + return false; + } + } + + @Override + public boolean isAssignableValue(Object value) { + if ( value == null ) { + return true; // a null-value can be assigned to any type. + } + if (!(value instanceof RangeImpl)) { + return gen.isAssignableValue(value); + } + return isInstanceOf(value); + } + + @Override + public String getName() { + return "[anonymous]"; + } + + public Type getGen() { + return gen; + } + + @Override + public boolean conformsTo(Type t) { + return (t instanceof GenRangeType && this.gen.conformsTo(((GenRangeType) t).gen)) || t == BuiltInType.RANGE; + } + +} diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ASTBuilderVisitor.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ASTBuilderVisitor.java index 62ce7781a5a..295b9fe539f 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ASTBuilderVisitor.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ASTBuilderVisitor.java @@ -49,6 +49,7 @@ import org.kie.dmn.feel.lang.ast.QualifiedNameNode; import org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode; import org.kie.dmn.feel.lang.ast.RangeNode; +import org.kie.dmn.feel.lang.ast.RangeTypeNode; import org.kie.dmn.feel.lang.ast.StringNode; import org.kie.dmn.feel.lang.ast.TypeNode; import org.kie.dmn.feel.lang.ast.UnaryTestListNode; @@ -586,6 +587,12 @@ public BaseNode visitListType(FEEL_1_1Parser.ListTypeContext ctx) { return new ListTypeNode(ctx, type); } + @Override + public BaseNode visitRangeType(FEEL_1_1Parser.RangeTypeContext ctx) { + TypeNode type = (TypeNode) visit(ctx.type()); + return new RangeTypeNode(ctx, type); + } + @Override public BaseNode visitContextType(FEEL_1_1Parser.ContextTypeContext ctx) { List pNames = new ArrayList<>(); diff --git a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ParserHelper.java b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ParserHelper.java index 3ac1ca33150..4308519335a 100644 --- a/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ParserHelper.java +++ b/kie-dmn/kie-dmn-feel/src/main/java/org/kie/dmn/feel/parser/feel11/ParserHelper.java @@ -42,6 +42,7 @@ import org.kie.dmn.feel.lang.types.DefaultBuiltinFEELTypeRegistry; import org.kie.dmn.feel.lang.types.FEELTypeRegistry; import org.kie.dmn.feel.lang.types.GenListType; +import org.kie.dmn.feel.lang.types.GenRangeType; import org.kie.dmn.feel.lang.types.ScopeImpl; import org.kie.dmn.feel.lang.types.SymbolTable; import org.kie.dmn.feel.lang.types.VariableSymbol; @@ -144,6 +145,10 @@ public void recoverScope( String name ) { scopeType = ((GenListType) scopeType).getGen(); } + if (scopeType instanceof GenRangeType) { + scopeType = ((GenRangeType) scopeType).getGen(); + } + if (resolved != null && scopeType instanceof CompositeType) { pushScope(scopeType); CompositeType type = (CompositeType) scopeType; diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/CodegenFEELUnaryTestsTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/CodegenFEELUnaryTestsTest.java index ff42ea47a6d..c598d4f9df5 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/CodegenFEELUnaryTestsTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/CodegenFEELUnaryTestsTest.java @@ -34,6 +34,7 @@ import org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor; import org.kie.dmn.feel.parser.feel11.FEELParser; import org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.util.NumberEvalHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -98,7 +99,7 @@ private List parseCompileEvaluate(String feelLiteralExpression, Object FEELEventListenersManager mgr = new FEELEventListenersManager(); SyntaxErrorListener listener = new SyntaxErrorListener(); mgr.addListener(listener); - EvaluationContext emptyContext = CodegenTestUtil.newEmptyEvaluationContext(mgr); + EvaluationContext emptyContext = EvaluationContextTestUtil.newEmptyEvaluationContext(mgr); CompiledFEELUnaryTests compiledUnaryTests = parse(feelLiteralExpression, mgr, listener); LOG.debug("{}", compiledUnaryTests); List result = compiledUnaryTests.getUnaryTests() diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/DirectCompilerTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/DirectCompilerTest.java index 9231a8b1d5d..5a8f00660d7 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/DirectCompilerTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/codegen/feel11/DirectCompilerTest.java @@ -36,6 +36,7 @@ import org.kie.dmn.feel.runtime.FEELTernaryLogicTest; import org.kie.dmn.feel.runtime.functions.CustomFEELFunction; import org.kie.dmn.feel.util.CompilerUtils; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -226,7 +227,7 @@ void filter_path_tricky1() { CompiledFEELExpression nameRef = CompilerUtils.parseCodegen("[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("x", 2); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -239,7 +240,7 @@ void filter_path_tricky2() { CompiledFEELExpression nameRef = CompilerUtils.parseCodegen("[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("x", false); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -410,7 +411,7 @@ void nameReference() { CompiledFEELExpression nameRef = parseCodegen(inputExpression, mapOf(entry("someSimpleName", BuiltInType.STRING) ) ); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("someSimpleName", 123L); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -425,7 +426,7 @@ void qualifiedName() { CompiledFEELExpression qualRef = parseCodegen(inputExpression, mapOf(entry("My Person", personType) ) ); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", mapOf( entry("Full Name", "John Doe"), entry("Age", 47) )); Object result = qualRef.apply(context); LOG.debug("{}", result); @@ -456,7 +457,7 @@ void qualifiedName2() { CompiledFEELExpression qualRef = parseCodegen(inputExpression, mapOf(entry("My Person", personType) ) ); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", new MyPerson()); Object result = qualRef.apply(context); LOG.debug("{}", result); @@ -471,7 +472,7 @@ void qualifiedName3() { CompiledFEELExpression qualRef = parseCodegen(inputExpression, mapOf(entry("a date", dateType))); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("a date", LocalDate.of(2016, 8, 2)); Object result = qualRef.apply(context); LOG.debug("{}", result); diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/ForExpressionNodeTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/ForExpressionNodeTest.java index 290abe5132f..f09f03caa66 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/ForExpressionNodeTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/ForExpressionNodeTest.java @@ -26,7 +26,7 @@ import java.util.Map; import org.junit.jupiter.api.Test; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.lang.Type; import org.kie.dmn.feel.lang.types.BuiltInType; @@ -39,7 +39,7 @@ void evaluateSimpleArray() { IterationContextNode x = getIterationContextNode("x", getListNode("[ 1, 2, 3, 4 ]", Arrays.asList("1", "2", "3", "4")), "x in [ 1, 2, 3, 4 ]"); IterationContextNode y = getIterationContextNode("y", getNameRefNode(BuiltInType.UNKNOWN, "x"), "y in x"); ForExpressionNode forExpressionNode = new ForExpressionNode(Arrays.asList(x, y), getNameRefNode(BuiltInType.UNKNOWN, "y"), "for x in [ 1, 2, 3, 4 ], y in x return y"); - Object retrieved = forExpressionNode.evaluate(CodegenTestUtil.newEmptyEvaluationContext()); + Object retrieved = forExpressionNode.evaluate(EvaluationContextTestUtil.newEmptyEvaluationContext()); assertThat(retrieved).isInstanceOf(List.class).asList(). containsExactly(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3), BigDecimal.valueOf(4)); } @@ -52,7 +52,7 @@ void evaluateNestedArray() { IterationContextNode x = getIterationContextNode("x", getNestedListNode("[ [1, 2], [3, 4] ]", firstIterationContext), "x in [ [1, 2], [3, 4] ]"); IterationContextNode y = getIterationContextNode("y", getNameRefNode(BuiltInType.UNKNOWN, "x"), "y in x"); ForExpressionNode forExpressionNode = new ForExpressionNode(Arrays.asList(x, y), getNameRefNode(BuiltInType.UNKNOWN, "y"), "for x in [ [1, 2], [3, 4] ], y in x return y"); - Object retrieved = forExpressionNode.evaluate(CodegenTestUtil.newEmptyEvaluationContext()); + Object retrieved = forExpressionNode.evaluate(EvaluationContextTestUtil.newEmptyEvaluationContext()); assertThat(retrieved).isInstanceOf(List.class).asList(). containsExactly(BigDecimal.ONE, BigDecimal.valueOf(2), BigDecimal.valueOf(3), BigDecimal.valueOf(4)); diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/RangeTypeNodeTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/RangeTypeNodeTest.java new file mode 100644 index 00000000000..aaa5a3caa02 --- /dev/null +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/RangeTypeNodeTest.java @@ -0,0 +1,52 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.dmn.feel.lang.ast; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.kie.dmn.feel.lang.types.GenRangeType; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; +import org.kie.dmn.feel.lang.types.BuiltInType; +import org.mockito.Mockito; + +class RangeTypeNodeTest { + + @Test + void evaluate() { + final CTypeNode typeNode = new CTypeNode(BuiltInType.BOOLEAN); + final RangeTypeNode rangeTypeNode = new RangeTypeNode(typeNode, "sometext"); + Assertions.assertThat(rangeTypeNode.evaluate(EvaluationContextTestUtil.newEmptyEvaluationContext())).isInstanceOf(GenRangeType.class); + } + + @Test + void accept() { + final Visitor visitor = Mockito.spy(Visitor.class); + final RangeTypeNode rangeTypeNode = new RangeTypeNode(null, "sometext"); + rangeTypeNode.accept(visitor); + Mockito.verify(visitor).visit(rangeTypeNode); + } + + @Test + void getGenTypeNode() { + final CTypeNode typeNode = new CTypeNode(BuiltInType.BOOLEAN); + final RangeTypeNode rangeTypeNode = new RangeTypeNode(typeNode, "sometext"); + Assertions.assertThat(rangeTypeNode.getGenericTypeNode()).isSameAs(typeNode); + } +} \ No newline at end of file diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/forexpressioniterators/ForIterationUtilsTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/forexpressioniterators/ForIterationUtilsTest.java index 226d2e1797d..43547f15081 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/forexpressioniterators/ForIterationUtilsTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/forexpressioniterators/ForIterationUtilsTest.java @@ -32,7 +32,7 @@ import org.mockito.ArgumentCaptor; import static org.assertj.core.api.Assertions.assertThat; -import static org.kie.dmn.feel.codegen.feel11.CodegenTestUtil.newEmptyEvaluationContext; +import static org.kie.dmn.feel.util.EvaluationContextTestUtil.newEmptyEvaluationContext; import static org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIterationUtils.getForIteration; import static org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIterationUtils.validateValues; import static org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIterationUtils.valueMustBeValid; diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELCompilerTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELCompilerTest.java index 80502839d79..f3b34fef4f9 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELCompilerTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELCompilerTest.java @@ -25,7 +25,7 @@ import java.util.List; import org.junit.jupiter.api.Test; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.codegen.feel11.CompiledFEELExpression; import org.kie.dmn.feel.lang.EvaluationContext; import org.kie.dmn.feel.lang.FEELProperty; @@ -226,7 +226,7 @@ void filter_path_tricky1() { CompiledFEELExpression nameRef = CompilerUtils.parseInterpreted("[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("x", 2); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -239,7 +239,7 @@ void filter_path_tricky2() { CompiledFEELExpression nameRef = CompilerUtils.parseInterpreted("[ {x:1, y:2}, {x:2, y:3} ][x]"); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("x", false); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -410,7 +410,7 @@ void nameReference() { CompiledFEELExpression nameRef = parseInterpreted(inputExpression, mapOf(entry("someSimpleName", BuiltInType.STRING) ) ); LOG.debug("{}", nameRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("someSimpleName", 123L); Object result = nameRef.apply(context); LOG.debug("{}", result); @@ -425,7 +425,7 @@ void qualifiedName() { CompiledFEELExpression qualRef = parseInterpreted(inputExpression, mapOf(entry("My Person", personType) ) ); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", mapOf( entry("Full Name", "John Doe"), entry("Age", 47) )); Object result = qualRef.apply(context); LOG.debug("{}", result); @@ -456,7 +456,7 @@ void qualifiedName2() { CompiledFEELExpression qualRef = parseInterpreted(inputExpression, mapOf(entry("My Person", personType) ) ); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("My Person", new MyPerson()); Object result = qualRef.apply(context); LOG.debug("{}", result); @@ -471,7 +471,7 @@ void qualifiedName3() { CompiledFEELExpression qualRef = parseInterpreted(inputExpression, mapOf(entry("a date", dateType))); LOG.debug("{}", qualRef); - EvaluationContext context = CodegenTestUtil.newEmptyEvaluationContext(); + EvaluationContext context = EvaluationContextTestUtil.newEmptyEvaluationContext(); context.setValue("a date", LocalDate.of(2016, 8, 2)); Object result = qualRef.apply(context); LOG.debug("{}", result); diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELOperatorsTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELOperatorsTest.java index 92258af10bf..5d167665c70 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELOperatorsTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/FEELOperatorsTest.java @@ -127,6 +127,8 @@ private static Collection data() { {"(function(a: context) {b: \"b\", c: \"c\"}) instance of function>->context", Boolean.TRUE , null}, {"(function(a: context) \"foo\") instance of function>->string", Boolean.FALSE , null}, {"(function(a: context, b: context) \"foo\") instance of function,context>->string", Boolean.TRUE , null}, + {"range(\"[1..3]\") instance of range", Boolean.TRUE , null}, + {"range(\"[1..3]\") instance of range", Boolean.FALSE , null} }; return addAdditionalParameters(cases, false); } diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionHelperTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionHelperTest.java index 9143712030a..b8d51268fa7 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionHelperTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionHelperTest.java @@ -30,7 +30,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.lang.EvaluationContext; import org.kie.dmn.feel.lang.impl.NamedParameter; import org.kie.dmn.feel.runtime.FEELFunction; @@ -44,7 +44,7 @@ class BaseFEELFunctionHelperTest { @BeforeEach public void setUp() { - ctx = CodegenTestUtil.newEmptyEvaluationContext(); + ctx = EvaluationContextTestUtil.newEmptyEvaluationContext(); } @Test diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java index f5deff2214d..af32bcc9c6a 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/BaseFEELFunctionTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.lang.EvaluationContext; import org.kie.dmn.feel.lang.ast.BaseNode; import org.kie.dmn.feel.lang.ast.InfixOpNode; @@ -51,7 +51,7 @@ class BaseFEELFunctionTest { @BeforeEach public void setUp() { - ctx = CodegenTestUtil.newEmptyEvaluationContext(); + ctx = EvaluationContextTestUtil.newEmptyEvaluationContext(); } @Test diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/ScorerHelperTest.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/ScorerHelperTest.java index 71f4bf6934f..9cc4098d8cb 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/ScorerHelperTest.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/runtime/functions/ScorerHelperTest.java @@ -24,7 +24,7 @@ import java.util.Random; import org.junit.jupiter.api.Test; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; +import org.kie.dmn.feel.util.EvaluationContextTestUtil; import org.kie.dmn.feel.lang.EvaluationContext; import org.kie.dmn.feel.lang.impl.NamedParameter; @@ -443,7 +443,7 @@ void typeIdentityOfParameters() { retrieved = ScoreHelper.typeIdentityOfParameters.applyAsInt(compares); assertThat(retrieved).isEqualTo(500); - originalInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String" }; + originalInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String" }; adaptedInput = originalInput; parameterTypes = new Class[] { EvaluationContext.class, String.class }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, parameterTypes); @@ -451,7 +451,7 @@ void typeIdentityOfParameters() { assertThat(retrieved).isEqualTo(1000); originalInput = new Object[] { "String" }; - adaptedInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String" }; + adaptedInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String" }; parameterTypes = new Class[] { EvaluationContext.class, String.class }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, parameterTypes); retrieved = ScoreHelper.typeIdentityOfParameters.applyAsInt(compares); @@ -465,7 +465,7 @@ void typeIdentityOfParameters() { assertThat(retrieved).isEqualTo(0); originalInput = new Object[] { "String", 34 }; - adaptedInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String", 34 }; + adaptedInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String", 34 }; parameterTypes = new Class[] { EvaluationContext.class, String.class, Integer.class }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, parameterTypes); retrieved = ScoreHelper.typeIdentityOfParameters.applyAsInt(compares); @@ -491,7 +491,7 @@ void typeIdentityOfParameters() { retrieved = ScoreHelper.typeIdentityOfParameters.applyAsInt(compares); assertThat(retrieved).isEqualTo(0); - originalInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String" }; + originalInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String" }; parameterTypes = new Class[] { EvaluationContext.class, Integer.class }; compares = new ScoreHelper.Compares(originalInput, null, parameterTypes); retrieved = ScoreHelper.typeIdentityOfParameters.applyAsInt(compares); @@ -531,20 +531,20 @@ void coercedToVarargs() { retrieved = ScoreHelper.coercedToVarargs.applyAsInt(compares); assertThat(retrieved).isEqualTo(coercedToVarargsScore); - originalInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String", 34 }; + originalInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String", 34 }; adaptedInput = new Object[] { new Object[] {"String", 34} }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, null); retrieved = ScoreHelper.coercedToVarargs.applyAsInt(compares); assertThat(retrieved).isEqualTo(coercedToVarargsScore); originalInput = new Object[] { "String", 34 }; - adaptedInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), new Object[] {"String", 34} }; + adaptedInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), new Object[] {"String", 34} }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, null); retrieved = ScoreHelper.coercedToVarargs.applyAsInt(compares); assertThat(retrieved).isEqualTo(coercedToVarargsScore); - originalInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), "String", 34 }; - adaptedInput = new Object[] { CodegenTestUtil.newEmptyEvaluationContext(), new Object[] {"String", 34} }; + originalInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), "String", 34 }; + adaptedInput = new Object[] { EvaluationContextTestUtil.newEmptyEvaluationContext(), new Object[] {"String", 34} }; compares = new ScoreHelper.Compares(originalInput, adaptedInput, null); retrieved = ScoreHelper.coercedToVarargs.applyAsInt(compares); assertThat(retrieved).isEqualTo(coercedToVarargsScore); diff --git a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/CompilerUtils.java b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/CompilerUtils.java index c201ec39ca3..3c645548ed0 100644 --- a/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/CompilerUtils.java +++ b/kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/util/CompilerUtils.java @@ -25,7 +25,6 @@ import com.github.javaparser.ast.stmt.BlockStmt; import org.antlr.v4.runtime.tree.ParseTree; import org.kie.dmn.feel.codegen.feel11.ASTCompilerVisitor; -import org.kie.dmn.feel.codegen.feel11.CodegenTestUtil; import org.kie.dmn.feel.codegen.feel11.CompiledFEELExpression; import org.kie.dmn.feel.codegen.feel11.CompilerBytecodeLoader; import org.kie.dmn.feel.lang.EvaluationContext; @@ -100,7 +99,7 @@ public static CompiledFEELExpression parseInterpreted(String input, Map org.apache.maven.plugins maven-scm-plugin - 1.11.1 run-tck-suite