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#854] DMN - Add implicit type conversion of date to date and time when necessary #5664

Merged
merged 19 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
125bc4e
[private-bamoe-issues#244] DMN - Add implicit type conversion of date…
Jan 18, 2024
a1749fc
[private-bamoe-issues#244] WIP
Jan 23, 2024
45a23b4
Merge remote-tracking branch 'origin/main' into private-bamoe-issues#244
Jan 23, 2024
0fb8490
[private-bamoe-issues#244] WIP
Jan 23, 2024
cc25601
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
Jan 25, 2024
00be48c
[incubator-kie-issues#854] Experiment implicit date -> date-time conv…
Jan 25, 2024
0b6d179
[incubator-kie-issues#854] Remove implicit date -> date-time conversion
Jan 25, 2024
cab1904
[incubator-kie-issues#854] Remove implicit date -> date-time conversion
Jan 25, 2024
e8b8a3f
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
Jan 26, 2024
ab679f0
[incubator-kie-issues#854] Converting value after its evaluation, whe…
Jan 26, 2024
e53e89f
[incubator-kie-issues#854] Minor modification on test
Jan 26, 2024
1227e27
[incubator-kie-issues#854] WIP
Jan 29, 2024
e3a9f20
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
Jan 30, 2024
43c65f0
[incubator-kie-issues#854] Centralize Coerce behaviour
Jan 31, 2024
360c576
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
Jan 31, 2024
ae400cc
[incubator-kie-issues#854] Add tests. Removed strong assumption where…
Jan 31, 2024
7704a6e
[incubator-kie-issues#854] Enforcing coercion inside lists
Feb 1, 2024
40d4579
[incubator-kie-issues#854] Enforcing coercion inside collections
Feb 5, 2024
c810775
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
Feb 7, 2024
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 @@ -19,7 +19,6 @@
package org.kie.dmn.core.ast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -45,6 +44,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.kie.dmn.core.util.CoerceUtil.coerceValue;

public class DMNContextEvaluator
implements DMNExpressionEvaluator {
private static final Logger logger = LoggerFactory.getLogger( DMNContextEvaluator.class );
Expand Down Expand Up @@ -98,14 +99,7 @@ public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult d
}
EvaluatorResult er = ed.getEvaluator().evaluate( eventManager, result );
if ( er.getResultType() == ResultType.SUCCESS ) {
Object value = er.getResult();
if( ! ed.getType().isCollection() && value instanceof Collection &&
((Collection)value).size()==1 ) {
// spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
// and vice-versa
value = ((Collection)value).toArray()[0];
}

Object value = coerceValue(ed.getType(), er.getResult());
if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
// checking directly the result type...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.kie.dmn.core.util.CoerceUtil.coerceValue;

public class DMNFunctionDefinitionEvaluator
implements DMNExpressionEvaluator {
private static final Logger logger = LoggerFactory.getLogger( DMNFunctionDefinitionEvaluator.class );
Expand Down Expand Up @@ -157,8 +159,9 @@ public Object invoke(EvaluationContext ctx, Object[] params) {
closureContext.getAll().forEach(dmnContext::set);
for( int i = 0; i < params.length; i++ ) {
final String paramName = parameters.get(i).name;
if ((!performRuntimeTypeCheck) || parameters.get(i).type.isAssignableValue(params[i])) {
ctx.setValue(paramName, params[i]);
Object coercedObject = coerceValue(parameters.get(i).type, params[i]);
if ((!performRuntimeTypeCheck) || parameters.get(i).type.isAssignableValue(coercedObject)) {
ctx.setValue(paramName, coercedObject);
} else {
ctx.setValue(paramName, null);
MsgUtil.reportMessage(logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public boolean isComposite() {
public CompositeTypeImpl clone() {
return new CompositeTypeImpl( getNamespace(), getName(), getId(), isCollection(), new LinkedHashMap<>( fields), getBaseType(), getFeelType() );
}

@Override
protected boolean internalIsInstanceOf(Object o) {
if (getBaseType() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.EVALUATING;
import static org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.FAILED;
import static org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.SKIPPED;
import static org.kie.dmn.core.util.CoerceUtil.coerceValue;

public class DMNRuntimeImpl
implements DMNRuntime {
Expand Down Expand Up @@ -661,16 +662,9 @@ private boolean evaluateDecision(DMNContext context, DMNResultImpl result, Decis
return false;
}
try {
EvaluatorResult er = decision.getEvaluator().evaluate( this, result );
EvaluatorResult er = decision.getEvaluator().evaluate( this, result);
if( er.getResultType() == EvaluatorResult.ResultType.SUCCESS ) {
Object value = er.getResult();
if( ! decision.getResultType().isCollection() && value instanceof Collection &&
((Collection)value).size()==1 ) {
// spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
// and vice-versa
value = ((Collection)value).toArray()[0];
}

Object value = coerceValue(decision.getResultType(), er.getResult());
try {
if (typeCheck && !d.getResultType().isAssignableValue(value)) {
DMNMessage message = MsgUtil.reportMessage( logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.core.util;

import java.time.LocalDate;
import java.util.Collection;

import org.kie.dmn.api.core.DMNType;
import org.kie.dmn.core.impl.SimpleTypeImpl;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.util.EvalHelper;

/**
* Class used to centralize all coercion-related behavior
*/
public class CoerceUtil {

private CoerceUtil() {
// singleton class
}

public static Object coerceValue(DMNType requiredType, Object valueToCoerce) {
return (requiredType != null && valueToCoerce != null) ? actualCoerceValue(requiredType, valueToCoerce) :
valueToCoerce;
}

static Object actualCoerceValue(DMNType requiredType, Object valueToCoerce) {
Object toReturn = valueToCoerce;
if (!requiredType.isCollection() && valueToCoerce instanceof Collection &&
((Collection) valueToCoerce).size() == 1) {
// spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
// and vice-versa
return ((Collection) valueToCoerce).toArray()[0];
}
if (valueToCoerce instanceof LocalDate localDate &&
requiredType instanceof SimpleTypeImpl simpleType &&
simpleType.getFeelType() == BuiltInType.DATE_TIME) {
return EvalHelper.coerceDateTime(localDate);
}
return toReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.chrono.ChronoPeriod;
Expand Down Expand Up @@ -634,6 +635,7 @@ public void testDateAndTime() {
assertThat(ctx.get("Date-Time")).isEqualTo( ZonedDateTime.of( 2016, 12, 24, 23, 59, 0, 0, ZoneOffset.ofHours( -5 )));
assertThat(ctx.get("Date")).isEqualTo( new HashMap<String, Object>( ) {{
put( "fromString", LocalDate.of( 2015, 12, 24 ) );
put( "fromStringToDateTime", ZonedDateTime.of( 2015, 12, 24, 0, 0, 0, 0, ZoneOffset.UTC) );
put( "fromDateTime", LocalDate.of( 2016, 12, 24 ) );
put( "fromYearMonthDay", LocalDate.of( 1999, 11, 22 ) );
}});
Expand All @@ -658,6 +660,24 @@ public void testDateAndTime() {

}

@Test
public void testDateToDateTimeFunction() {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("DateToDateTimeFunction.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_A7F17D7B-F0AB-4C0B-B521-02EA26C2FBEE",
"new-file");
assertThat(dmnModel).isNotNull();
assertThat(dmnModel.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isFalse();

final DMNContext ctx = runtime.newContext();

final DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
LOG.debug("{}", dmnResult);
assertThat(dmnResult.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnResult.getMessages())).isFalse();

ZonedDateTime expected = ZonedDateTime.of(LocalDate.of(2021, 05, 31), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
assertThat(dmnResult.getDecisionResultByName("usingNormal").getResult()).isEqualTo(expected);
}

@Test
public void testFiltering() {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Person_filtering_by_age.dmn", getClass() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public void testDateAndTime() {
assertThat(ctx.get("Date-Time")).isEqualTo(ZonedDateTime.of(2016, 12, 24, 23, 59, 0, 0, ZoneOffset.ofHours(-5)));
assertThat(ctx.get("Date")).isEqualTo(new HashMap<String, Object>() {{
put("fromString", LocalDate.of(2015, 12, 24));
put( "fromStringToDateTime", ZonedDateTime.of( 2015, 12, 24, 0, 0, 0, 0, ZoneOffset.UTC) );
put("fromDateTime", LocalDate.of(2016, 12, 24));
put("fromYearMonthDay", LocalDate.of(1999, 11, 22));
}});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.kie.dmn.core.ast;

import java.io.File;
import java.math.BigDecimal;
import java.util.stream.Collectors;

import org.drools.util.FileUtils;
import org.junit.Test;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.dmn.api.core.ast.DecisionNode;
import org.kie.dmn.core.api.DMNExpressionEvaluator;
import org.kie.dmn.core.api.DMNFactory;
import org.kie.dmn.core.api.EvaluatorResult;
import org.kie.dmn.core.impl.DMNDecisionResultImpl;
import org.kie.dmn.core.impl.DMNResultImpl;
import org.kie.dmn.core.impl.DMNResultImplFactory;
import org.kie.dmn.core.util.DMNRuntimeUtil;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class DMNContextEvaluatorTest {

private DMNResultImplFactory dmnResultFactory = new DMNResultImplFactory();

@Test
public void dateToDateTime() {
File file = FileUtils.getFile("0007-date-time.dmn");
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime(file);
final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_69430b3e-17b8-430d-b760-c505bf6469f9", "dateTime Table 58");
assertThat(dmnModel).isNotNull();
assertThat(dmnModel.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isFalse();

DecisionNode date = dmnModel.getDecisionByName("Date");
DMNExpressionEvaluator dateDecisionEvaluator = ((DecisionNodeImpl) date).getEvaluator();
DMNContextEvaluator.ContextEntryDef ed = ((DMNContextEvaluator) dateDecisionEvaluator).getEntries().stream()
.filter(entry -> entry.getName().equals("fromStringToDateTime"))
.findFirst()
.orElseThrow(() -> new RuntimeException("Failed to find fromStringToDateTime ContextEntryDef"));
final DMNContext context = DMNFactory.newContext();
context.set( "dateString", "2015-12-24" );
context.set( "timeString", "00:00:01-01:00" );
context.set( "dateTimeString", "2016-12-24T23:59:00-05:00" );
context.set( "Hours", 12 );
context.set( "Minutes", 59 );
context.set( "Seconds", new BigDecimal("1.3" ) );
context.set( "Timezone", "PT-1H" );
context.set( "Year", 1999 );
context.set( "Month", 11 );
context.set( "Day", 22 );
context.set( "durationString", "P13DT2H14S" ); // <variable name="durationString" typeRef="feel:string"/>
DMNResultImpl result = createResult(dmnModel, context );
DMNExpressionEvaluator evaluator = ed.getEvaluator();
EvaluatorResult evaluated = evaluator.evaluate(runtime, result);
assertNotNull(evaluated);
assertEquals(EvaluatorResult.ResultType.SUCCESS, evaluated.getResultType());
}

private DMNResultImpl createResult(DMNModel model, DMNContext context) {
DMNResultImpl result = createResultImpl(model, context);

for (DecisionNode decision : model.getDecisions().stream().filter(d -> d.getModelNamespace().equals(model.getNamespace())).collect(Collectors.toSet())) {
result.addDecisionResult(new DMNDecisionResultImpl(decision.getId(), decision.getName()));
}
return result;
}

private DMNResultImpl createResultImpl(DMNModel model, DMNContext context) {
DMNResultImpl result = dmnResultFactory.newDMNResultImpl(model);
result.setContext(context.clone());
return result;
}
}
Loading
Loading