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

Support generic range type in DMN engine #6123

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -431,6 +433,12 @@ public BlockStmt add(RangeNode n) {
endExpression), n.getText());
}

public BlockStmt add(RangeTypeNode n) {
Expression genTypeNodeExpression = getNodeExpression(n.getGenTypeNode());
return addVariableDeclaratorWithObjectCreation(RANGETYPENODE_CT, NodeList.nodeList(genTypeNodeExpression),
n.getText());
}

public BlockStmt add(SignedUnaryNode n) {
Expression signExpression = getEnumExpression(n.getSign());
Expression expressionExpression = getNodeExpression(n.getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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 genTypeNode;

public RangeTypeNode(ParserRuleContext ctx, TypeNode gen) {
super(ctx);
this.genTypeNode = gen;
}

public RangeTypeNode(TypeNode genTypeNode, String text) {
this.genTypeNode = genTypeNode;
this.setText(text);
}

@Override
public Type evaluate(EvaluationContext ctx) {
Type gen = genTypeNode.evaluate(ctx);
return new GenRangeType(gen);
}

@Override
public <T> T accept(Visitor<T> v) {
return v.visit(this);
}

public TypeNode getGenTypeNode() {
return genTypeNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public interface Visitor<T> {
T visit(QualifiedNameNode n);
T visit(QuantifiedExpressionNode n);
T visit(RangeNode n);
T visit(RangeTypeNode n);
T visit(SignedUnaryNode n);
T visit(StringNode n);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> pNames = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ private static Collection<Object[]> data() {
{"(function(a: context<a: string>) {b: \"b\", c: \"c\"}) instance of function<context<a: string, b: string>>->context<b: string>", Boolean.TRUE , null},
{"(function(a: context<a: string, b: string>) \"foo\") instance of function<context<a: string>>->string", Boolean.FALSE , null},
{"(function(a: context<a: string>, b: context<a: string, b: string>) \"foo\") instance of function<context<a: string, b: string>,context<a: string, b: string, c: string>>->string", Boolean.TRUE , null},
{"range(\"[1..3]\") instance of range<number>", Boolean.TRUE , null},
{"range(\"[1..3]\") instance of range<string>", Boolean.FALSE , null}
};
return addAdditionalParameters(cases, false);
}
Expand Down
1 change: 0 additions & 1 deletion kie-dmn/kie-dmn-tck/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.1</version>
<executions>
<execution>
<id>run-tck-suite</id>
Expand Down
Loading