-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-issues#854] DMN - Add implicit type conversion of date…
… to date and time when necessary (#5664) * [private-bamoe-issues#244] DMN - Add implicit type conversion of date to date and time when necessary * [private-bamoe-issues#244] WIP * [private-bamoe-issues#244] WIP * [incubator-kie-issues#854] Experiment implicit date -> date-time conversion * [incubator-kie-issues#854] Remove implicit date -> date-time conversion * [incubator-kie-issues#854] Remove implicit date -> date-time conversion * [incubator-kie-issues#854] Converting value after its evaluation, when needed * [incubator-kie-issues#854] Minor modification on test * [incubator-kie-issues#854] WIP * [incubator-kie-issues#854] Centralize Coerce behaviour * [incubator-kie-issues#854] Add tests. Removed strong assumption where Date is always to be treated as DateTime (not true). Removed redundant code on DMNType * [incubator-kie-issues#854] Enforcing coercion inside lists * [incubator-kie-issues#854] Enforcing coercion inside collections --------- Co-authored-by: BAMOE CI <bamoe.ci@ibm.com> Co-authored-by: Gabriele-Cardosi <gabriele.cardosi@ibm.com>
- Loading branch information
1 parent
d8462f8
commit 3d60586
Showing
18 changed files
with
655 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/util/CoerceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/ast/DMNContextEvaluatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.