use of org.kie.dmn.feel.runtime.Range in project drools by kiegroup.
the class FunctionInvocationNode method evaluate.
@Override
public Object evaluate(EvaluationContext ctx) {
if (this.tcFolded != null) {
return tcFolded.value;
}
FEELFunction function = null;
Object value = null;
if (name instanceof NameRefNode) {
// simple name
value = ctx.getValue(name.getText());
} else if (name instanceof QualifiedNameNode) {
QualifiedNameNode qn = (QualifiedNameNode) name;
String[] qns = qn.getPartsAsStringArray();
value = ctx.getValue(qns);
} else if (name instanceof PathExpressionNode) {
PathExpressionNode pathExpressionNode = (PathExpressionNode) name;
value = pathExpressionNode.evaluate(ctx);
} else {
value = name.evaluate(ctx);
}
if (value instanceof FEELFunction) {
function = (FEELFunction) value;
Object[] p = params.getElements().stream().map(e -> e.evaluate(ctx)).toArray(Object[]::new);
List<String> functionNameParts;
if (name instanceof NameRefNode) {
functionNameParts = Collections.singletonList(name.getText());
} else if (name instanceof QualifiedNameNode) {
functionNameParts = Arrays.asList(((QualifiedNameNode) name).getPartsAsStringArray());
} else if (name instanceof PathExpressionNode) {
functionNameParts = Collections.singletonList(function.getName());
} else {
functionNameParts = Collections.emptyList();
}
Object result = invokeTheFunction(functionNameParts, function, ctx, p);
return result;
} else if (value instanceof UnaryTest) {
if (params.getElements().size() == 1) {
Object p = params.getElements().get(0).evaluate(ctx);
return ((UnaryTest) value).apply(ctx, p);
} else {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.CAN_T_INVOKE_AN_UNARY_TEST_WITH_S_PARAMETERS_UNARY_TESTS_REQUIRE_1_SINGLE_PARAMETER, params.getElements().size())));
}
} else if (value instanceof Range) {
if (params.getElements().size() == 1) {
Object p = params.getElements().get(0).evaluate(ctx);
return ((Range) value).includes(p);
} else {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.CAN_T_INVOKE_AN_UNARY_TEST_WITH_S_PARAMETERS_UNARY_TESTS_REQUIRE_1_SINGLE_PARAMETER, params.getElements().size())));
}
}
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.CANNOT_INVOKE, MsgUtil.clipToString(value, 50))));
return null;
}
use of org.kie.dmn.feel.runtime.Range in project drools by kiegroup.
the class TypeUtil method formatValue.
public static String formatValue(final Object val, final boolean wrapForCodeUsage) {
if (val instanceof String) {
return formatString(val.toString(), wrapForCodeUsage);
} else if (val instanceof LocalDate) {
return formatDate((LocalDate) val, wrapForCodeUsage);
} else if (val instanceof LocalTime || val instanceof OffsetTime) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof LocalDateTime || val instanceof OffsetDateTime) {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else if (val instanceof ZonedDateTime) {
TemporalAccessor ta = (TemporalAccessor) val;
ZoneId zone = ta.query(TemporalQueries.zone());
if (!(zone instanceof ZoneOffset)) {
// it is a ZoneRegion
return formatDateTimeString(DateAndTimeFunction.REGION_DATETIME_FORMATTER.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return formatDateTimeString(DateAndTimeFunction.FEEL_DATE_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
}
} else if (val instanceof Duration) {
return formatDuration((Duration) val, wrapForCodeUsage);
} else if (val instanceof ChronoPeriod) {
return formatPeriod((ChronoPeriod) val, wrapForCodeUsage);
} else if (val instanceof TemporalAccessor) {
TemporalAccessor ta = (TemporalAccessor) val;
if (ta.query(TemporalQueries.localDate()) == null && ta.query(TemporalQueries.localTime()) != null && ta.query(TemporalQueries.zoneId()) != null) {
return formatTimeString(TimeFunction.FEEL_TIME.format((TemporalAccessor) val), wrapForCodeUsage);
} else {
return String.valueOf(val);
}
} else if (val instanceof List) {
return formatList((List) val, wrapForCodeUsage);
} else if (val instanceof Range) {
return formatRange((Range) val, wrapForCodeUsage);
} else if (val instanceof Map) {
return formatContext((Map) val, wrapForCodeUsage);
} else {
return String.valueOf(val);
}
}
use of org.kie.dmn.feel.runtime.Range in project drools by kiegroup.
the class DuringFunctionTest method invokeParamIsNull.
@Test
public void invokeParamIsNull() {
FunctionTestUtil.assertResultError(duringFunction.invoke((Comparable) null, (Range) new RangeImpl()), InvalidParametersEvent.class);
FunctionTestUtil.assertResultError(duringFunction.invoke((Comparable) "a", (Range) null), InvalidParametersEvent.class);
}
use of org.kie.dmn.feel.runtime.Range in project drools by kiegroup.
the class FinishedByFunctionTest method invokeParamIsNull.
@Test
public void invokeParamIsNull() {
FunctionTestUtil.assertResultError(finishedByFunction.invoke((Range) null, (Comparable) "b"), InvalidParametersEvent.class);
FunctionTestUtil.assertResultError(finishedByFunction.invoke((Range) new RangeImpl(), (Comparable) null), InvalidParametersEvent.class);
}
use of org.kie.dmn.feel.runtime.Range in project drools by kiegroup.
the class MeetsFunctionTest method invokeParamIsNull.
@Test
public void invokeParamIsNull() {
FunctionTestUtil.assertResultError(meetsFunction.invoke((Range) null, (Range) new RangeImpl()), InvalidParametersEvent.class);
FunctionTestUtil.assertResultError(meetsFunction.invoke((Range) new RangeImpl(), (Range) null), InvalidParametersEvent.class);
}
Aggregations