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#1743]Return a list of dates instead of a range #6223

Merged
merged 18 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -18,6 +18,6 @@
*/
package org.kie.dmn.feel.exceptions;

public class EndpointOfRangeOfDifferentTypeException extends RuntimeException {
public class EndpointOfForIterationDifferentTypeException extends RuntimeException {
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*/
package org.kie.dmn.feel.exceptions;

public class EndpointOfRangeNotValidTypeException extends RuntimeException {
public class EndpointOfForIterationNotValidTypeException extends RuntimeException {
private static final long serialVersionUID = 1L;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
package org.kie.dmn.feel.lang.ast;

import org.antlr.v4.runtime.ParserRuleContext;
import org.kie.dmn.feel.exceptions.EndpointOfRangeNotValidTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfRangeOfDifferentTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationNotValidTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationDifferentTypeException;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.ast.forexpressioniterators.ForIteration;
import org.kie.dmn.feel.lang.types.BuiltInType;
import org.kie.dmn.feel.runtime.Range;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -82,7 +83,7 @@ public Object evaluate(EvaluationContext ctx) {
populateToReturn(0, ctx, toReturn);
LOG.trace("returning {}", toReturn);
return toReturn;
} catch (EndpointOfRangeNotValidTypeException | EndpointOfRangeOfDifferentTypeException e) {
} catch (EndpointOfForIterationNotValidTypeException | EndpointOfForIterationDifferentTypeException e) {
// ast error already reported
return null;
} finally {
Expand Down Expand Up @@ -127,13 +128,18 @@ public Type getResultType() {

private ForIteration createForIteration(EvaluationContext ctx, IterationContextNode iterationContextNode) {
LOG.trace("Creating ForIteration for {}", iterationContextNode);
ForIteration toReturn;
ForIteration toReturn = null;
String name = iterationContextNode.evaluateName(ctx);
Object result = iterationContextNode.evaluate(ctx);
Object rangeEnd = iterationContextNode.evaluateRangeEnd(ctx);
if (rangeEnd == null) {
Iterable values = result instanceof Iterable iterable? iterable : Collections.singletonList(result);
toReturn = new ForIteration(name, values);
if (result instanceof Iterable iterable) {
toReturn = new ForIteration(name, iterable);
} else if (result instanceof Range) {
toReturn = getForIteration(ctx, name, ((Range) result).getStart(), ((Range) result).getEnd());
} else {
toReturn = new ForIteration(name, Collections.singletonList(result));
}
} else {
toReturn = getForIteration(ctx, name, result, rangeEnd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.time.LocalDate;

import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.feel.exceptions.EndpointOfRangeNotValidTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfRangeOfDifferentTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationDifferentTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationNotValidTypeException;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.runtime.events.ASTEventBase;
import org.kie.dmn.feel.util.Msg;
Expand All @@ -42,25 +42,26 @@ public static ForIteration getForIteration(EvaluationContext ctx, String name, O
return new ForIteration(name, localDate, (LocalDate) end);
}
ctx.notifyEvt(() -> new ASTEventBase(FEELEvent.Severity.ERROR,
Msg.createMessage(Msg.VALUE_X_NOT_A_VALID_ENDPOINT_FOR_RANGE_BECAUSE_NOT_A_NUMBER_NOT_A_DATE, start), null));
throw new EndpointOfRangeOfDifferentTypeException();
Msg.createMessage(Msg.VALUE_X_NOT_A_VALID_ENDPOINT_FOR_FORITERATION_BECAUSE_NOT_A_NUMBER_NOT_A_DATE, start), null));
throw new EndpointOfForIterationDifferentTypeException();
}

static void validateValues(EvaluationContext ctx, Object start, Object end) {
if (start.getClass() != end.getClass()) {
ctx.notifyEvt(() -> new ASTEventBase(FEELEvent.Severity.ERROR,
Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, start, end), null));
throw new EndpointOfRangeOfDifferentTypeException();
throw new EndpointOfForIterationDifferentTypeException();
}
valueMustBeValid(ctx, start);
valueMustBeValid(ctx, end);
}

static void valueMustBeValid(EvaluationContext ctx, Object value) {
if (!(value instanceof BigDecimal) && !(value instanceof LocalDate)) {
ctx.notifyEvt(() -> new ASTEventBase(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.VALUE_X_NOT_A_VALID_ENDPOINT_FOR_RANGE_BECAUSE_NOT_A_NUMBER_NOT_A_DATE, value), null));
throw new EndpointOfRangeNotValidTypeException();
ctx.notifyEvt(() -> new ASTEventBase(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.VALUE_X_NOT_A_VALID_ENDPOINT_FOR_FORITERATION_BECAUSE_NOT_A_NUMBER_NOT_A_DATE, value), null));
throw new EndpointOfForIterationNotValidTypeException();
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import org.kie.dmn.feel.lang.FEELDialect;

import java.math.BigDecimal;
import java.time.LocalDate;

public interface Range {

enum RangeBoundary {
Expand All @@ -38,4 +41,9 @@ enum RangeBoundary {

boolean isWithUndefined();

Comparable getStart();

Comparable getEnd();


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.kie.dmn.feel.runtime.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.function.BiPredicate;

import org.kie.dmn.feel.lang.FEELDialect;
Expand Down Expand Up @@ -98,6 +100,34 @@ public boolean isWithUndefined() {
return withUndefined;
}

@Override
public Comparable getStart() {
if(lowEndPoint instanceof BigDecimal) {
BigDecimal start = (BigDecimal) lowEndPoint;
start = lowBoundary == Range.RangeBoundary.OPEN ? start.add(BigDecimal.ONE) : start;
return start;
} else if (lowEndPoint instanceof LocalDate) {
LocalDate start = (LocalDate) lowEndPoint;
start = lowBoundary == Range.RangeBoundary.OPEN ? start.plusDays(1) : start;
return start;
}
return lowEndPoint;
}

@Override
public Comparable getEnd() {
if (highEndPoint instanceof BigDecimal) {
BigDecimal end = (BigDecimal) highEndPoint;
end = highBoundary == Range.RangeBoundary.OPEN ? end.subtract(BigDecimal.ONE) : end;
return end;
} else if (highEndPoint instanceof LocalDate) {
LocalDate end = (LocalDate) highEndPoint;
end = highBoundary == Range.RangeBoundary.OPEN ? end.minusDays(1) : end;
return end;
}
return highEndPoint;
}

private Boolean finiteRangeIncludes(FEELDialect feelDialect, Object param) {
if (lowBoundary == RangeBoundary.OPEN && highBoundary == RangeBoundary.OPEN) {
return bothOrThrow(compare(feelDialect, lowEndPoint, param, (l, r) -> l.compareTo(r) < 0) , compare(feelDialect, highEndPoint, param, (l, r) -> l.compareTo(r) > 0), param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class Msg {
public static final Message2 X_TYPE_INCOMPATIBLE_WITH_Y_TYPE = new Message2("%s type incompatible with %s type");
public static final Message1 INCOMPATIBLE_TYPE_FOR_RANGE = new Message1("Type %s can not be used in a range unary test");
public static final Message1 VALUE_X_NOT_A_VALID_ENDPOINT_FOR_RANGE_BECAUSE_NOT_A_NUMBER_NOT_A_DATE = new Message1("Value %s is not a valid endpoint for range, because neither a feel:number nor a feel:date");
public static final Message1 VALUE_X_NOT_A_VALID_ENDPOINT_FOR_FORITERATION_BECAUSE_NOT_A_NUMBER_NOT_A_DATE = new Message1("Value %s is not a valid value for forIteration, because neither a feel:number nor a feel:date");
public static final Message1 EVALUATED_TO_NULL = new Message1("%s evaluated to null");
public static final Message1 IS_NULL = new Message1("%s is null");
public static final Message0 BASE_NODE_EVALUATE_CALLED = new Message0("BaseNode evaluate called");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.kie.dmn.feel.lang.ast;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -27,10 +28,14 @@

import org.junit.jupiter.api.Test;
import org.kie.dmn.feel.util.EvaluationContextTestUtil;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.types.BuiltInType;

import static org.assertj.core.api.Assertions.assertThat;
import static org.kie.dmn.feel.util.ExpressionNodeFactoryUtils.getIterationContextNode;
import static org.kie.dmn.feel.util.ExpressionNodeFactoryUtils.getListNode;
import static org.kie.dmn.feel.util.ExpressionNodeFactoryUtils.getNameRefNode;
import static org.kie.dmn.feel.util.ExpressionNodeFactoryUtils.getNestedListNode;
import static org.kie.dmn.feel.util.ExpressionNodeFactoryUtils.getRangeNode;

class ForExpressionNodeTest {

Expand Down Expand Up @@ -58,32 +63,12 @@ void evaluateNestedArray() {

}

private IterationContextNode getIterationContextNode(String variableName, BaseNode expression, String text) {
return new IterationContextNode(getNameDefNode(variableName), expression, null, text);
}

private NameDefNode getNameDefNode(String text) {
return new NameDefNode(Collections.singletonList(text), null, text);
}

private NameRefNode getNameRefNode(Type type, String text) {
return new NameRefNode(type, text);
}

private ListNode getNestedListNode(String text, Map<String, List<String>> values) {
List<BaseNode> elements = values.entrySet()
.stream()
.map(entry -> getListNode(entry.getKey(), entry.getValue()))
.map(BaseNode.class::cast)
.toList();
return new ListNode(elements, text);
}

private ListNode getListNode(String text, List<String> values) {
List<BaseNode> elements = values.stream()
.map(value -> new NumberNode(new BigDecimal(value), value))
.map(BaseNode.class::cast)
.toList();
return new ListNode(elements, text);
@Test
void evaluateRange() {
IterationContextNode x = getIterationContextNode("x", getRangeNode("[1980-01-01 .. 1980-01-03]", LocalDate.of(1980, 1, 1), LocalDate.of(1980, 1, 3), RangeNode.IntervalBoundary.CLOSED, RangeNode.IntervalBoundary.CLOSED ), "x in [1980-01-01 .. 1980-01-03]");
ForExpressionNode forExpressionNode = new ForExpressionNode(Collections.singletonList(x), getNameRefNode(BuiltInType.DATE, "x"), "for x in [1980-01-01 .. 1980-01-03] return x");
Object retrieved = forExpressionNode.evaluate(EvaluationContextTestUtil.newEmptyEvaluationContext());
assertThat(retrieved).isInstanceOf(List.class).asList().containsExactly(LocalDate.of(1980, 1, 1),
LocalDate.of(1980, 1, 2), LocalDate.of(1980, 1, 3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.junit.jupiter.api.Test;
import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.api.feel.runtime.events.FEELEventListener;
import org.kie.dmn.feel.exceptions.EndpointOfRangeNotValidTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfRangeOfDifferentTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationDifferentTypeException;
import org.kie.dmn.feel.exceptions.EndpointOfForIterationNotValidTypeException;
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.impl.FEELEventListenersManager;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -71,23 +71,23 @@ void getForIterationNotValidTest() {
try {
getForIteration(ctx, "iteration", "NOT", "VALID");
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeNotValidTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationNotValidTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
}
try {
getForIteration(ctx, "iteration", BigDecimal.valueOf(1), LocalDate.of(2021, 1, 1));
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeOfDifferentTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationDifferentTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
}
try {
getForIteration(ctx, "iteration", LocalDate.of(2021, 1, 1), BigDecimal.valueOf(1));
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeOfDifferentTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationDifferentTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
Expand All @@ -107,7 +107,7 @@ void valueMustBeValidFalseTest() {
try {
valueMustBeValid(ctx, "INVALID");
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeNotValidTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationNotValidTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
}
Expand All @@ -126,26 +126,26 @@ void validateValuesFalseTest() {
try {
validateValues(ctx, "INVALID", "INVALID");
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeNotValidTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationNotValidTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
}
try {
validateValues(ctx, BigDecimal.valueOf(1), LocalDate.of(2021, 1, 1));
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeOfDifferentTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationDifferentTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
}
try {
validateValues(ctx, LocalDate.of(2021, 1, 1), BigDecimal.valueOf(1));
} catch (Exception e) {
assertThat(e).isInstanceOf(EndpointOfRangeOfDifferentTypeException.class);
assertThat(e).isInstanceOf(EndpointOfForIterationDifferentTypeException.class);
final ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, times(1)).onEvent(captor.capture());
reset(listener);
}
}
}
}
Loading
Loading