ChinchuAjith commented on code in PR #6632: URL: https://github.com/apache/incubator-kie-drools/pull/6632#discussion_r3098297314
########## kie-dmn/kie-dmn-feel/src/test/java/org/kie/dmn/feel/lang/ast/dialectHandlers/BFEELDialectHandlerTest.java: ########## @@ -0,0 +1,151 @@ +/* + * 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 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 + assertThat(handler.executeAdd(10, 20, ctx)).isEqualTo(new BigDecimal("30")); + assertThat(handler.executeAdd(10, null, ctx)).isEqualTo(10); + + // Date operations - returns number instead of null + assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), 5, ctx)).isEqualTo(5); + assertThat(handler.executeAdd(LocalDate.of(2024, 1, 1), Duration.ofDays(5), ctx)) + .isEqualTo(LocalDate.of(2024, 1, 6)); + } + + @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); + + // 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 + + 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 - returns false for invalid + assertThat(handler.executePow("2", 3, ctx)).isEqualTo(Boolean.FALSE); + assertThat(handler.executePow(2, 3, ctx)).isEqualTo(new BigDecimal("8")); + + // Subtraction - returns empty string for invalid + assertThat(handler.executeSub("10", 5, ctx)).isEqualTo(""); + assertThat(handler.executeSub(20, 10, ctx)).isEqualTo(new BigDecimal("10")); + assertThat(handler.executeSub(LocalDate.of(2024, 1, 10), Duration.ofDays(5), ctx)) + .isEqualTo(LocalDate.of(2024, 1, 5)); + + // Multiplication - returns zero for invalid + assertThat(handler.executeMult("test", 5, ctx)).isEqualTo(BigDecimal.ZERO); + assertThat(handler.executeMult(5, 10, ctx)).isEqualTo(new BigDecimal("50")); + assertThat(handler.executeMult(Duration.ofDays(5), null, ctx)).isEqualTo(Duration.ZERO); + assertThat(handler.executeMult(Period.ofMonths(3), null, ctx)).isEqualTo(ComparablePeriod.ofMonths(0)); + assertThat(handler.executeMult(Duration.ofDays(5), Duration.ofDays(3), ctx)).isNull(); + + // Division - returns zero for invalid, null for division by zero + assertThat(handler.executeDivision("test", 5, ctx)).isEqualTo(BigDecimal.ZERO); + assertThat(handler.executeDivision(20, 4, ctx)).isEqualTo(new BigDecimal("5")); + assertThat(handler.executeDivision(10, Duration.ofDays(5), ctx)).isNull(); Review Comment: Got it @gitgabrio So based on further analysis, arrived at the following steps **Step 1: Identify Incompatible Types** The engine identifies that the operands for the division operation are a Number and a Duration (either years and months or days and time) **Step 2: Apply B-FEEL Implicit Coercion** In B-FEEL, when performing division and one operand is a number while the other is not, the non-number type is implicitly converted to a number using the B-FEEL number() function logic before the operation proceeds **Step 3: Translate Duration to Numeric Value** The duration is translated into its numeric semantic value as defined in the semantic domain: (The spec defines ( Section: 10.3.2 Semantics defines : 10.3.2.3.8 years and months duration)) Years and Months Duration: The value is expressed as the total number of months it contains (Years×12+Months). (Implemented already in drools -ComparablePeriod.java ) Days and Time Duration: The value is expressed as the total number of seconds it contain (duration.getSeconds()) **Step 4: Execute Number ÷ Number Calculation** The operation is then performed as a standard numeric division using the value derived in Step 3. Example: If you divide 26 by duration("P2Y"), the duration becomes 24 (months). The calculation is 26 / 24, resulting in approximately 1.0833. Step 5: Return Default Value for Errors If the operation encounters a semantic error—such as division by zero (e.g., dividing by duration("P0M"))—B-FEEL semantics are modified to return 0 everywhere standard FEEL would return null. (Spec reference 11.3 ) -- 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]
