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#1742] DMN: B-FEEL implementation #6213

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 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 @@ -171,7 +171,7 @@ private static class JSR223WrappingEC implements EvaluationContext {
private final List<FEELEvent> events;
// Defaulting FEELDialect to FEEL
private final FEELDialect dialect = FEELDialect.FEEL;

public JSR223WrappingEC(Map<String, Object> values, List<FEELEvent> events) {
this.values = Collections.unmodifiableMap(values);
this.events = events;
Expand Down Expand Up @@ -253,7 +253,7 @@ public Object getRootObject() {
}

@Override
public FEELDialect getDialect() {
public FEELDialect getFEELDialect() {
return dialect;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Object getRootObject() {
}

@Override
public FEELDialect getDialect() {
public FEELDialect getFEELDialect() {
// Defaulting FEELDialect to FEEL
return FEELDialect.FEEL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.CastExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.NullLiteralExpr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ public interface EvaluationContext {

Object getRootObject();

FEELDialect getDialect();
FEELDialect getFEELDialect();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.antlr.v4.runtime.ParserRuleContext;
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.FEELDialect;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.ast.infixexecutors.InfixExecutorUtils;
import org.kie.dmn.feel.lang.types.BuiltInType;
Expand Down Expand Up @@ -104,17 +105,17 @@ public Object evaluate(EvaluationContext ctx) {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "end")));
problem = true;
}
if (problem) return null;
if (problem && ctx.getFEELDialect() != FEELDialect.BFEEL) return null;

Object gte = InfixExecutorUtils.or(BooleanEvalHelper.compare(o_val, o_s, (l, r) -> l.compareTo(r) > 0),
BooleanEvalHelper.isEqual(o_val, o_s),
Object gte = InfixExecutorUtils.or(BooleanEvalHelper.compare(o_val, o_s, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) > 0),
BooleanEvalHelper.isEqual(o_val, o_s, ctx.getFEELDialect()),
ctx); // do not use Java || to avoid potential NPE due to FEEL 3vl.
if (gte == null) {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "value", "start")));
}

Object lte = InfixExecutorUtils.or(BooleanEvalHelper.compare(o_val, o_e, (l, r) -> l.compareTo(r) < 0),
BooleanEvalHelper.isEqual(o_val, o_e),
Object lte = InfixExecutorUtils.or(BooleanEvalHelper.compare(o_val, o_e, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) < 0),
BooleanEvalHelper.isEqual(o_val, o_e, ctx.getFEELDialect()),
ctx); // do not use Java || to avoid potential NPE due to FEEL 3vl.
if (lte == null) {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "value", "end")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Object evaluate(EvaluationContext ctx) {
} else if (value instanceof Range) {
if (params.getElements().size() == 1) {
Object p = params.getElements().get(0).evaluate(ctx);
return ((Range) value).includes(p);
return ((Range) value).includes(ctx.getFEELDialect(), p);
} else {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.CAN_T_INVOKE_AN_UNARY_TEST_WITH_S_PARAMETERS_UNARY_TESTS_REQUIRE_1_SINGLE_PARAMETER, params.getElements().size())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private Boolean in(EvaluationContext ctx, Object value, Object expr) {
return ((UnaryTest) expr).apply( ctx, value );
} else if ( expr instanceof Range ) {
try {
return ((Range) expr).includes( value );
return ((Range) expr).includes(ctx.getFEELDialect(), value );
} catch ( Exception e ) {
ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.EXPRESSION_IS_RANGE_BUT_VALUE_IS_NOT_COMPARABLE, value.toString(), expr.toString() ), e ) );
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.antlr.v4.runtime.ParserRuleContext;
import org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.FEELDialect;
import org.kie.dmn.feel.runtime.Range;
import org.kie.dmn.feel.runtime.UnaryTest;
import org.kie.dmn.feel.runtime.UnaryTestImpl;
Expand Down Expand Up @@ -142,7 +143,8 @@ public UnaryTest getUnaryTest() {
private UnaryTest createCompareUnaryTest( BiPredicate<Comparable, Comparable> op ) {
return (context, left) -> {
Object right = value.evaluate( context );
return BooleanEvalHelper.compare(left, right, op );
// Defaulting FEELDialect to FEEL
return BooleanEvalHelper.compare(left, right, FEELDialect.FEEL, op );
};
}

Expand All @@ -156,7 +158,7 @@ private Boolean utEqualSemantic(Object left, Object right) {
return ((Collection) right).contains(left);
} else {
// evaluate single entity
return BooleanEvalHelper.isEqual(left, right);
return BooleanEvalHelper.isEqual(left, right, FEELDialect.FEEL);
}
}

Expand All @@ -183,7 +185,7 @@ private UnaryTest createInUnaryTest() {
Object val = value.evaluate( c );
if (val instanceof Range) {
try {
return ((Range) val).includes(o);
return ((Range) val).includes(c.getFEELDialect(), o);
} catch (Exception e) {
c.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.EXPRESSION_IS_RANGE_BUT_VALUE_IS_NOT_COMPARABLE, o, val)));
throw e;
Expand Down Expand Up @@ -218,7 +220,7 @@ private UnaryTest createNotUnaryTest() {
}
} else if( test instanceof Range ) {
try {
if( ((Range)test).includes( o ) ) {
if( ((Range)test).includes(c.getFEELDialect(), o ) ) {
return false;
}
} catch ( Exception e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ public Object evaluate(Object left, Object right, EvaluationContext ctx) {

@Override
public Object evaluate(InfixOpNode infixNode, EvaluationContext ctx) {
Boolean leftAND = BooleanEvalHelper.getBooleanOrNull(infixNode.getLeft().evaluate(ctx));
Boolean leftAND = BooleanEvalHelper.getBooleanOrDialectDefault(infixNode.getLeft().evaluate(ctx),
ctx.getFEELDialect());
if (leftAND != null) {
if (leftAND.booleanValue()) {
return BooleanEvalHelper.getBooleanOrNull(infixNode.getRight().evaluate(ctx));
return BooleanEvalHelper.getBooleanOrDialectDefault(infixNode.getRight().evaluate(ctx),
ctx.getFEELDialect());
} else {
return Boolean.FALSE; //left hand operand is false, we do not need to evaluate right side
}
} else {
Boolean rightAND = BooleanEvalHelper.getBooleanOrNull(infixNode.getRight().evaluate(ctx));
return Boolean.FALSE.equals(rightAND) ? Boolean.FALSE : null;
return BooleanEvalHelper.getFalseOrDialectDefault(infixNode.getRight().evaluate(ctx), ctx.getFEELDialect());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static EqExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
return BooleanEvalHelper.isEqual(left, right);
return BooleanEvalHelper.isEqual(left, right, ctx.getFEELDialect());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static GtExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
return BooleanEvalHelper.compare(left, right, (l, r) -> l.compareTo(r) > 0);
return BooleanEvalHelper.compare(left, right, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) > 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static GteExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
return or(BooleanEvalHelper.compare(left, right, (l, r) -> l.compareTo(r) > 0),
BooleanEvalHelper.isEqual(left, right),
return or(BooleanEvalHelper.compare(left, right, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) > 0),
BooleanEvalHelper.isEqual(left, right, ctx.getFEELDialect()),
ctx); // do not use Java || to avoid potential NPE due to FEEL 3vl.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class InfixExecutorUtils {
*/
@Deprecated
public static Object or(Object left, Object right, EvaluationContext ctx) {
Boolean l = BooleanEvalHelper.getBooleanOrNull(left);
Boolean r = BooleanEvalHelper.getBooleanOrNull(right);
Boolean l = BooleanEvalHelper.getBooleanOrDialectDefault(left, ctx.getFEELDialect());
Boolean r = BooleanEvalHelper.getBooleanOrDialectDefault(right, ctx.getFEELDialect());
// have to check for all nulls first to avoid NPE
if ((l == null && r == null) || (l == null && r == false) || (r == null && l == false)) {
return null;
Expand All @@ -65,8 +65,8 @@ public static Object or(Object left, Object right, EvaluationContext ctx) {
*/
@Deprecated
public static Object and(Object left, Object right, EvaluationContext ctx) {
Boolean l = BooleanEvalHelper.getBooleanOrNull(left);
Boolean r = BooleanEvalHelper.getBooleanOrNull(right);
Boolean l = BooleanEvalHelper.getBooleanOrDialectDefault(left, ctx.getFEELDialect());
Boolean r = BooleanEvalHelper.getBooleanOrDialectDefault(right, ctx.getFEELDialect());
// have to check for all nulls first to avoid NPE
if ((l == null && r == null) || (l == null && r == true) || (r == null && l == true)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static LtExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
return BooleanEvalHelper.compare(left, right, (l, r) -> l.compareTo(r) < 0);
return BooleanEvalHelper.compare(left, right, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) < 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static LteExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
return or(BooleanEvalHelper.compare(left, right, (l, r) -> l.compareTo(r) < 0),
BooleanEvalHelper.isEqual(left, right),
return or(BooleanEvalHelper.compare(left, right, ctx.getFEELDialect(), (l, r) -> l.compareTo(r) < 0),
BooleanEvalHelper.isEqual(left, right, ctx.getFEELDialect()),
ctx); // do not use Java || to avoid potential NPE due to FEEL 3vl.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static NeExecutor instance() {

@Override
public Object evaluate(Object left, Object right, EvaluationContext ctx) {
Boolean result = BooleanEvalHelper.isEqual(left, right);
Boolean result = BooleanEvalHelper.isEqual(left, right, ctx.getFEELDialect());
return result != null ? !result : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.ast.InfixOpNode;
import org.kie.dmn.feel.util.BooleanEvalHelper;
import org.kie.dmn.feel.util.EvalHelper;

import static org.kie.dmn.feel.lang.ast.infixexecutors.InfixExecutorUtils.or;

Expand All @@ -43,16 +42,15 @@ public Object evaluate(Object left, Object right, EvaluationContext ctx) {

@Override
public Object evaluate(InfixOpNode infixNode, EvaluationContext ctx) {
Boolean leftOR = BooleanEvalHelper.getBooleanOrNull(infixNode.getLeft().evaluate(ctx));
Boolean leftOR = BooleanEvalHelper.getBooleanOrDialectDefault(infixNode.getLeft().evaluate(ctx), ctx.getFEELDialect());
if (leftOR != null) {
if (!leftOR.booleanValue()) {
return BooleanEvalHelper.getBooleanOrNull(infixNode.getRight().evaluate(ctx));
return BooleanEvalHelper.getBooleanOrDialectDefault(infixNode.getRight().evaluate(ctx), ctx.getFEELDialect());
} else {
return Boolean.TRUE; //left hand operand is true, we do not need to evaluate right side
}
} else {
Boolean rightOR = BooleanEvalHelper.getBooleanOrNull(infixNode.getRight().evaluate(ctx));
return Boolean.TRUE.equals(rightOR) ? Boolean.TRUE : null;
return BooleanEvalHelper.getTrueOrDialectDefault(infixNode.getRight().evaluate(ctx), ctx.getFEELDialect());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public Object getRootObject() {
}

@Override
public FEELDialect getDialect() {
public FEELDialect getFEELDialect() {
return feelDialect;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public ClassLoader getRootClassLoader() {
}

@Override
public FEELDialect getDialect() {
return wrapped.getDialect();
public FEELDialect getFEELDialect() {
return wrapped.getFEELDialect();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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>
gitgabrio marked this conversation as resolved.
Show resolved Hide resolved
* 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.runtime;

public interface FEELBooleanFunction extends FEELFunction {

@Override
default Object defaultValue() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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>
gitgabrio marked this conversation as resolved.
Show resolved Hide resolved
* 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.runtime;

import java.util.Collections;

public interface FEELCollectionFunction extends FEELFunction {

@Override
default Object defaultValue() {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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>
gitgabrio marked this conversation as resolved.
Show resolved Hide resolved
* 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.runtime;

import org.kie.dmn.feel.lang.types.impl.ComparablePeriod;

public interface FEELDurationFunction extends FEELFunction {

@Override
default Object defaultValue() {
return ComparablePeriod.parse("P0M");
}
}
Loading
Loading