Search in sources :

Example 1 with ComparablePeriod

use of org.kie.dmn.feel.lang.types.impl.ComparablePeriod in project drools by kiegroup.

the class RangeNode method convertToComparable.

private Comparable convertToComparable(EvaluationContext ctx, Object s) {
    Comparable start;
    if (s == null) {
        start = null;
    } else if (s instanceof Comparable) {
        start = (Comparable) s;
    } else if (s instanceof Period) {
        // period has special semantics
        start = new ComparablePeriod((Period) s);
    } else {
        ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.INCOMPATIBLE_TYPE_FOR_RANGE, s.getClass().getSimpleName())));
        start = null;
    }
    return start;
}
Also used : ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) Period(java.time.Period) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod)

Example 2 with ComparablePeriod

use of org.kie.dmn.feel.lang.types.impl.ComparablePeriod in project drools by kiegroup.

the class YearsAndMonthsFunction method invoke.

public FEELFnResult<TemporalAmount> invoke(@ParameterName("from") Temporal from, @ParameterName("to") Temporal to) {
    if (from == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null"));
    }
    if (to == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "cannot be null"));
    }
    final LocalDate fromDate = getLocalDateFromTemporal(from);
    if (fromDate == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "is of type not suitable for years and months function"));
    }
    final LocalDate toDate = getLocalDateFromTemporal(to);
    if (toDate == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "is of type not suitable for years and months function"));
    }
    return FEELFnResult.ofResult(new ComparablePeriod(Period.between(fromDate, toDate).withDays(0)));
}
Also used : ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) LocalDate(java.time.LocalDate)

Example 3 with ComparablePeriod

use of org.kie.dmn.feel.lang.types.impl.ComparablePeriod in project drools by kiegroup.

the class DTAnalysisMeta method boundAsExpression.

private static Expression boundAsExpression(Bound<?> bound) {
    Comparable<?> value = bound.getValue();
    Expression valueExpr = null;
    if (value == Interval.NEG_INF) {
        valueExpr = parseExpression("Interval.NEG_INF");
    } else if (value == Interval.POS_INF) {
        valueExpr = parseExpression("Interval.POS_INF");
    } else if (value instanceof BigDecimal) {
        BigDecimal bigDecimal = (BigDecimal) value;
        ObjectCreationExpr newExpression = parseExpression("new BigDecimal()");
        StringLiteralExpr stringRep = new StringLiteralExpr(bigDecimal.toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else if (value instanceof String) {
        String string = (String) value;
        StringLiteralExpr newExpression = new StringLiteralExpr();
        newExpression.setString(string);
        valueExpr = newExpression;
    } else if (value instanceof Boolean) {
        Boolean b = (Boolean) value;
        valueExpr = new BooleanLiteralExpr(b);
    } else if (value instanceof LocalDate) {
        LocalDate localDateTime = (LocalDate) value;
        MethodCallExpr newExpression = parseExpression("java.time.LocalDate.parse()");
        StringLiteralExpr stringRep = new StringLiteralExpr(localDateTime.toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else if (value instanceof ComparablePeriod) {
        ComparablePeriod comparablePeriod = (ComparablePeriod) value;
        MethodCallExpr newExpression = parseExpression("org.kie.dmn.feel.lang.types.impl.ComparablePeriod.parse()");
        StringLiteralExpr stringRep = new StringLiteralExpr(comparablePeriod.asPeriod().toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else {
        throw new UnsupportedOperationException("boundAsExpression value " + value + " not supported.");
    }
    Expression typeExpr = null;
    if (bound.getBoundaryType() == RangeBoundary.OPEN) {
        typeExpr = parseExpression("RangeBoundary.OPEN");
    } else if (bound.getBoundaryType() == RangeBoundary.CLOSED) {
        typeExpr = parseExpression("RangeBoundary.CLOSED");
    } else {
        throw new IllegalStateException("illegal getBoundaryType");
    }
    ObjectCreationExpr newExpression = parseExpression("new Bound()");
    newExpression.addArgument(valueExpr);
    newExpression.addArgument(typeExpr);
    newExpression.addArgument(new NullLiteralExpr());
    return newExpression;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ComparablePeriod (org.kie.dmn.feel.lang.types.impl.ComparablePeriod)3 LocalDate (java.time.LocalDate)2 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BigDecimal (java.math.BigDecimal)1 Period (java.time.Period)1 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)1