yesamer commented on code in PR #6632:
URL:
https://github.com/apache/incubator-kie-drools/pull/6632#discussion_r3115990755
##########
kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/dialectHandlers/BFEELDialectHandlerTest.java:
##########
@@ -44,192 +44,240 @@ void setUp() {
handler = new BFEELDialectHandler();
ctx = EvaluationContextTestUtil.newEmptyEvaluationContext();
}
-
+
@Test
- void testAddOperations() {
- // String concatenation with type coercion
- assertThat(handler.executeAdd("Hello", "World",
ctx)).isEqualTo("HelloWorld");
- assertThat(handler.executeAdd("Hello", 123,
ctx)).isEqualTo("Hello123");
- assertThat(handler.executeAdd("Hello", null, ctx)).isEqualTo("Hello");
-
- // Numbers
+ void testAdditionWithPrecedenceOrder() {
+ //Basic addition operation
assertThat(handler.executeAdd(10, 20, ctx)).isEqualTo(new
BigDecimal("30"));
assertThat(handler.executeAdd(10, null, ctx)).isEqualTo(10);
+ // Case 1 : STRING - If either operand is a string, convert non-string
to string
+ // Left: string + other
+ assertThat(handler.executeAdd("Hello", 123,
ctx)).isEqualTo("Hello123");
+ assertThat(handler.executeAdd("Date:", LocalDate.of(2024, 1, 1),
ctx)).isEqualTo("Date:2024-01-01");
+ assertThat(handler.executeAdd("Duration:", Duration.ofDays(5),
ctx)).isEqualTo("Duration:PT120H");
+ assertThat(handler.executeAdd("Period:", Period.ofMonths(3),
ctx)).isEqualTo("Period:P3M");
- // Date operations - returns number instead of null
+ // Right: other + string
+ assertThat(handler.executeAdd(123, "World",
ctx)).isEqualTo("123World");
+ assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), " is the
date", ctx)).isEqualTo("2024-01-01 is the date");
+ assertThat(handler.executeAdd(Duration.ofDays(5), " duration",
ctx)).isEqualTo("PT120H duration");
+ assertThat(handler.executeAdd(Period.ofMonths(3), " period",
ctx)).isEqualTo("P3M period");
+
+ // Case 2 : NUMBER - If either operand is a number (and neither is
string), convert non-number to number
+ // Per BFEEL Section 11.3: number(date) = 0, number(duration) =
seconds, number(period) = months
+
+ // Left: date + number → 0 + number = number
assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), 5,
ctx)).isEqualTo(5);
+ assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), new
BigDecimal("10.5"), ctx))
+ .isEqualTo(new BigDecimal("10.5"));
+
+ // Right: number + date → number + 0 = number
+ assertThat(handler.executeAdd(5, LocalDate.of(2024, 1, 1),
ctx)).isEqualTo(5);
+ assertThat(handler.executeAdd(new BigDecimal("10.5"),
LocalDate.of(2024, 1, 1), ctx))
+ .isEqualTo(new BigDecimal("10.5"));
+
+ // Case 3: DATE - If either operand is a date, convert non-date to
duration
+ // Left: date + duration
assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1),
Duration.ofDays(5), ctx))
.isEqualTo(LocalDate.of(2024, 1, 6));
+ assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1),
Period.ofMonths(2), ctx))
+ .isEqualTo(LocalDate.of(2024, 3, 1));
+ assertThat(handler.executeAdd("Hello", null, ctx)).isEqualTo("Hello");
}
@Test
- void testLogicalOperations() {
- // AND with boolean coercion
- assertThat(handler.executeAnd(Boolean.TRUE, Boolean.TRUE,
ctx)).isEqualTo(Boolean.TRUE);
- assertThat(handler.executeAnd(Boolean.TRUE, Boolean.FALSE,
ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeAnd("test", Boolean.TRUE,
ctx)).isEqualTo(Boolean.FALSE); // Non-boolean coerced to false
-
- // OR operations
- assertThat(handler.executeOr(Boolean.TRUE, Boolean.FALSE,
ctx)).isEqualTo(Boolean.TRUE);
- assertThat(handler.executeOr(Boolean.FALSE, Boolean.FALSE,
ctx)).isEqualTo(Boolean.FALSE);
- }
-
- @Test
- void testEqualityOperations() {
- // Equal - returns false for different types (not null)
- assertThat(handler.executeEqual(null, null,
ctx)).isEqualTo(Boolean.TRUE);
- assertThat(handler.executeEqual(10, 10, ctx)).isEqualTo(Boolean.TRUE);
- assertThat(handler.executeEqual(10, 20, ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeEqual("10", 10,
ctx)).isEqualTo(Boolean.FALSE); // Different types
- assertThat(handler.executeEqual(null, 10,
ctx)).isEqualTo(Boolean.FALSE);
+ void testSubtractionWithPrecedenceOrder() {
+ // Basic subtraction operation
+ assertThat(handler.executeSub(20, 10, ctx)).isEqualTo(new
BigDecimal("10"));
+ // Case 1: STRING - If either operand is a string, subtraction returns
empty string ""
+ // Left: string - other
+ assertThat(handler.executeSub("Hello", 123, ctx)).isEqualTo("");
+ assertThat(handler.executeSub("Date", LocalDate.of(2024, 1, 1),
ctx)).isEqualTo("");
+ assertThat(handler.executeSub("Duration", Duration.ofDays(5),
ctx)).isEqualTo("");
- // Not Equal
- assertThat(handler.executeNotEqual(null, null,
ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeNotEqual(10, 20,
ctx)).isEqualTo(Boolean.TRUE);
- }
-
- @Test
- void testComparisonOperations() {
- // BFEEL returns false for invalid comparisons (not null)
- assertThat(handler.executeGte(Boolean.FALSE, Boolean.FALSE,
ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeGte("test", Boolean.TRUE,
ctx)).isEqualTo(Boolean.TRUE);
- assertThat(handler.executeGte(20, 10, ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeGte("test", 10,
ctx)).isEqualTo(Boolean.FALSE); // Incomparable
+ // Right: other - string
+ assertThat(handler.executeSub(123, "World", ctx)).isEqualTo("");
+ assertThat(handler.executeSub(LocalDate.of(2024, 1, 1), "date",
ctx)).isEqualTo("");
+ assertThat(handler.executeSub(Duration.ofDays(5), "duration",
ctx)).isEqualTo("");
- assertThat(handler.executeGt(20, 10, ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeLte(5, 10, ctx)).isEqualTo(Boolean.FALSE);
- assertThat(handler.executeLt(5, 10, ctx)).isEqualTo(Boolean.FALSE);
- }
-
- @Test
- void testArithmeticOperations() {
- // Power - B-FEEL implicit coercion: string "2" can be coerced to
number 2
- assertThat(handler.executePow("2", 3, ctx)).isEqualTo(new
BigDecimal("8")); // "2" coerced to 2, then 2^3 = 8
- assertThat(handler.executePow(2, 3, ctx)).isEqualTo(new
BigDecimal("8"));
- assertThat(handler.executePow(2, "3", ctx)).isEqualTo(new
BigDecimal("8")); // "3" coerced to 3
- assertThat(handler.executePow(2, "invalid",
ctx)).isEqualTo(BigDecimal.ZERO); // Invalid string returns 0
- assertThat(handler.executePow(null, 3,
ctx)).isEqualTo(BigDecimal.ZERO); // Null returns 0
- assertThat(handler.executePow(2, null,
ctx)).isEqualTo(BigDecimal.ZERO); // Null returns 0
+ // Case 2: NUMBER - If either operand is a number (and neither is
string), convert non-number to number
+ // Standard numeric subtraction applies after conversion
+ assertThat(handler.executeSub(10, 5, ctx)).isEqualTo(new
BigDecimal("5"));
+ assertThat(handler.executeSub(new BigDecimal("20.5"), new
BigDecimal("10.5"), ctx))
+ .isEqualTo(new BigDecimal("10.0"));
- // Subtraction - returns empty string for invalid
- assertThat(handler.executeSub("10", 5, ctx)).isEqualTo("");
- assertThat(handler.executeSub(20, 10, ctx)).isEqualTo(new
BigDecimal("10"));
+ // Case 3: DATE - If either operand is a date, convert non-date to
duration
+ // Left: date - duration
assertThat(handler.executeSub(LocalDate.of(2024, 1, 10),
Duration.ofDays(5), ctx))
.isEqualTo(LocalDate.of(2024, 1, 5));
+ assertThat(handler.executeSub(LocalDate.of(2024, 3, 1),
Period.ofMonths(1), ctx))
+ .isEqualTo(LocalDate.of(2024, 2, 1));
- // Multiplication - B-FEEL returns default values (never null)
- assertThat(handler.executeMult("test", 5,
ctx)).isEqualTo(BigDecimal.ZERO);
+ //Case 4 : DAYS AND TIME DURATION - Duration subtraction
+ assertThat(handler.executeSub(Duration.ofDays(10), Duration.ofDays(3),
ctx))
+ .isEqualTo(Duration.ofDays(7));
+ }
+
+ @Test
+ void testMultiplication() {
+ // basic multiplication operation
assertThat(handler.executeMult(5, 10, ctx)).isEqualTo(new
BigDecimal("50"));
+
+ // multiplication - invalid types
+ assertThat(handler.executeMult("test", 5,
ctx)).isEqualTo(BigDecimal.ZERO);
assertThat(handler.executeMult(Duration.ofDays(5), null,
ctx)).isEqualTo(Duration.ZERO);
assertThat(handler.executeMult(Period.ofMonths(3), null,
ctx)).isEqualTo(ComparablePeriod.ofMonths(0));
-
- // Case A: Same Duration Types - Duration × Duration (same type) →
zero duration (B-FEEL default)
+
+ // Duration × Duration (same type) → zero duration (B-FEEL default)
assertThat(handler.executeMult(Duration.ofDays(5), Duration.ofDays(3),
ctx)).isEqualTo(Duration.ZERO);
Review Comment:
@gitgabrio @ChinchuAjith My interpretation:
```
If type(e1) or type(e2) is … e1 * e2
The non-duration type is converted to a number using the number B-FEEL
function
```
In this case, both operands are duration types, so the conversion to
number() should not occur.
##########
kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/dialectHandlers/BFEELDialectHandlerTest.java:
##########
@@ -0,0 +1,285 @@
+/*
+ * 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.dialectHandlers;
+
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.LocalDate;
+import java.time.Period;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.kie.dmn.feel.lang.EvaluationContext;
+import org.kie.dmn.feel.lang.types.impl.ComparablePeriod;
+import org.kie.dmn.feel.util.EvaluationContextTestUtil;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Unit tests for BFEELDialectHandler - BFEEL uses type coercion and returns
default values
+ */
+class BFEELDialectHandlerTest {
+
+ private BFEELDialectHandler handler;
+ private EvaluationContext ctx;
+
+ @BeforeEach
+ void setUp() {
+ handler = new BFEELDialectHandler();
+ ctx = EvaluationContextTestUtil.newEmptyEvaluationContext();
+ }
+
+ @Test
+ void testAdditionWithPrecedenceOrder() {
+ //Basic addition operation
+ assertThat(handler.executeAdd(10, 20, ctx)).isEqualTo(new
BigDecimal("30"));
+ assertThat(handler.executeAdd(10, null, ctx)).isEqualTo(10);
+ // Case 1 : STRING - If either operand is a string, convert non-string
to string
+ // Left: string + other
+ assertThat(handler.executeAdd("Hello", 123,
ctx)).isEqualTo("Hello123");
+ assertThat(handler.executeAdd("Date:", LocalDate.of(2024, 1, 1),
ctx)).isEqualTo("Date:2024-01-01");
+ assertThat(handler.executeAdd("Duration:", Duration.ofDays(5),
ctx)).isEqualTo("Duration:PT120H");
+ assertThat(handler.executeAdd("Period:", Period.ofMonths(3),
ctx)).isEqualTo("Period:P3M");
+
+ // Right: other + string
+ assertThat(handler.executeAdd(123, "World",
ctx)).isEqualTo("123World");
+ assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), " is the
date", ctx)).isEqualTo("2024-01-01 is the date");
+ assertThat(handler.executeAdd(Duration.ofDays(5), " duration",
ctx)).isEqualTo("PT120H duration");
+ assertThat(handler.executeAdd(Period.ofMonths(3), " period",
ctx)).isEqualTo("P3M period");
+
+ // Case 2 : NUMBER - If either operand is a number (and neither is
string), convert non-number to number
+ // Per BFEEL Section 11.3: number(date) = 0, number(duration) =
seconds, number(period) = months
Review Comment:
@ChinchuAjith very good explanation!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]