-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 5 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…
a1749fc
[private-bamoe-issues#244] WIP
45a23b4
Merge remote-tracking branch 'origin/main' into private-bamoe-issues#244
0fb8490
[private-bamoe-issues#244] WIP
cc25601
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
00be48c
[incubator-kie-issues#854] Experiment implicit date -> date-time conv…
0b6d179
[incubator-kie-issues#854] Remove implicit date -> date-time conversion
cab1904
[incubator-kie-issues#854] Remove implicit date -> date-time conversion
e8b8a3f
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
ab679f0
[incubator-kie-issues#854] Converting value after its evaluation, whe…
e53e89f
[incubator-kie-issues#854] Minor modification on test
1227e27
[incubator-kie-issues#854] WIP
e3a9f20
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
43c65f0
[incubator-kie-issues#854] Centralize Coerce behaviour
360c576
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
ae400cc
[incubator-kie-issues#854] Add tests. Removed strong assumption where…
7704a6e
[incubator-kie-issues#854] Enforcing coercion inside lists
40d4579
[incubator-kie-issues#854] Enforcing coercion inside collections
c810775
Merge remote-tracking branch 'origin/main' into incubator-kie-issues#854
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
183 changes: 183 additions & 0 deletions
183
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,183 @@ | ||
package org.kie.dmn.core.ast; | ||
|
||
import java.io.File; | ||
import java.math.BigDecimal; | ||
import java.net.URL; | ||
import java.time.Duration; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.Collections; | ||
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.DMNType; | ||
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.impl.SimpleTypeImpl; | ||
import org.kie.dmn.core.util.DMNRuntimeUtil; | ||
import org.kie.dmn.feel.lang.types.BuiltInType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.*; | ||
|
||
public class DMNContextEvaluatorTest { | ||
|
||
private DMNResultImplFactory dmnResultFactory = new DMNResultImplFactory(); | ||
|
||
@Test | ||
public void optionallyConvertCollectionToArrayConverted() { | ||
Object item = "TESTED_OBJECT"; | ||
Object value = Collections.singleton(item); | ||
DMNType requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"string", | ||
null, | ||
false, | ||
null, | ||
null, | ||
BuiltInType.STRING); | ||
Object retrieved = DMNContextEvaluator.optionallyConvertCollectionToArray(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertEquals(item, retrieved); | ||
} | ||
|
||
@Test | ||
public void optionallyConvertCollectionToArrayNotConverted() { | ||
Object item = "TESTED_OBJECT"; | ||
Object value = Collections.singleton(item); | ||
DMNType requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"string", | ||
null, | ||
true, | ||
null, | ||
null, | ||
BuiltInType.STRING); | ||
Object retrieved = DMNContextEvaluator.optionallyConvertCollectionToArray(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertEquals(value, retrieved); | ||
value = "TESTED_OBJECT"; | ||
requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"string", | ||
null, | ||
false, | ||
null, | ||
null, | ||
BuiltInType.STRING); | ||
retrieved = DMNContextEvaluator.optionallyConvertCollectionToArray(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertEquals(value, retrieved); | ||
} | ||
|
||
@Test | ||
public void optionallyConvertDateToDateTimeConverted() { | ||
Object value = LocalDate.now(); | ||
DMNType requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"date and time", | ||
null, | ||
false, | ||
null, | ||
null, | ||
BuiltInType.DATE_TIME); | ||
Object retrieved = DMNContextEvaluator.optionallyConvertDateToDateTime(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertTrue(retrieved instanceof ZonedDateTime); | ||
ZonedDateTime zdtRetrieved = (ZonedDateTime)retrieved; | ||
assertEquals(value, zdtRetrieved.toLocalDate()); | ||
assertEquals(ZoneOffset.UTC, zdtRetrieved.getOffset()); | ||
assertEquals(0, zdtRetrieved.getHour()); | ||
assertEquals(0, zdtRetrieved.getMinute()); | ||
assertEquals(0, zdtRetrieved.getSecond()); | ||
} | ||
|
||
@Test | ||
public void optionallyConvertDateToDateTimeNotConverted() { | ||
Object value = "TEST_OBJECT"; | ||
DMNType requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"date and time", | ||
null, | ||
false, | ||
null, | ||
null, | ||
BuiltInType.DATE_TIME); | ||
Object retrieved = DMNContextEvaluator.optionallyConvertDateToDateTime(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertEquals(value, retrieved); | ||
value = LocalDate.now(); | ||
requiredType = new SimpleTypeImpl("http://www.omg.org/spec/DMN/20180521/FEEL/", | ||
"date", | ||
null, | ||
false, | ||
null, | ||
null, | ||
BuiltInType.DATE); | ||
retrieved = DMNContextEvaluator.optionallyConvertDateToDateTime(value, requiredType); | ||
assertNotNull(retrieved); | ||
assertEquals(value, retrieved); | ||
} | ||
|
||
@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"); | ||
DMNContextEvaluator.ContextEntryDef ed = ((DMNContextEvaluator) ((DecisionNodeImpl) date).getEvaluator()).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); | ||
Object retrieved = evaluated.getResult(); | ||
assertNotNull(retrieved); | ||
assertThat(retrieved instanceof LocalDateTime); | ||
//ed.getEvaluator().evaluate( runtime, result ); | ||
//contextEntryDef.getEvaluator().evaluate() | ||
|
||
// DMNExpressionEvaluator evaluator = | ||
// new DMNContextEvaluator.ContextEntryDef("fromStringToDateTime", | ||
// type, evaluator, ce ) | ||
} | ||
|
||
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()); // DMNContextFPAImpl.clone() creates DMNContextImpl | ||
return result; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in-memory date to date time expression-string conversion