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

Merged
merged 23 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
18c116c
[incubator-kie-issues#1659] Implemented FEELBuilder. Removing FEEL st…
May 8, 2024
b08b4f7
[incubator-kie-issues#1187] Split EvalHelper
May 10, 2024
dabebc4
Merge remote-tracking branch 'origin/main' into private-bamoe-issues#…
May 13, 2024
691347d
[private-bamoe-issues#1659] WIP - working compilation.
May 13, 2024
cdacd5b
[private-bamoe-issues#1659] WIP - working compilation. Put on hold fo…
May 14, 2024
b5b7d55
Merge remote-tracking branch 'origin/main' into private-bamoe-issues#…
Jun 25, 2024
1315b48
[private-bamoe-issues#1659] Implementing B-FEEL - overall working. Su…
Jun 27, 2024
ca9dbbc
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#…
Jan 8, 2025
6454b4d
[incubator-kie-issues#1742] Working status - compilation and tests su…
Jan 8, 2025
70c7668
[incubator-kie-issues#1742] Fixed as per PR suggestion
Jan 8, 2025
8912aeb
[incubator-kie-issues#1742] Extend test coverage
Jan 8, 2025
b9de803
[incubator-kie-issues#1742] WIP
Jan 8, 2025
938bfa2
[incubator-kie-issues#1742] Fixing headers
Jan 9, 2025
fd7ff21
[incubator-kie-issues#1742] WIP
Jan 9, 2025
18b8565
[incubator-kie-issues#1742] Implemented BFEEL local override management
Jan 14, 2025
3232bd2
[incubator-kie-issues#1742] Removing duplicated decision/assertions
Jan 15, 2025
539c513
[incubator-kie-issues#1742] Moving/renaming/testing "equal" method to…
Jan 15, 2025
0f0704e
[incubator-kie-issues#1742] Extending BFEEL paradigm to all functions.
Jan 15, 2025
b0e007b
[incubator-kie-issues#1742] Fixed as per PR review
Jan 16, 2025
a7d37d4
[incubator-kie-issues#1742] Fixing license header
Jan 16, 2025
af9718d
[incubator-kie-issues#1742] Fixed as per PR review
Jan 17, 2025
9c37f80
[incubator-kie-issues#1742] Fixed as per PR review
Jan 17, 2025
ff85844
[incubator-kie-issues#1742] Fixed as per PR review
Jan 20, 2025
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 @@ -19,6 +19,10 @@
package org.kie.dmn.feel.lang;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;


public enum FEELDialect {

Expand All @@ -33,15 +37,41 @@ public enum FEELDialect {

public String getNamespace() {return namespace;}

public final static Set<String> STANDARD_FEEL_URIS = getStandardFEELDialectURIS();

/**
* In current implementation, it returns <code>BFEEL</code> if provided namespace matches, otherwise returns <code>FEEL</code>
* It returns <code>BFEEL</code> if provided namespace matches,
* or <code>FEEL</code> if it matches any of the <code>STANDARD_FEEL_URIS</code>,
* or throws an <code>IllegalArgumentException</code> if no match is found
*
* @param namespace
* @return
*/
public static FEELDialect fromNamespace(String namespace) {
return Arrays.stream(FEELDialect.values())
Optional<FEELDialect> byNamespace = Arrays.stream(FEELDialect.values())
.filter(dialect -> dialect.getNamespace().equals(namespace))
.findFirst()
.orElse(FEELDialect.FEEL);
.findFirst();
if (byNamespace.isPresent()) {
return byNamespace.get();
}
Optional<FEELDialect> fromStandardFeelUris = getStandardFeelDialect(namespace);
if (fromStandardFeelUris.isPresent()) {
return fromStandardFeelUris.get();
}
throw new IllegalArgumentException("Unknown FEEL dialect '" + namespace + "'");
}

static Optional<FEELDialect> getStandardFeelDialect(String namespace) {
return STANDARD_FEEL_URIS.contains(namespace) ? Optional.of(FEEL) : Optional.empty();
}

private static Set<String> getStandardFEELDialectURIS() {
Set<String> toReturn = new HashSet<>();
toReturn.add(org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_FEEL);
toReturn.add(org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_FEEL);
toReturn.add(org.kie.dmn.model.v1_3.KieDMNModelInstrumentedBase.URI_FEEL);
toReturn.add(org.kie.dmn.model.v1_4.KieDMNModelInstrumentedBase.URI_FEEL);
toReturn.add(org.kie.dmn.model.v1_5.KieDMNModelInstrumentedBase.URI_FEEL);
return toReturn;
}
}
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;
}
}
Loading
Loading