ChinchuAjith commented on code in PR #6632:
URL:
https://github.com/apache/incubator-kie-drools/pull/6632#discussion_r3109745110
##########
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:
Hi @gitgabrio Reached in that assumption based on a chain of semantic rules
defined across different sections of the DMN 1.6 specification.
1. FEEL Table 60: Duration * Duration is "Not Allowed".
2. FEEL Section 7.3.8: "Not Allowed" operations return null.
3. B-FEEL Section 11.1: Any operation returning null in FEEL due to error
must return a non-null default.
4. B-FEEL Section 11.6: The default for durations is 0 months or 0 seconds
(
duration(“P0M”) )
Thus, in B-FEEL, multiplying Duration.ofDays(5) by Duration.ofDays(3)
results in a duration of 0 seconds (represented in Java as Duration.ZERO),
because this operation is "Not allowed" in standard FEEL and is modified in the
business dialect to return a non-null default value. WDYT?
--
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]